Ticket #2086: ffmpeg-72ff8ee-wg.diff
| File ffmpeg-72ff8ee-wg.diff, 56.3 KB (added by , 14 years ago) |
|---|
-
configure
diff --git a/configure b/configure index faea887..f933fd8 100755
a b External library support: 232 232 native MPEG-4/Xvid encoder exists [no] 233 233 --enable-openal enable OpenAL 1.1 capture support [no] 234 234 --enable-openssl enable openssl [no] 235 --enable-teletext-zvbi enable teletext support via libzvbi [no] 235 236 --enable-zlib enable zlib [autodetect] 236 237 237 238 Advanced options (experts only): … … CONFIG_LIST=" 1206 1207 sram 1207 1208 static 1208 1209 swscale_alpha 1210 teletext_zvbi 1209 1211 thumb 1210 1212 vaapi 1211 1213 vda … … zlib_decoder_select="zlib" 1775 1777 zlib_encoder_select="zlib" 1776 1778 zmbv_decoder_select="zlib" 1777 1779 zmbv_encoder_select="zlib" 1780 #teletext_decoder_select="teletext_zvbi" 1778 1781 1779 1782 # hardware accelerators 1780 1783 crystalhd_deps="libcrystalhd_libcrystalhd_if_h" … … libxavs_encoder_deps="libxavs" 1858 1861 libxvid_encoder_deps="libxvid" 1859 1862 libutvideo_decoder_deps="libutvideo" 1860 1863 libutvideo_encoder_deps="libutvideo" 1864 teletext_decoder_deps="teletext_zvbi" 1861 1865 1862 1866 # demuxers / muxers 1863 1867 ac3_demuxer_select="ac3_parser" … … enabled openssl && { check_lib openssl/ssl.h SSL_library_init -lssl -lcrypto 3872 3876 check_lib openssl/ssl.h SSL_library_init -lssl32 -leay32 || 3873 3877 check_lib openssl/ssl.h SSL_library_init -lssl -lcrypto -lws2_32 -lgdi32 || 3874 3878 die "ERROR: openssl not found"; } 3879 enabled teletext_zvbi && require libzvbi libzvbi.h vbi_decoder_new -lzvbi 3875 3880 3876 3881 if enabled gnutls; then 3877 3882 { check_lib nettle/bignum.h nettle_mpz_get_str_256 -lnettle -lhogweed -lgmp && enable nettle; } || … … echo "libxavs enabled ${libxavs-no}" 4273 4278 echo "libxvid enabled ${libxvid-no}" 4274 4279 echo "openal enabled ${openal-no}" 4275 4280 echo "openssl enabled ${openssl-no}" 4281 echo "teletext_zvbi enabled ${teletext_zvbi-no}" 4276 4282 echo "zlib enabled ${zlib-no}" 4277 4283 echo "bzlib enabled ${bzlib-no}" 4278 4284 echo "texi2html enabled ${texi2html-no}" -
ffmpeg.c
diff --git a/ffmpeg.c b/ffmpeg.c index 977218f..5b05549 100644
a b static int64_t video_size = 0; 117 117 static int64_t audio_size = 0; 118 118 static int64_t subtitle_size = 0; 119 119 static int64_t extra_size = 0; 120 static int64_t video_iframe_count = 0; 121 static int64_t video_iframe_last; 122 static int64_t video_iframe_delta_sum = 0; 120 123 static int nb_frames_dup = 0; 121 124 static int nb_frames_drop = 0; 122 125 … … int nb_output_files = 0; 145 148 FilterGraph **filtergraphs; 146 149 int nb_filtergraphs; 147 150 151 static double vdelta_sum; 152 static long vdelta_n; 153 148 154 #if HAVE_TERMIOS_H 149 155 150 156 /* init terminal so that we can grab keys */ … … static void write_frame(AVFormatContext *s, AVPacket *pkt, OutputStream *ost) 573 579 } 574 580 575 581 pkt->stream_index = ost->index; 582 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && 583 pkt->flags & AV_PKT_FLAG_KEY && pkt->pts != AV_NOPTS_VALUE) { 584 int64_t pts = av_rescale_q(pkt->pts, 585 s->streams[pkt->stream_index]->time_base, 586 AV_TIME_BASE_Q); 587 if (video_iframe_count++ > 0) 588 video_iframe_delta_sum += pts - video_iframe_last; 589 video_iframe_last = pts; 590 } 576 591 577 592 if (debug_ts) { 578 593 av_log(NULL, AV_LOG_INFO, "muxer <- type:%s " … … static void do_video_out(AVFormatContext *s, 796 811 797 812 sync_ipts = in_picture->pts; 798 813 delta = sync_ipts - ost->sync_opts + duration; 814 vdelta_sum += delta; 815 ++vdelta_n; 799 816 800 817 /* by default, we output a single frame */ 801 818 nb_frames = 1; … … static void print_report(int is_last_report, int64_t timer_start, int64_t cur_ti 1245 1262 if(video_size + audio_size + subtitle_size + extra_size == 0){ 1246 1263 av_log(NULL, AV_LOG_WARNING, "Output file is empty, nothing was encoded (check -ss / -t / -frames parameters if used)\n"); 1247 1264 } 1265 if (vdelta_n > 0) 1266 av_log(NULL, AV_LOG_VERBOSE, "vdelta_avg:%f\n", vdelta_sum/(double)vdelta_n); 1267 if (video_iframe_count > 0) 1268 av_log(NULL, AV_LOG_VERBOSE, "iframes_delta_avg:%f sec\n", (double)video_iframe_delta_sum/(video_iframe_count*AV_TIME_BASE)); 1248 1269 } 1249 1270 } 1250 1271 … … static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *p 1353 1374 av_init_packet(&opkt); 1354 1375 1355 1376 if ((!ost->frame_number && !(pkt->flags & AV_PKT_FLAG_KEY)) && 1356 !ost->copy_initial_nonkeyframes) 1377 !ost->copy_initial_nonkeyframes) { 1378 int64_t d = ist->next_pts - of->start_time; 1379 fprintf(stderr, "ist:%d dropping non-keyframe %"PRId64"\n", 1380 ist->st->index, d); 1381 av_interleaved_discard(of->ctx, d); 1382 // input context start_time is effectively increased by d 1383 if (!copy_ts) 1384 input_files[ist->file_index]->ts_offset -= d; 1357 1385 return; 1386 } 1358 1387 1359 1388 if (!ost->frame_number && ist->pts < of->start_time && 1360 1389 !ost->copy_prior_start) … … static int process_input(int file_index) 2788 2817 av_ts2timestr(input_files[ist->file_index]->ts_offset, &AV_TIME_BASE_Q)); 2789 2818 } 2790 2819 2820 // Teletext streams are often shared between programs 2821 // and their absolute timestamps are meaningless 2822 if (ist->st->codec->codec_id == CODEC_ID_DVB_TELETEXT && !copy_ts) { 2823 pkt.dts = pkt.pts = av_rescale_q(av_get_cur_dts(is, -1), AV_TIME_BASE_Q, ist->st->time_base); 2824 //fprintf(stderr, "teletext %d ts set to %f -> %"PRId64"\n", ist_index, ipts_min, pkt.dts/90); 2825 } 2826 2791 2827 if(!ist->wrap_correction_done && is->start_time != AV_NOPTS_VALUE && ist->st->pts_wrap_bits < 64){ 2792 2828 int64_t stime, stime2; 2793 2829 // Correcting starttime based on the enabled streams … … static int process_input(int file_index) 2844 2880 pkt_dts+1<ist->pts){ 2845 2881 ifile->ts_offset -= delta; 2846 2882 av_log(NULL, AV_LOG_DEBUG, 2847 " timestamp discontinuity %"PRId64", new offset= %"PRId64"\n",2848 delta, ifile->ts_offset);2883 "ist_index:%d timestamp discontinuity %"PRId64", new offset= %"PRId64"\n", 2884 ifile->ist_index + pkt.stream_index, delta, ifile->ts_offset); 2849 2885 pkt.dts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base); 2850 2886 if (pkt.pts != AV_NOPTS_VALUE) 2851 2887 pkt.pts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base); -
ffmpeg_opt.c
diff --git a/ffmpeg_opt.c b/ffmpeg_opt.c index fcd6048..e2b63da 100644
a b static void add_input_streams(OptionsContext *o, AVFormatContext *ic) 628 628 case AVMEDIA_TYPE_SUBTITLE: 629 629 if(!ist->dec) 630 630 ist->dec = avcodec_find_decoder(dec->codec_id); 631 if (dec->codec_id == CODEC_ID_DVB_SUBTITLE || 632 dec->codec_id == CODEC_ID_DVB_TELETEXT) 633 ist->fix_sub_duration = 1; 631 634 MATCH_PER_STREAM_OPT(fix_sub_duration, i, ist->fix_sub_duration, ic, st); 632 635 break; 633 636 case AVMEDIA_TYPE_ATTACHMENT: … … static int open_input_file(OptionsContext *o, const char *filename) 810 813 av_log(NULL, AV_LOG_WARNING, "%s: could not seek to position %0.3f\n", 811 814 filename, (double)timestamp / AV_TIME_BASE); 812 815 } 816 if (av_get_cur_dts(ic, -1) != AV_NOPTS_VALUE) { 817 av_log(NULL, AV_LOG_INFO, "*** seek timestamp=%"PRId64"\n", timestamp); 818 timestamp = av_get_cur_dts(ic, -1); 819 av_log(NULL, AV_LOG_INFO, "*** after seek timestamp=%"PRId64"\n", timestamp); 820 } 821 /* reload stream parameters */ 822 if (ic->nb_streams > orig_nb_streams) 823 opts = grow_array(opts, sizeof *opts, &orig_nb_streams, ic->nb_streams); 824 ret = avformat_find_stream_info(ic, opts); 825 if (ret < 0) { 826 av_log(NULL, AV_LOG_FATAL, "%s: could not find codec parameters\n", filename); 827 avformat_close_input(&ic); 828 exit(1); 829 } 813 830 } 814 831 815 832 /* update the current parameters so that they match the one of the input stream */ -
libavcodec/Makefile
diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 27c9a11..9bd17d9 100644
a b OBJS-$(CONFIG_LIBVPX_ENCODER) += libvpxenc.o 702 702 OBJS-$(CONFIG_LIBX264_ENCODER) += libx264.o 703 703 OBJS-$(CONFIG_LIBXAVS_ENCODER) += libxavs.o 704 704 OBJS-$(CONFIG_LIBXVID_ENCODER) += libxvid.o 705 OBJS-$(CONFIG_TELETEXT_DECODER) += teletextdec.o pes_parser.o 705 706 706 707 # parsers 707 708 OBJS-$(CONFIG_AAC_PARSER) += aac_parser.o aac_ac3_parser.o \ … … OBJS-$(CONFIG_MPEGAUDIO_PARSER) += mpegaudio_parser.o \ 738 739 mpegaudiodecheader.o mpegaudiodata.o 739 740 OBJS-$(CONFIG_MPEGVIDEO_PARSER) += mpegvideo_parser.o \ 740 741 mpeg12.o mpeg12data.o 742 OBJS-$(CONFIG_PES_PARSER) += pes_parser.o 741 743 OBJS-$(CONFIG_PNM_PARSER) += pnm_parser.o pnm.o 742 744 OBJS-$(CONFIG_RV30_PARSER) += rv34_parser.o 743 745 OBJS-$(CONFIG_RV40_PARSER) += rv34_parser.o -
libavcodec/allcodecs.c
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index 987b877..91ccc5e 100644
a b void avcodec_register_all(void) 457 457 REGISTER_ENCDEC (SUBRIP, subrip); 458 458 REGISTER_DECODER(SUBVIEWER, subviewer); 459 459 REGISTER_DECODER(SUBVIEWER1, subviewer1); 460 REGISTER_DECODER(TELETEXT, teletext); 460 461 REGISTER_DECODER(TEXT, text); 461 462 REGISTER_DECODER(VPLAYER, vplayer); 462 463 REGISTER_DECODER(WEBVTT, webvtt); … … void avcodec_register_all(void) 518 519 REGISTER_PARSER(MPEG4VIDEO, mpeg4video); 519 520 REGISTER_PARSER(MPEGAUDIO, mpegaudio); 520 521 REGISTER_PARSER(MPEGVIDEO, mpegvideo); 522 REGISTER_PARSER(PES, pes); 521 523 REGISTER_PARSER(PNG, png); 522 524 REGISTER_PARSER(PNM, pnm); 523 525 REGISTER_PARSER(RV30, rv30); -
libavcodec/dvbsub_parser.c
diff --git a/libavcodec/dvbsub_parser.c b/libavcodec/dvbsub_parser.c index 5274fd6..478e897 100644
a b 30 30 /* parser definition */ 31 31 typedef struct DVBSubParseContext { 32 32 uint8_t *packet_buf; 33 int packet_start;33 int cursor; 34 34 int packet_index; 35 35 int in_packet; 36 36 } DVBSubParseContext; … … static int dvbsub_parse(AVCodecParserContext *s, 49 49 const uint8_t *buf, int buf_size) 50 50 { 51 51 DVBSubParseContext *pc = s->priv_data; 52 uint8_t *p, *p_end; 53 int i, len, buf_pos = 0; 52 int i; 54 53 55 54 av_dlog(avctx, "DVB parse packet pts=%"PRIx64", lpts=%"PRIx64", cpts=%"PRIx64":\n", 56 55 s->pts, s->last_pts, s->cur_frame_pts[s->cur_frame_start_index]); … … static int dvbsub_parse(AVCodecParserContext *s, 68 67 *poutbuf = NULL; 69 68 *poutbuf_size = 0; 70 69 71 s->fetch_timestamp = 1; 72 73 if (s->last_pts != s->pts && s->pts != AV_NOPTS_VALUE) /* Start of a new packet */ 74 { 75 if (pc->packet_index != pc->packet_start) 76 { 77 av_dlog(avctx, "Discarding %d bytes\n", 78 pc->packet_index - pc->packet_start); 79 } 80 81 pc->packet_start = 0; 82 pc->packet_index = 0; 83 70 if (!pc->in_packet) { 84 71 if (buf_size < 2 || buf[0] != 0x20 || buf[1] != 0x00) { 85 72 av_dlog(avctx, "Bad packet header\n"); 86 return -1;73 return buf_size; /* discard data */ 87 74 } 88 89 buf_pos = 2; 90 75 if (buf_size-2 > PARSE_BUF_SIZE) 76 return -1; 77 memcpy(pc->packet_buf, buf+2, buf_size-2); 78 pc->cursor = 0; 79 pc->packet_index = buf_size-2; 91 80 pc->in_packet = 1; 92 81 } else { 93 if (pc->packet_start != 0) 94 { 95 if (pc->packet_index != pc->packet_start) 96 { 97 memmove(pc->packet_buf, pc->packet_buf + pc->packet_start, 98 pc->packet_index - pc->packet_start); 99 100 pc->packet_index -= pc->packet_start; 101 pc->packet_start = 0; 102 } else { 103 pc->packet_start = 0; 104 pc->packet_index = 0; 105 } 106 } 82 if (buf_size + pc->packet_index > PARSE_BUF_SIZE) 83 return -1; 84 memcpy(pc->packet_buf + pc->packet_index, buf, buf_size); 85 pc->packet_index += buf_size; 107 86 } 108 87 109 if (buf_size - buf_pos + pc->packet_index > PARSE_BUF_SIZE) 110 return -1; 111 112 /* if not currently in a packet, discard data */ 113 if (pc->in_packet == 0) 114 return buf_size; 115 116 memcpy(pc->packet_buf + pc->packet_index, buf + buf_pos, buf_size - buf_pos); 117 pc->packet_index += buf_size - buf_pos; 118 119 p = pc->packet_buf; 120 p_end = pc->packet_buf + pc->packet_index; 121 122 while (p < p_end) 123 { 124 if (*p == 0x0f) 125 { 126 if (p + 6 <= p_end) 127 { 128 len = AV_RB16(p + 4); 88 /* parse newly obtained segments */ 89 while (pc->cursor < pc->packet_index) { 90 if (pc->packet_buf[pc->cursor] == 0x0f) { 91 if (pc->cursor + 6 <= pc->packet_index) { 92 int len = AV_RB16(pc->packet_buf + pc->cursor + 4); 129 93 130 if (p + len + 6 <= p_end) 131 { 132 *poutbuf_size += len + 6; 133 134 p += len + 6; 94 if (pc->cursor + len + 6 <= pc->packet_index) { 95 pc->cursor += len + 6; 135 96 } else 136 97 break; 137 98 } else 138 99 break; 139 } else if (*p == 0xff) { 140 if (p + 1 < p_end) 141 { 100 } else if (pc->packet_buf[pc->cursor] == 0xff) { 101 if (pc->cursor + 1 < pc->packet_index) { 142 102 av_dlog(avctx, "Junk at end of packet\n"); 143 103 } 144 pc->packet_index = p - pc->packet_buf; 104 *poutbuf = pc->packet_buf; 105 *poutbuf_size = pc->cursor; 106 pc->packet_index = 0; 145 107 pc->in_packet = 0; 108 av_dlog(avctx, "parse packet sz:%d\n", *poutbuf_size); 146 109 break; 147 110 } else { 148 111 av_log(avctx, AV_LOG_ERROR, "Junk in packet\n"); 149 112 150 pc->packet_index = p - pc->packet_buf;151 113 pc->in_packet = 0; 152 114 break; 153 115 } 154 116 } 155 117 156 if (*poutbuf_size > 0)157 {158 *poutbuf = pc->packet_buf;159 pc->packet_start = *poutbuf_size;160 }161 162 if (s->pts == AV_NOPTS_VALUE)163 s->pts = s->last_pts;164 165 118 return buf_size; 166 119 } 167 120 -
new file libavcodec/pes_parser.c
diff --git a/libavcodec/pes_parser.c b/libavcodec/pes_parser.c new file mode 100644 index 0000000..074b3ad
- + 1 /* 2 * PES parsing for ffmpeg 3 * Copyright (c) 2008, 2011 Wolfram Gloger. 4 * 5 * This file is part of FFmpeg. 6 * 7 * FFmpeg is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * FFmpeg is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public 18 * License along with FFmpeg; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 20 */ 21 #include "avcodec.h" 22 #include "get_bits.h" 23 24 /* parser definition */ 25 typedef struct PESParseContext { 26 uint8_t *packet; 27 int packet_len; 28 int packet_index; 29 } PESParseContext; 30 31 static int pes_parse_init(AVCodecParserContext *s) 32 { 33 return 0; 34 } 35 36 static int pes_parse(AVCodecParserContext *s, 37 AVCodecContext *avctx, 38 const uint8_t **poutbuf, int *poutbuf_size, 39 const uint8_t *buf, int buf_size) 40 { 41 int old_len; 42 PESParseContext *pc = s->priv_data; 43 44 if (pc->packet_index == 0) { 45 if (buf_size < 6 || buf[0]!=0x00 || buf[1]!=0x00 || buf[2]!=0x01) { 46 av_log(avctx, AV_LOG_DEBUG, "PES wrong header\n"); 47 *poutbuf = NULL; 48 *poutbuf_size = 0; 49 return buf_size; 50 } 51 old_len = pc->packet_len; 52 pc->packet_len = AV_RB16(buf + 4); 53 if (pc->packet_len == 0) /* unbounded */ 54 pc->packet_len = buf_size; 55 else 56 pc->packet_len += 6; 57 //av_log(avctx, AV_LOG_DEBUG, "PES len:%d\n", pc->packet_len); 58 if (pc->packet_len > old_len) { 59 av_freep(&pc->packet); 60 pc->packet = av_malloc(pc->packet_len); 61 } 62 } 63 if (pc->packet) { 64 if (pc->packet_index + buf_size <= pc->packet_len) { 65 memcpy(pc->packet + pc->packet_index, buf, buf_size); 66 pc->packet_index += buf_size; 67 if (pc->packet_index >= pc->packet_len) { 68 *poutbuf = pc->packet; 69 *poutbuf_size = pc->packet_len; 70 pc->packet_index = 0; 71 return buf_size; 72 } 73 } else { 74 /* erroneous size */ 75 pc->packet_index = 0; 76 } 77 } 78 *poutbuf = NULL; 79 *poutbuf_size = 0; 80 return buf_size; 81 } 82 83 static void pes_parse_close(AVCodecParserContext *s) 84 { 85 PESParseContext *pc = s->priv_data; 86 av_freep(&pc->packet); 87 } 88 89 AVCodecParser ff_pes_parser = { 90 { CODEC_ID_DVB_TELETEXT }, 91 sizeof(PESParseContext), 92 pes_parse_init, 93 pes_parse, 94 pes_parse_close, 95 }; -
libavcodec/srtenc.c
diff --git a/libavcodec/srtenc.c b/libavcodec/srtenc.c index 3036a7f..33e9d8d 100644
a b static av_cold int srt_encode_init(AVCodecContext *avctx) 137 137 { 138 138 SRTContext *s = avctx->priv_data; 139 139 s->avctx = avctx; 140 if (!avctx->subtitle_header) 141 return 0; 140 142 s->ass_ctx = ff_ass_split(avctx->subtitle_header); 141 143 return s->ass_ctx ? 0 : AVERROR_INVALIDDATA; 142 144 } … … static void srt_end_cb(void *priv) 225 227 srt_print(priv, "\r\n\r\n"); 226 228 } 227 229 230 static void srt_print_timing(SRTContext *s, int sc, int ec) 231 { 232 int sh, sm, ss; 233 int eh, em, es; 234 235 sh = sc/3600000; sc -= 3600000*sh; 236 sm = sc/ 60000; sc -= 60000*sm; 237 ss = sc/ 1000; sc -= 1000*ss; 238 eh = ec/3600000; ec -= 3600000*eh; 239 em = ec/ 60000; ec -= 60000*em; 240 es = ec/ 1000; ec -= 1000*es; 241 srt_print(s,"%d\r\n%02d:%02d:%02d,%03d --> %02d:%02d:%02d,%03d\r\n", 242 ++s->count, sh, sm, ss, sc, eh, em, es, ec); 243 } 244 228 245 static const ASSCodesCallbacks srt_callbacks = { 229 246 .text = srt_text_cb, 230 247 .new_line = srt_new_line_cb, … … static int srt_encode_frame(AVCodecContext *avctx, 250 267 251 268 for (i=0; i<sub->num_rects; i++) { 252 269 253 if (sub->rects[i]->type != SUBTITLE_ASS) { 254 av_log(avctx, AV_LOG_ERROR, "Only SUBTITLE_ASS type supported.\n"); 255 return AVERROR(ENOSYS); 256 } 270 switch (sub->rects[i]->type) { 271 case SUBTITLE_TEXT: { 272 int sc0 = av_rescale(sub->pts, 1000, AV_TIME_BASE); 257 273 274 if (avctx->codec->id == CODEC_ID_SRT) 275 srt_print_timing(s, 276 sc0 + sub->start_display_time, 277 sc0 + sub->end_display_time); 278 srt_print(s, sub->rects[i]->text); 279 break; 280 } 281 case SUBTITLE_ASS: 282 if (!s->ass_ctx) { 283 av_log(avctx, AV_LOG_ERROR, "Got SUBTITLE_ASS but no header.\n"); 284 return AVERROR_INVALIDDATA; 285 } 258 286 dialog = ff_ass_split_dialog(s->ass_ctx, sub->rects[i]->ass, 0, &num); 259 287 for (; dialog && num--; dialog++) { 260 288 if (avctx->codec->id == AV_CODEC_ID_SRT) { 261 int sh, sm, ss, sc = 10 * dialog->start; 262 int eh, em, es, ec = 10 * dialog->end; 263 sh = sc/3600000; sc -= 3600000*sh; 264 sm = sc/ 60000; sc -= 60000*sm; 265 ss = sc/ 1000; sc -= 1000*ss; 266 eh = ec/3600000; ec -= 3600000*eh; 267 em = ec/ 60000; ec -= 60000*em; 268 es = ec/ 1000; ec -= 1000*es; 269 srt_print(s,"%d\r\n%02d:%02d:%02d,%03d --> %02d:%02d:%02d,%03d\r\n", 270 ++s->count, sh, sm, ss, sc, eh, em, es, ec); 289 srt_print_timing(s, 10 * dialog->start, 10 * dialog->end); 271 290 s->dialog_start = s->ptr - 2; 272 291 } 273 292 s->alignment_applied = 0; 274 293 srt_style_apply(s, dialog->style); 275 294 ff_ass_split_override_codes(&srt_callbacks, s, dialog->text); 276 295 } 277 } 296 break; 297 default: 298 av_log(avctx, AV_LOG_ERROR, "Only SUBTITLE_TEXT, SUBTITLE_ASS types supported.\n"); 299 return AVERROR(ENOSYS); 300 } 301 302 } 278 303 279 304 if (s->ptr == s->buffer) 280 305 return 0; -
new file libavcodec/teletextdec.c
diff --git a/libavcodec/teletextdec.c b/libavcodec/teletextdec.c new file mode 100644 index 0000000..a415a1d
- + 1 /* 2 * Teletext decoding for ffmpeg 3 * Copyright (c) 2005-2010, 2012 Wolfram Gloger. 4 * 5 * This library is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU Lesser General Public 7 * License as published by the Free Software Foundation; either 8 * version 2 of the License, or (at your option) any later version. 9 * 10 * This library is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public 16 * License along with this library; if not, write to the Free Software 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 */ 19 20 #include "avcodec.h" 21 #include "libavutil/opt.h" 22 23 #include <libzvbi.h> 24 25 //#define DEBUG 1 26 27 #define TEXT_MAXSZ 2044 28 #define BINARIZE 0 29 #define PALETTE_MAX 40 30 #define RGBA(r,g,b,a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b)) 31 #define VBI_R(rgba) (((rgba) >> 0) & 0xFF) 32 #define VBI_G(rgba) (((rgba) >> 8) & 0xFF) 33 #define VBI_B(rgba) (((rgba) >> 16) & 0xFF) 34 #define VBI_A(rgba) (((rgba) >> 24) & 0xFF) 35 36 /* main data structure */ 37 typedef struct TeletextContext 38 { 39 AVClass *class; 40 char *pgno; 41 int y_offset; 42 int ci_clip; 43 int gen_bitmap; 44 int force_chop_top; 45 int sub_display_time; /* in msec */ 46 47 int lines_processed; 48 int last_rows, last_cols; 49 AVSubtitleRect *sub_rect; 50 51 vbi_decoder * vbi; 52 vbi_dvb_demux * dx; 53 #ifdef DEBUG 54 vbi_export * ex; 55 #endif 56 /* Don't even _think_ about making sliced stack-local! */ 57 vbi_sliced sliced[64]; 58 } TeletextContext; 59 60 /************************************************************************/ 61 62 static int 63 teletext_decode_frame(AVCodecContext *avctx, 64 void *data, int *data_size, 65 AVPacket *pkt) 66 { 67 TeletextContext *ctx = avctx->priv_data; 68 AVSubtitle *sub = data; 69 const uint8_t *buf = pkt->data; 70 unsigned int left = pkt->size; 71 72 while (left > 0) { 73 int64_t pts = 0; 74 unsigned int lines = vbi_dvb_demux_cor(ctx->dx, ctx->sliced, 64, &pts, &buf, &left); 75 #ifdef DEBUG 76 av_log(avctx, AV_LOG_INFO, 77 " Teletext: ctx=%p buf_size=%d left=%u lines=%u pts=%f\n", 78 ctx, pkt->size, left, lines, (double)pts/90000.0); 79 #endif 80 if (lines > 0) { 81 #ifdef DEBUGx 82 int i; 83 for(i=0; i<lines; ++i) 84 if (ctx->sliced[i].id >= 0) 85 av_log(avctx, AV_LOG_INFO, 86 " Teletext: lines=%d id=%x\n", i, ctx->sliced[i].id); 87 #endif 88 vbi_decode(ctx->vbi, ctx->sliced, lines, (double)pts/90000.0); 89 ctx->lines_processed += lines; 90 } 91 } 92 93 // is there a subtitle to pass? 94 if (ctx->sub_rect) { 95 sub->format = 0; 96 sub->start_display_time = 0; 97 sub->end_display_time = ctx->sub_display_time; 98 sub->num_rects = 0; 99 100 if (ctx->sub_rect->type != SUBTITLE_NONE) { 101 sub->rects = av_malloc(sizeof(*sub->rects) * 1); 102 if (sub->rects) { 103 sub->num_rects = 1; 104 sub->rects[0] = ctx->sub_rect; 105 } 106 } else { 107 av_log(avctx, AV_LOG_DEBUG, " Teletext: sending empty sub\n"); 108 sub->rects = 0; 109 } 110 if (!sub->rects) // no rect was passed 111 av_free(ctx->sub_rect); 112 ctx->sub_rect = 0; 113 114 *data_size = 1; 115 } else 116 *data_size = 0; 117 118 return pkt->size; 119 } 120 121 static int 122 chop_spaces_utf8(const unsigned char* t, int len) 123 { 124 t += len; 125 while (len > 0) { 126 if (*--t != ' ' || (len-1 > 0 && *(t-1) & 0x80)) 127 break; 128 --len; 129 } 130 return len; 131 } 132 133 // draw a page as text 134 static int 135 gen_sub_text(TeletextContext *ctx, vbi_page *page, int chop_top) 136 { 137 AVSubtitleRect *sub_rect = ctx->sub_rect; 138 char *text; 139 const char *in; 140 char *out; 141 char *vbi_text = av_malloc(TEXT_MAXSZ); 142 int sz = vbi_print_page_region(page, vbi_text, TEXT_MAXSZ-1, "UTF-8", 143 /*table mode*/ TRUE, FALSE, 144 0, chop_top, 145 page->columns, page->rows-chop_top); 146 if (sz <= 0) { 147 av_log(0, AV_LOG_ERROR, "vbi_print error\n"); 148 av_free(vbi_text); 149 return -1; 150 } 151 vbi_text[sz] = '\0'; 152 in = vbi_text; 153 out = text = av_malloc(TEXT_MAXSZ); 154 for (;;) { 155 int nl, sz; 156 157 // skip leading spaces and newlines 158 in += strspn(in, " \n"); 159 // compute end of row 160 for (nl = 0; in[nl]; ++nl) 161 if (in[nl] == '\n' && (nl==0 || !(in[nl-1] & 0x80))) 162 break; 163 if (!in[nl]) 164 break; 165 // skip trailing spaces 166 sz = chop_spaces_utf8(in, nl); 167 memcpy(out, in, sz); 168 out += sz; 169 *out++ = '\n'; 170 in += nl; 171 } 172 av_free(vbi_text); 173 *out = '\0'; 174 if (out > text) { 175 sub_rect->type = SUBTITLE_TEXT; 176 sub_rect->text = text; 177 av_log(0, AV_LOG_DEBUG, "subtext:%s:txetbus\n", text); 178 } else { 179 sub_rect->type = SUBTITLE_NONE; 180 av_free(text); 181 } 182 return 0; 183 } 184 185 static int 186 find_ci_in_page(const vbi_rgba *pix, const vbi_page *page) 187 { 188 static int last_i = 0; // cache for performance 189 190 if (*pix == page->color_map[last_i]) 191 return last_i; 192 for (last_i=0; 193 last_i<(sizeof page->color_map)/(sizeof page->color_map[0]); 194 ++last_i) { 195 if (*pix == page->color_map[last_i]) 196 return last_i; 197 } 198 return -1; 199 } 200 201 // draw a page as bitmap 202 static int 203 gen_sub_bitmap(TeletextContext *ctx, vbi_page *page, int chop_top) 204 { 205 AVSubtitleRect *sub_rect = ctx->sub_rect; 206 int resx = page->columns*12, resy = page->rows*10; 207 int ix, iy, ci, ci_max = 0, ci_max_count = 0; 208 const vbi_rgba* vptr; 209 uint8_t *bptr; 210 char *canvas = av_malloc(resx*resy*sizeof(vbi_rgba)); 211 212 if (!canvas) 213 return -1; 214 215 /* only VBI_PIXFMT_RGBA32_LE is supported in libzvbi */ 216 vbi_draw_vt_page(page, VBI_PIXFMT_RGBA32_LE, canvas, 217 /*reveal*/ 1, /*flash*/ 1); 218 219 if (chop_top) { 220 resy -= 10; 221 av_log(0, AV_LOG_DEBUG, " Teletext: chopping top line\n"); 222 } 223 //avpicture_alloc(&sub_rect->pict, PIX_FMT_PAL8, resx, resy); 224 sub_rect->pict.data[0] = av_mallocz(resx*resy); 225 226 vptr = (const vbi_rgba *)canvas + (chop_top*10*resx); 227 bptr = sub_rect->pict.data[0]; 228 229 for (iy=0; iy<resy; ++iy) { 230 for (ix=0; ix<resx; ++ix, ++vptr, ++bptr) { 231 ci = find_ci_in_page(vptr, page); 232 if (ci < 0) 233 av_log(0, AV_LOG_ERROR, 234 " Teletext: can't find pixel in LUT!\n"); 235 else { 236 if (ci >= ctx->ci_clip) 237 ci = ci % ctx->ci_clip; 238 if (ci > ci_max) 239 ci_max = ci; 240 if (ci == ci_max) 241 ++ci_max_count; 242 *bptr = BINARIZE ? (!!ci) : (uint8_t)ci; 243 } 244 } 245 } 246 av_free(canvas); 247 248 if (ci_max_count == resx*resy) { 249 av_log(0, AV_LOG_DEBUG, " Teletext: dropping empty page\n"); 250 // send empty subtitle 251 //avpicture_free(&sub_rect->pict); 252 av_freep(&sub_rect->pict.data[0]); 253 sub_rect->type = SUBTITLE_NONE; 254 } else { 255 sub_rect->x = resx<720 ? (720 - resx)/2 : 0; 256 sub_rect->y = resy<576 ? (576 - resy) : 0; 257 if (sub_rect->y > ctx->y_offset) 258 sub_rect->y -= ctx->y_offset; 259 sub_rect->w = resx; 260 sub_rect->h = resy; 261 sub_rect->nb_colors = ci_max+1; 262 sub_rect->pict.linesize[0] = resx; 263 sub_rect->pict.data[1] = av_malloc(sub_rect->nb_colors * sizeof(uint32_t)); 264 //sub_rect->rgba_palette = 265 // av_malloc(sizeof(*sub_rect->rgba_palette)* 266 // sub_rect->nb_colors); 267 for (ci=0; ci<sub_rect->nb_colors; ++ci) { 268 int r, g, b, a; 269 270 r = VBI_R(page->color_map[ci]); 271 g = VBI_G(page->color_map[ci]); 272 b = VBI_B(page->color_map[ci]); 273 a = (r+g+b == 0) ? 0 : VBI_A(page->color_map[ci]); 274 ((uint32_t *)sub_rect->pict.data[1])[ci] = 275 RGBA(r, g, b, a); 276 #ifdef DEBUG 277 av_log(0, AV_LOG_DEBUG, " Teletext: palette %0x\n", 278 ((uint32_t *)sub_rect->pict.data[1])[ci]); 279 #endif 280 } 281 sub_rect->type = SUBTITLE_BITMAP; 282 } 283 #ifdef DEBUG 284 fprintf(stderr, "ci_max=%d...\n", ci_max); 285 #endif 286 return 0; 287 } 288 289 static void 290 handler(vbi_event *ev, void *user_data) 291 { 292 TeletextContext *ctx = user_data; 293 vbi_page page; 294 int res; 295 char pgno_str[12]; 296 297 snprintf(pgno_str, sizeof pgno_str, "%03x", ev->ev.ttx_page.pgno); 298 av_log(0, AV_LOG_DEBUG, "Page %s.%02x %s\n", 299 pgno_str, ev->ev.ttx_page.subno & 0xFF, ctx->pgno); 300 301 if (strcmp(ctx->pgno, "*") && !strstr(ctx->pgno, pgno_str)) { 302 return; 303 } 304 305 /* Fetch the page. */ 306 res = vbi_fetch_vt_page(ctx->vbi, &page, 307 ev->ev.ttx_page.pgno, 308 ev->ev.ttx_page.subno, 309 VBI_WST_LEVEL_3p5, 25, TRUE); 310 311 #ifdef DEBUG 312 fprintf(stderr, "\nSaving res=%d dy0=%d dy1=%d...\n", 313 res, page.dirty.y0, page.dirty.y1); 314 fflush(stderr); 315 316 if (!vbi_export_stdio(ctx->ex, stderr, &page)) { 317 fprintf(stderr, "failed: %s\n", vbi_export_errstr(ctx->ex)); 318 //exit(EXIT_FAILURE); 319 } 320 #endif 321 322 // TODO: check if page changed 323 if (page.columns!=ctx->last_cols || page.rows!=ctx->last_rows || 1) { 324 vbi_subno subno; 325 char *lang; 326 vbi_page_type vpt = vbi_classify_page(ctx->vbi, 327 ev->ev.ttx_page.pgno, 328 &subno, &lang); 329 int chop_top = ctx->force_chop_top || 330 ((page.rows > 1) && (vpt == VBI_SUBTITLE_PAGE)); 331 332 av_log(0, AV_LOG_DEBUG, "%d x %d page chop:%d\n", 333 page.columns, page.rows, chop_top); 334 335 ctx->sub_rect = av_mallocz(sizeof(*ctx->sub_rect)); 336 res = ctx->gen_bitmap ? 337 gen_sub_bitmap(ctx, &page, chop_top) : 338 gen_sub_text (ctx, &page, chop_top); 339 if (res) 340 av_freep(&ctx->sub_rect); 341 342 ctx->last_cols = page.columns; 343 ctx->last_rows = page.rows; 344 } 345 vbi_unref_page(&page); 346 } 347 348 static int teletext_init_decoder(AVCodecContext *avctx) 349 { 350 TeletextContext *ctx = avctx->priv_data; 351 const char* p_env; 352 353 ctx->sub_rect = 0; 354 355 ctx->vbi = vbi_decoder_new(); 356 assert(NULL != ctx->vbi); 357 vbi_event_handler_add(ctx->vbi, VBI_EVENT_TTX_PAGE, handler, ctx); 358 ctx->dx = vbi_dvb_pes_demux_new (/* callback */ NULL, NULL); 359 assert(NULL != ctx->dx); 360 #ifdef DEBUG 361 { 362 char *t; 363 ctx->ex = vbi_export_new("text", &t); 364 } 365 #endif 366 av_log(avctx, AV_LOG_INFO, "teletext pgno:%s\n", ctx->pgno); 367 p_env = getenv("TELETEXT_Y_OFFSET"); 368 if (p_env) 369 ctx->y_offset = strtol(p_env, 0, 10); 370 ctx->sub_display_time = 10000; 371 ctx->ci_clip = 256; /* dvdsub encoder cannot take more than 16.. */ 372 return 0; 373 } 374 375 static int teletext_close_decoder(AVCodecContext *avctx) 376 { 377 TeletextContext *ctx = avctx->priv_data; 378 379 #ifdef DEBUG 380 av_log(avctx, AV_LOG_INFO, 381 " Teletext: lines_total=%u\n", ctx->lines_processed); 382 #endif 383 av_free(ctx->sub_rect); 384 385 vbi_dvb_demux_delete(ctx->dx); 386 vbi_decoder_delete(ctx->vbi); 387 return 0; 388 } 389 390 #define OFFSET(x) offsetof(TeletextContext, x) 391 #define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM 392 static const AVOption options[] = { 393 {"teletext_page", "Number(s) of teletext page(s) to decode", OFFSET(pgno), AV_OPT_TYPE_STRING, {.str = "100"}, 0, 0, SD}, 394 {"teletext_chop_top", "Force discarding the top teletext line", OFFSET(force_chop_top), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, SD}, 395 {"teletext_bitmap", "Generate bitmap rather than text subtitles", OFFSET(gen_bitmap), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, SD}, 396 {"teletext_y_offset", "y offset of generated bitmaps", OFFSET(y_offset), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1080, SD}, 397 { NULL }, 398 }; 399 400 static const AVClass teletext_class = { 401 .class_name = "teletext", 402 .item_name = av_default_item_name, 403 .option = options, 404 .version = LIBAVUTIL_VERSION_INT, 405 }; 406 407 AVCodec ff_teletext_decoder = { 408 .name = "teletext", 409 .type = AVMEDIA_TYPE_SUBTITLE, 410 .id = CODEC_ID_DVB_TELETEXT, 411 .priv_data_size = sizeof(TeletextContext), 412 .init = teletext_init_decoder, 413 .close = teletext_close_decoder, 414 .decode = teletext_decode_frame, 415 .long_name = NULL_IF_CONFIG_SMALL("Teletext subtitles"), 416 .priv_class= &teletext_class, 417 }; 418 419 /* Local Variables: */ 420 /* indent-tabs-mode:nil */ 421 /* tab-width:4 */ 422 /* c-basic-offset:4 */ 423 /* End: */ -
libavformat/avformat.h
diff --git a/libavformat/avformat.h b/libavformat/avformat.h index c907d4e..fd5f504 100644
a b AVFormatContext *avformat_alloc_output_context(const char *format, 1382 1382 const char *filename); 1383 1383 #endif 1384 1384 1385 void av_interleaved_discard(AVFormatContext *s, int64_t dts_min); 1386 1385 1387 /** 1386 1388 * Allocate an AVFormatContext for an output format. 1387 1389 * avformat_free_context() can be used to free the context and … … AVInputFormat *av_probe_input_format2(AVProbeData *pd, int is_opened, int *score 1441 1443 */ 1442 1444 AVInputFormat *av_probe_input_format3(AVProbeData *pd, int is_opened, int *score_ret); 1443 1445 1446 /* current dts in AV_TIME_BASE units */ 1447 int64_t av_get_cur_dts(AVFormatContext *s, int stream_index); 1448 1444 1449 /** 1445 1450 * Probe a bytestream to determine the input format. Each time a probe returns 1446 1451 * with a score that is too low, the probe buffer size is increased and another -
libavformat/avidec.c
diff --git a/libavformat/avidec.c b/libavformat/avidec.c index 7436b63..68b60d0 100644
a b static int avi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp 1534 1534 index=0; 1535 1535 while(!avi->non_interleaved && index>0 && st2->index_entries[index-1].pos >= pos_min) 1536 1536 index--; 1537 ast2->frame_offset = st2->index_entries[index].timestamp;1537 st2->cur_dts = ast2->frame_offset = st2->index_entries[index].timestamp; 1538 1538 } 1539 1539 1540 1540 /* do the seek */ -
libavformat/mpegenc.c
diff --git a/libavformat/mpegenc.c b/libavformat/mpegenc.c index e2a96bc..1a1d6a5 100644
a b typedef struct PacketDesc { 38 38 int64_t dts; 39 39 int size; 40 40 int unwritten_size; 41 int flags;41 //int flags; 42 42 struct PacketDesc *next; 43 43 } PacketDesc; 44 44 … … typedef struct { 55 55 int lpcm_align; 56 56 int bytes_to_iframe; 57 57 int align_iframe; 58 int64_t vobu_start_pts;59 58 } StreamInfo; 60 59 61 60 typedef struct { … … typedef struct { 80 79 int64_t vcd_padding_bytes_written; 81 80 82 81 int preload; 82 int64_t vobu_start_pts; 83 83 } MpegMuxContext; 84 84 85 85 extern AVOutputFormat ff_mpeg1vcd_muxer; … … static int put_system_header(AVFormatContext *ctx, uint8_t *buf,int only_for_str 267 267 buf[4] = (size - 6) >> 8; 268 268 buf[5] = (size - 6) & 0xff; 269 269 270 //av_log(ctx, AV_LOG_DEBUG, "system header size:%d\n", size); 270 271 return size; 271 272 } 272 273 … … static int mpeg_mux_init(AVFormatContext *ctx) 425 426 bitrate += 10000; 426 427 s->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50); 427 428 } 429 av_log(ctx, AV_LOG_DEBUG, "mux_rate=%d bytes/sec\n", s->mux_rate*50); 428 430 429 431 if (s->is_vcd) { 430 432 double overhead_rate; … … static int mpeg_mux_init(AVFormatContext *ctx) 483 485 } 484 486 s->system_header_size = get_system_header_size(ctx); 485 487 s->last_scr = 0; 488 s->vobu_start_pts = AV_NOPTS_VALUE; 486 489 return 0; 487 490 fail: 488 491 for(i=0;i<ctx->nb_streams;i++) { … … static int flush_packet(AVFormatContext *ctx, int stream_index, 607 610 } 608 611 609 612 if (stream->bytes_to_iframe == 0 || s->packet_number == 0) { 613 //av_log(ctx, AV_LOG_DEBUG, "DVD ALIGN2 pack_num:%d pts:%f\n", s->packet_number, pts/90000.0); 610 614 size = put_system_header(ctx, buf_ptr, 0); 611 615 buf_ptr += size; 612 616 size = buf_ptr - buffer; … … static int mpeg_mux_write_packet(AVFormatContext *ctx, AVPacket *pkt) 1052 1056 AVStream *st = ctx->streams[stream_index]; 1053 1057 StreamInfo *stream = st->priv_data; 1054 1058 int64_t pts, dts; 1055 PacketDesc *pkt_desc ;1056 int preload ;1059 PacketDesc *pkt_desc, *dec_next; 1060 int preload, requeue = 0; 1057 1061 const int is_iframe = st->codec->codec_type == AVMEDIA_TYPE_VIDEO && (pkt->flags & AV_PKT_FLAG_KEY); 1058 1062 1059 1063 preload = av_rescale(s->preload, 90000, AV_TIME_BASE); … … static int mpeg_mux_write_packet(AVFormatContext *ctx, AVPacket *pkt) 1071 1075 av_dlog(ctx, "dts:%f pts:%f flags:%d stream:%d nopts:%d\n", 1072 1076 dts / 90000.0, pts / 90000.0, pkt->flags, 1073 1077 pkt->stream_index, pts != AV_NOPTS_VALUE); 1074 if (!stream->premux_packet) 1078 if (!stream->premux_packet) { 1075 1079 stream->next_packet = &stream->premux_packet; 1080 requeue = 1; 1081 } 1076 1082 *stream->next_packet= 1077 pkt_desc= av_malloc z(sizeof(PacketDesc));1083 pkt_desc= av_malloc(sizeof(PacketDesc)); 1078 1084 pkt_desc->pts= pts; 1079 1085 pkt_desc->dts= dts; 1080 1086 pkt_desc->unwritten_size= 1081 1087 pkt_desc->size= size; 1088 pkt_desc->next= NULL; 1082 1089 if(!stream->predecode_packet) 1083 1090 stream->predecode_packet= pkt_desc; 1091 else if (requeue) { 1092 for (dec_next= stream->predecode_packet; dec_next->next; dec_next= dec_next->next); 1093 dec_next->next = pkt_desc; 1094 } 1095 1084 1096 stream->next_packet= &pkt_desc->next; 1085 1097 1086 1098 if (av_fifo_realloc2(stream->fifo, av_fifo_size(stream->fifo) + size) < 0) 1087 1099 return -1; 1088 1100 1089 1101 if (s->is_dvd){ 1090 if (is_iframe && (s->packet_number == 0 || (pts - stream->vobu_start_pts >= 36000))) { // min VOBU length 0.4 seconds (mpucoder) 1102 if (is_iframe && s->packet_number>0 && pts != AV_NOPTS_VALUE && 1103 s->vobu_start_pts != AV_NOPTS_VALUE && pts - s->vobu_start_pts >= 36000) { // min VOBU length 0.4 seconds (mpucoder) 1091 1104 stream->bytes_to_iframe = av_fifo_size(stream->fifo); 1092 1105 stream->align_iframe = 1; 1093 stream->vobu_start_pts = pts; 1106 s->vobu_start_pts = pts; 1107 } else { 1108 if (s->vobu_start_pts == AV_NOPTS_VALUE) 1109 s->vobu_start_pts = pts; 1094 1110 } 1095 1111 } 1096 1112 -
libavformat/mpegts.c
diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c index a6e07fa..b5780c4 100644
a b static int mpegts_set_stream_info(AVStream *st, PESContext *pes, 699 699 700 700 static void new_pes_packet(PESContext *pes, AVPacket *pkt) 701 701 { 702 /* For a teletext stream, output the complete packet, 703 including header. */ 704 if (pes->st->codec->codec_id == CODEC_ID_DVB_TELETEXT) { 705 if (av_new_packet(pkt, pes->pes_header_size+pes->data_index) == 0) { 706 memcpy(pkt->data, pes->header, pes->pes_header_size); 707 memcpy(pkt->data+pes->pes_header_size, pes->buffer, pes->data_index); 708 av_freep(&pes->buffer); 709 } else 710 av_init_packet(pkt); 711 } else { 712 702 713 av_init_packet(pkt); 703 714 704 715 pkt->destruct = av_destruct_packet; … … static void new_pes_packet(PESContext *pes, AVPacket *pkt) 712 723 } 713 724 memset(pkt->data+pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE); 714 725 726 } 727 715 728 // Separate out the AC3 substream from an HDMV combined TrueHD/AC3 PID 716 729 if (pes->sub_st && pes->stream_type == 0x83 && pes->extended_stream_id == 0x76) 717 730 pkt->stream_index = pes->sub_st->index; -
libavformat/mpegts.h
diff --git a/libavformat/mpegts.h b/libavformat/mpegts.h index 98c4b93..98c89a3 100644
a b 57 57 58 58 #define STREAM_TYPE_AUDIO_AC3 0x81 59 59 #define STREAM_TYPE_AUDIO_DTS 0x8a 60 //#define STREAM_TYPE_TELETEXT 0x101 60 61 61 62 typedef struct MpegTSContext MpegTSContext; 62 63 -
libavformat/mux.c
diff --git a/libavformat/mux.c b/libavformat/mux.c index c7e176a..470ccf5 100644
a b int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt) 763 763 } 764 764 } 765 765 766 void av_interleaved_discard(AVFormatContext *s, int64_t dts_min) 767 { 768 AVPacketList *pktl= s->packet_buffer, **prev= &s->packet_buffer, *next; 769 int i; 770 771 av_log(s, AV_LOG_DEBUG, "av_interleaved_discard dts_min:%"PRId64"\n", dts_min); 772 while(pktl) { 773 const AVStream *st= s->streams[pktl->pkt.stream_index]; 774 next = pktl->next; 775 if(dts_min == AV_NOPTS_VALUE || 776 pktl->pkt.dts*st->time_base.num*AV_TIME_BASE < dts_min*st->time_base.den) { 777 av_log(s, AV_LOG_DEBUG, "av_interleaved_discard %"PRId64"\n", pktl->pkt.dts); 778 if(s->streams[pktl->pkt.stream_index]->last_in_packet_buffer == pktl) 779 s->streams[pktl->pkt.stream_index]->last_in_packet_buffer= NULL; 780 av_free_packet(&pktl->pkt); 781 *prev = next; 782 av_freep(&pktl); 783 } else { 784 int64_t ts_diff= av_rescale_q(dts_min, AV_TIME_BASE_Q, st->time_base); 785 if (pktl->pkt.pts != AV_NOPTS_VALUE) 786 pktl->pkt.pts -= ts_diff; 787 if (pktl->pkt.dts != AV_NOPTS_VALUE) 788 pktl->pkt.dts -= ts_diff; 789 prev = &pktl->next; 790 } 791 pktl= next; 792 } 793 for(i = 0; i < s->nb_streams; i++) { 794 AVStream *st = s->streams[i]; 795 796 if (st->cur_dts != AV_NOPTS_VALUE) { 797 if (!st->last_in_packet_buffer) // all packets have been deleted 798 st->cur_dts = 0; 799 else // some packets remain 800 st->cur_dts -= av_rescale_q(dts_min, AV_TIME_BASE_Q, st->time_base); 801 } 802 } 803 } 804 766 805 int av_write_trailer(AVFormatContext *s) 767 806 { 768 807 int ret, i; -
libavformat/seek.c
diff --git a/libavformat/seek.c b/libavformat/seek.c index 0ae99eb..3e655f2 100644
a b typedef struct { 60 60 * @param tb_lo low timestamp time base 61 61 * @return representation of distance between high and low timestamps 62 62 */ 63 #if 0 63 64 static int64_t ts_distance(int64_t ts_hi, 64 65 AVRational tb_hi, 65 66 int64_t ts_lo, … … static int64_t ts_distance(int64_t ts_hi, 72 73 73 74 return hi - lo; 74 75 } 76 #endif 75 77 76 78 /** 77 79 * Partial search for keyframes in multiple streams. … … static int64_t ts_distance(int64_t ts_hi, 94 96 * @param found_hi ptr to the count of already found high timestamp keyframes 95 97 * @param first_iter flag for first iteration 96 98 */ 97 static voidsearch_hi_lo_keyframes(AVFormatContext *s,99 static int search_hi_lo_keyframes(AVFormatContext *s, 98 100 int64_t timestamp, 99 101 AVRational timebase, 100 102 int flags, … … static void search_hi_lo_keyframes(AVFormatContext *s, 102 104 int keyframes_to_find, 103 105 int *found_lo, 104 106 int *found_hi, 105 int first_iter) 107 int first_iter, 108 int64_t delta_max) 106 109 { 107 110 AVPacket pkt; 108 111 AVSyncPoint *sp; … … static void search_hi_lo_keyframes(AVFormatContext *s, 114 117 int64_t pts, dts; // PTS/DTS from stream 115 118 int64_t ts; // PTS in stream-local time base or position for byte seeking 116 119 AVRational ts_tb; // Time base of the stream or 1:1 for byte seeking 120 int ret = 0; 117 121 118 122 for (;;) { 119 123 if (av_read_frame(s, &pkt) < 0) { … … static void search_hi_lo_keyframes(AVFormatContext *s, 171 175 ts_tb.den = 1; 172 176 } else { 173 177 // otherwise, get stream time_base 178 int64_t delta; 174 179 ts = pts; 175 180 ts_tb = st->time_base; 181 delta = timestamp - av_rescale_q(ts, ts_tb, timebase); 182 if (FFABS(delta) > delta_max) { 183 ret = -1; 184 break; 185 } 176 186 } 177 187 178 188 if (sp->first_ts == AV_NOPTS_VALUE) { … … static void search_hi_lo_keyframes(AVFormatContext *s, 239 249 240 250 // Clean up the parser. 241 251 ff_read_frame_flush(s); 252 return ret; 242 253 } 243 254 244 255 int64_t ff_gen_syncpoint_search(AVFormatContext *s, … … int64_t ff_gen_syncpoint_search(AVFormatContext *s, 257 268 int64_t step; 258 269 int found_lo = 0, found_hi = 0; 259 270 int64_t min_distance, distance; 260 int64_t min_ pos = 0;271 int64_t min_distance2; 261 272 int first_iter = 1; 262 273 AVRational time_base; 274 int64_t delta_max = INT64_MAX; 263 275 264 276 if (flags & AVSEEK_FLAG_BYTE) { 265 277 // for byte seeking, we have exact 1:1 "timestamps" - positions … … int64_t ff_gen_syncpoint_search(AVFormatContext *s, 275 287 time_base.num = 1; 276 288 time_base.den = AV_TIME_BASE; 277 289 } 290 delta_max = av_rescale_q(20*AV_TIME_BASE, AV_TIME_BASE_Q, time_base); 278 291 } 279 292 280 293 // Initialize syncpoint structures for each stream. … … int64_t ff_gen_syncpoint_search(AVFormatContext *s, 315 328 curpos = FFMAX(pos - step / 2, 0); 316 329 for (;;) { 317 330 avio_seek(s->pb, curpos, SEEK_SET); 318 search_hi_lo_keyframes(s, 319 ts, time_base, 320 flags, 321 sync, 322 keyframes_to_find, 323 &found_lo, &found_hi, 324 first_iter); 331 i = search_hi_lo_keyframes(s, 332 ts, time_base, 333 flags, 334 sync, 335 keyframes_to_find, 336 &found_lo, &found_hi, 337 first_iter, 338 delta_max); 339 av_log(s, AV_LOG_ERROR, "search_hi_lo=%d flo:%d fhi:%d ktf:%d delta_max=%lld\n", 340 i, found_lo, found_hi, keyframes_to_find, delta_max); 325 341 if (found_lo == keyframes_to_find && found_hi == keyframes_to_find) 326 342 break; // have all keyframes we wanted 327 343 if (!curpos) … … int64_t ff_gen_syncpoint_search(AVFormatContext *s, 351 367 352 368 // Find actual position to start decoding so that decoder synchronizes 353 369 // closest to ts and between ts_min and ts_max. 354 pos= INT64_MAX;370 min_distance2 = INT64_MAX; 355 371 356 372 for (i = 0; i < s->nb_streams; ++i) { 357 373 st = s->streams[i]; 358 374 if (st->discard < AVDISCARD_ALL) { 375 int64_t cur_dts; 376 int64_t min_pos = 0; 377 int64_t ts_num = st->time_base.num*time_base.den; 378 int64_t ts_den = st->time_base.den*time_base.num; 379 359 380 sp = &sync[i]; 360 381 min_distance = INT64_MAX; 361 382 // Find timestamp closest to requested timestamp within min/max limits. … … int64_t ff_gen_syncpoint_search(AVFormatContext *s, 363 384 && av_compare_ts(ts_min, time_base, sp->ts_lo, st->time_base) <= 0 364 385 && av_compare_ts(sp->ts_lo, st->time_base, ts_max, time_base) <= 0) { 365 386 // low timestamp is in range 366 min_distance = ts _distance(ts, time_base, sp->ts_lo, st->time_base);387 min_distance = ts - av_rescale(sp->ts_lo, ts_num, ts_den); //ts_distance(ts, time_base, sp->ts_lo, st->time_base); 367 388 min_pos = sp->pos_lo; 389 cur_dts = sp->ts_lo; 368 390 } 369 391 if (sp->pos_hi != INT64_MAX 370 392 && av_compare_ts(ts_min, time_base, sp->ts_hi, st->time_base) <= 0 371 393 && av_compare_ts(sp->ts_hi, st->time_base, ts_max, time_base) <= 0) { 372 394 // high timestamp is in range, check distance 373 distance = ts_distance(sp->ts_hi, st->time_base, ts, time_base);395 distance = av_rescale(sp->ts_hi, ts_num, ts_den) - ts; 374 396 if (distance < min_distance) { 375 397 min_distance = distance; 376 398 min_pos = sp->pos_hi; 399 cur_dts = sp->ts_hi; 377 400 } 378 401 } 402 av_log(s, AV_LOG_DEBUG, "seek2 st:%d dist=%lld pos=%lld dts=%lld\n", i, min_distance, min_pos, cur_dts); 379 403 if (min_distance == INT64_MAX) { 380 404 // no timestamp is in range, cannot seek 381 405 av_free(sync); 382 406 return -1; 383 407 } 384 if (min_pos < pos) 408 if (min_distance < min_distance2) { 409 min_distance2 = min_distance; 385 410 pos = min_pos; 411 st->cur_dts = cur_dts; 412 } 386 413 } 387 414 } 388 415 389 416 avio_seek(s->pb, pos, SEEK_SET); 390 417 av_free(sync); 418 av_log(s, AV_LOG_DEBUG, "seek2 pos=%lld\n", pos); 391 419 return pos; 392 420 } 393 421 -
libavformat/utils.c
diff --git a/libavformat/utils.c b/libavformat/utils.c index a78da36..2d8ff75 100644
a b static int read_frame_internal(AVFormatContext *s, AVPacket *pkt) 1409 1409 cur_pkt.duration, 1410 1410 cur_pkt.flags); 1411 1411 1412 if (st->need_parsing && !st->parser && !(s->flags & AVFMT_FLAG_NOPARSE) ) {1412 if (st->need_parsing && !st->parser && !(s->flags & AVFMT_FLAG_NOPARSE)&& st->codec->codec_id != CODEC_ID_PROBE) { 1413 1413 st->parser = av_parser_init(st->codec->codec_id); 1414 1414 if (!st->parser) { 1415 1415 av_log(s, AV_LOG_VERBOSE, "parser not found for codec " … … int64_t ff_gen_search(AVFormatContext *s, int stream_index, int64_t target_ts, 1846 1846 } 1847 1847 1848 1848 if(ts_max == AV_NOPTS_VALUE){ 1849 int step= 1024;1849 int64_t step= 1024; 1850 1850 filesize = avio_size(s->pb); 1851 1851 pos_max = filesize - 1; 1852 1852 do{ 1853 1853 pos_max -= step; 1854 1854 ts_max = ff_read_timestamp(s, stream_index, &pos_max, pos_max + step, read_timestamp); 1855 step += step; 1856 }while(ts_max == AV_NOPTS_VALUE && pos_max >= step); 1855 #ifdef DEBUG_SEEK 1856 av_log(s, AV_LOG_DEBUG, "ts_min=0x%llx ts_max=0x%llx\n", 1857 ts_min, ts_max); 1858 #endif 1859 1860 if (ts_max == AV_NOPTS_VALUE || ts_max < target_ts) 1861 step += step; 1862 else { 1863 if (ts_max < ts_min) { /* wraparound */ 1864 #ifdef DEBUG_SEEK 1865 av_log(s, AV_LOG_DEBUG, "wraparound: target=%lld ts_min=%lld ts_max=%lld\n", 1866 target_ts, ts_min, ts_max); 1867 #endif 1868 /* try to adjust pos_min until ts_min<=target_ts */ 1869 step = pos_max - pos_min; 1870 while (step > 0) { 1871 step >>= 1; 1872 pos= pos_min + step; 1873 ts= ff_read_timestamp(s, stream_index, &pos, pos_max, read_timestamp); 1874 #ifdef DEBUG_SEEK 1875 av_log(s, AV_LOG_DEBUG, "wraparound2: pos=%lld step=%lld ts=%lld\n", pos, step, ts); 1876 #endif 1877 if (ts != AV_NOPTS_VALUE) { 1878 if (ts <= target_ts ) { 1879 pos_min = pos; 1880 ts_min = ts; 1881 break; 1882 } else if (ts_max < ts) { 1883 pos_min = pos; 1884 step = pos_max - pos_min; 1885 } 1886 } 1887 } 1888 } 1889 break; 1890 } 1891 } while (pos_max >= step); 1857 1892 if (ts_max == AV_NOPTS_VALUE) 1858 1893 return -1; 1859 1894 1860 1895 for(;;){ 1861 1896 int64_t tmp_pos= pos_max + 1; 1862 1897 int64_t tmp_ts= ff_read_timestamp(s, stream_index, &tmp_pos, INT64_MAX, read_timestamp); 1863 if(tmp_ts == AV_NOPTS_VALUE )1898 if(tmp_ts == AV_NOPTS_VALUE || tmp_ts < ts_max) 1864 1899 break; 1865 1900 ts_max= tmp_ts; 1866 1901 pos_max= tmp_pos; … … int64_t ff_gen_search(AVFormatContext *s, int stream_index, int64_t target_ts, 1881 1916 pos_limit= pos_min; 1882 1917 } 1883 1918 1919 #ifdef DEBUG_SEEK 1920 av_log(s, AV_LOG_DEBUG, "ts_min=0x%llx ts_max=0x%llx pos_limit=%lld\n", 1921 ts_min, ts_max, pos_limit); 1922 #endif 1923 1884 1924 no_change=0; 1885 1925 while (pos_min < pos_limit) { 1886 1926 av_dlog(s, "pos_min=0x%"PRIx64" pos_max=0x%"PRIx64" dts_min=%s dts_max=%s\n", … … static int seek_frame_generic(AVFormatContext *s, 2026 2066 static int seek_frame_internal(AVFormatContext *s, int stream_index, 2027 2067 int64_t timestamp, int flags) 2028 2068 { 2029 int ret;2069 int i, ret; 2030 2070 AVStream *st; 2031 2071 2032 2072 if (flags & AVSEEK_FLAG_BYTE) { … … static int seek_frame_internal(AVFormatContext *s, int stream_index, 2052 2092 ret = s->iformat->read_seek(s, stream_index, timestamp, flags); 2053 2093 } else 2054 2094 ret = -1; 2055 if (ret >= 0) { 2056 return 0; 2057 } 2058 2059 if (s->iformat->read_timestamp && !(s->iformat->flags & AVFMT_NOBINSEARCH)) { 2095 if (ret >= 0) 2096 ; 2097 else if (s->iformat->read_timestamp && !(s->iformat->flags & AVFMT_NOBINSEARCH)) { 2060 2098 ff_read_frame_flush(s); 2061 ret urnff_seek_frame_binary(s, stream_index, timestamp, flags);2099 ret = ff_seek_frame_binary(s, stream_index, timestamp, flags); 2062 2100 } else if (!(s->iformat->flags & AVFMT_NOGENSEARCH)) { 2063 2101 ff_read_frame_flush(s); 2064 return seek_frame_generic(s, stream_index, timestamp, flags); 2102 ret = seek_frame_generic(s, stream_index, timestamp, flags); 2103 } else 2104 ret = -1; 2105 if (ret >= 0) { 2106 for(i=0; i<s->nb_streams; i++) { 2107 st = s->streams[i]; 2108 st->first_dts = st->cur_dts; 2109 av_log(s, AV_LOG_DEBUG, "after seek st:%d cur:%lld\n", i, st->cur_dts); 2110 } 2111 return 0; 2065 2112 } 2066 else 2067 return -1; 2113 return ret; 2068 2114 } 2069 2115 2070 2116 int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp, int flags) … … int avformat_seek_file(AVFormatContext *s, int stream_index, int64_t min_ts, int 2112 2158 // try some generic seek like seek_frame_generic() but with new ts semantics 2113 2159 } 2114 2160 2161 /** 2162 * Obtain the current dts in AV_TIME_BASE units. 2163 * @param stream_index If stream_index is (-1), a default 2164 * stream is selected. 2165 */ 2166 int64_t av_get_cur_dts(AVFormatContext *s, int stream_index) 2167 { 2168 AVStream *st; 2169 2170 if (stream_index < 0){ 2171 stream_index= av_find_default_stream_index(s); 2172 if(stream_index < 0) 2173 return AV_NOPTS_VALUE; 2174 } 2175 st= s->streams[stream_index]; 2176 if (st->cur_dts == AV_NOPTS_VALUE || is_relative(st->cur_dts)) 2177 return AV_NOPTS_VALUE; 2178 return av_rescale(st->cur_dts, AV_TIME_BASE * (int64_t)st->time_base.num, 2179 st->time_base.den); 2180 } 2181 2115 2182 /*******************************************************/ 2116 2183 2117 2184 /** … … static int has_codec_parameters(AVStream *st, const char **errmsg_ptr) 2405 2472 FAIL("no decodable DTS frames"); 2406 2473 break; 2407 2474 case AVMEDIA_TYPE_VIDEO: 2475 if (avctx->codec_id == CODEC_ID_MPEG2VIDEO && 2476 avctx->sample_aspect_ratio.num == 0) 2477 FAIL("mpeg2v unspecified aspect"); 2408 2478 if (!avctx->width) 2409 2479 FAIL("unspecified size"); 2410 2480 if (st->info->found_decoder >= 0 && avctx->pix_fmt == AV_PIX_FMT_NONE) … … int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options) 2658 2728 st->time_base= */ 2659 2729 if(!st->codec->time_base.num) 2660 2730 st->codec->time_base= st->time_base; 2731 if (st->codec->codec_id == CODEC_ID_MPEG2VIDEO) 2732 // aspect ratio may change; re-probe it 2733 st->codec->sample_aspect_ratio = (AVRational){0,1}; 2661 2734 } 2662 2735 //only for the split stuff 2663 2736 if (!st->parser && !(ic->flags & AVFMT_FLAG_NOPARSE)) { … … int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options) 3042 3115 for (i=0; i < ic->nb_streams; i++) { 3043 3116 if (ic->streams[i]->codec) 3044 3117 ic->streams[i]->codec->thread_count = 0; 3045 av_freep(&ic->streams[i]->info);3118 memset(ic->streams[i]->info, 0, sizeof *ic->streams[i]->info); 3046 3119 } 3047 3120 if(ic->pb) 3048 3121 av_log(ic, AV_LOG_DEBUG, "File position after avformat_find_stream_info() is %"PRId64"\n", avio_tell(ic->pb));
