avpacket: Deprecate av_dup_packet
[libav.git] / libavformat / mux.c
1 /*
2 * muxing functions for use within Libav
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4 *
5 * This file is part of Libav.
6 *
7 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "avformat.h"
23 #include "avio_internal.h"
24 #include "internal.h"
25 #include "libavcodec/internal.h"
26 #include "libavcodec/bytestream.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/dict.h"
29 #include "libavutil/pixdesc.h"
30 #include "metadata.h"
31 #include "id3v2.h"
32 #include "libavutil/avassert.h"
33 #include "libavutil/avstring.h"
34 #include "libavutil/internal.h"
35 #include "libavutil/mathematics.h"
36 #include "libavutil/parseutils.h"
37 #include "libavutil/time.h"
38 #include "riff.h"
39 #include "audiointerleave.h"
40 #include "url.h"
41 #include <stdarg.h>
42 #if CONFIG_NETWORK
43 #include "network.h"
44 #endif
45
46 #undef NDEBUG
47 #include <assert.h>
48
49 /**
50 * @file
51 * muxing functions for use within Libav
52 */
53
54 static int validate_codec_tag(AVFormatContext *s, AVStream *st)
55 {
56 const AVCodecTag *avctag;
57 int n;
58 enum AVCodecID id = AV_CODEC_ID_NONE;
59 unsigned int tag = 0;
60
61 /**
62 * Check that tag + id is in the table
63 * If neither is in the table -> OK
64 * If tag is in the table with another id -> FAIL
65 * If id is in the table with another tag -> FAIL unless strict < normal
66 */
67 for (n = 0; s->oformat->codec_tag[n]; n++) {
68 avctag = s->oformat->codec_tag[n];
69 while (avctag->id != AV_CODEC_ID_NONE) {
70 if (avpriv_toupper4(avctag->tag) == avpriv_toupper4(st->codec->codec_tag)) {
71 id = avctag->id;
72 if (id == st->codec->codec_id)
73 return 1;
74 }
75 if (avctag->id == st->codec->codec_id)
76 tag = avctag->tag;
77 avctag++;
78 }
79 }
80 if (id != AV_CODEC_ID_NONE)
81 return 0;
82 if (tag && (s->strict_std_compliance >= FF_COMPLIANCE_NORMAL))
83 return 0;
84 return 1;
85 }
86
87
88 static int init_muxer(AVFormatContext *s, AVDictionary **options)
89 {
90 int ret = 0, i;
91 AVStream *st;
92 AVDictionary *tmp = NULL;
93 AVCodecContext *codec = NULL;
94 AVOutputFormat *of = s->oformat;
95
96 if (options)
97 av_dict_copy(&tmp, *options, 0);
98
99 if ((ret = av_opt_set_dict(s, &tmp)) < 0)
100 goto fail;
101
102 #if FF_API_LAVF_BITEXACT
103 if (s->nb_streams && s->streams[0]->codec->flags & AV_CODEC_FLAG_BITEXACT)
104 s->flags |= AVFMT_FLAG_BITEXACT;
105 #endif
106
107 // some sanity checks
108 if (s->nb_streams == 0 && !(of->flags & AVFMT_NOSTREAMS)) {
109 av_log(s, AV_LOG_ERROR, "no streams\n");
110 ret = AVERROR(EINVAL);
111 goto fail;
112 }
113
114 for (i = 0; i < s->nb_streams; i++) {
115 st = s->streams[i];
116 codec = st->codec;
117
118 #if FF_API_LAVF_CODEC_TB
119 FF_DISABLE_DEPRECATION_WARNINGS
120 if (!st->time_base.num && codec->time_base.num) {
121 av_log(s, AV_LOG_WARNING, "Using AVStream.codec.time_base as a "
122 "timebase hint to the muxer is deprecated. Set "
123 "AVStream.time_base instead.\n");
124 avpriv_set_pts_info(st, 64, codec->time_base.num, codec->time_base.den);
125 }
126 FF_ENABLE_DEPRECATION_WARNINGS
127 #endif
128
129 if (!st->time_base.num) {
130 /* fall back on the default timebase values */
131 if (codec->codec_type == AVMEDIA_TYPE_AUDIO && codec->sample_rate)
132 avpriv_set_pts_info(st, 64, 1, codec->sample_rate);
133 else
134 avpriv_set_pts_info(st, 33, 1, 90000);
135 }
136
137 switch (codec->codec_type) {
138 case AVMEDIA_TYPE_AUDIO:
139 if (codec->sample_rate <= 0) {
140 av_log(s, AV_LOG_ERROR, "sample rate not set\n");
141 ret = AVERROR(EINVAL);
142 goto fail;
143 }
144 if (!codec->block_align)
145 codec->block_align = codec->channels *
146 av_get_bits_per_sample(codec->codec_id) >> 3;
147 break;
148 case AVMEDIA_TYPE_VIDEO:
149 if ((codec->width <= 0 || codec->height <= 0) &&
150 !(of->flags & AVFMT_NODIMENSIONS)) {
151 av_log(s, AV_LOG_ERROR, "dimensions not set\n");
152 ret = AVERROR(EINVAL);
153 goto fail;
154 }
155
156 if (av_cmp_q(st->sample_aspect_ratio,
157 codec->sample_aspect_ratio)) {
158 if (st->sample_aspect_ratio.num != 0 &&
159 st->sample_aspect_ratio.den != 0 &&
160 codec->sample_aspect_ratio.den != 0 &&
161 codec->sample_aspect_ratio.den != 0) {
162 av_log(s, AV_LOG_ERROR, "Aspect ratio mismatch between muxer "
163 "(%d/%d) and encoder layer (%d/%d)\n",
164 st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
165 codec->sample_aspect_ratio.num,
166 codec->sample_aspect_ratio.den);
167 ret = AVERROR(EINVAL);
168 goto fail;
169 }
170 }
171 break;
172 }
173
174 if (of->codec_tag) {
175 if (codec->codec_tag &&
176 codec->codec_id == AV_CODEC_ID_RAWVIDEO &&
177 !av_codec_get_tag(of->codec_tag, codec->codec_id) &&
178 !validate_codec_tag(s, st)) {
179 // the current rawvideo encoding system ends up setting
180 // the wrong codec_tag for avi, we override it here
181 codec->codec_tag = 0;
182 }
183 if (codec->codec_tag) {
184 if (!validate_codec_tag(s, st)) {
185 char tagbuf[32];
186 av_get_codec_tag_string(tagbuf, sizeof(tagbuf), codec->codec_tag);
187 av_log(s, AV_LOG_ERROR,
188 "Tag %s/0x%08x incompatible with output codec id '%d'\n",
189 tagbuf, codec->codec_tag, codec->codec_id);
190 ret = AVERROR_INVALIDDATA;
191 goto fail;
192 }
193 } else
194 codec->codec_tag = av_codec_get_tag(of->codec_tag, codec->codec_id);
195 }
196
197 if (of->flags & AVFMT_GLOBALHEADER &&
198 !(codec->flags & AV_CODEC_FLAG_GLOBAL_HEADER))
199 av_log(s, AV_LOG_WARNING,
200 "Codec for stream %d does not use global headers "
201 "but container format requires global headers\n", i);
202
203 if (codec->codec_type != AVMEDIA_TYPE_ATTACHMENT)
204 s->internal->nb_interleaved_streams++;
205 }
206
207 if (!s->priv_data && of->priv_data_size > 0) {
208 s->priv_data = av_mallocz(of->priv_data_size);
209 if (!s->priv_data) {
210 ret = AVERROR(ENOMEM);
211 goto fail;
212 }
213 if (of->priv_class) {
214 *(const AVClass **)s->priv_data = of->priv_class;
215 av_opt_set_defaults(s->priv_data);
216 if ((ret = av_opt_set_dict(s->priv_data, &tmp)) < 0)
217 goto fail;
218 }
219 }
220
221 /* set muxer identification string */
222 if (!(s->flags & AVFMT_FLAG_BITEXACT)) {
223 av_dict_set(&s->metadata, "encoder", LIBAVFORMAT_IDENT, 0);
224 }
225
226 if (options) {
227 av_dict_free(options);
228 *options = tmp;
229 }
230
231 return 0;
232
233 fail:
234 av_dict_free(&tmp);
235 return ret;
236 }
237
238 int avformat_write_header(AVFormatContext *s, AVDictionary **options)
239 {
240 int ret = 0;
241
242 if (ret = init_muxer(s, options))
243 return ret;
244
245 if (s->oformat->write_header) {
246 ret = s->oformat->write_header(s);
247 if (ret < 0)
248 return ret;
249 }
250
251 if (s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_AUTO) {
252 if (s->oformat->flags & (AVFMT_TS_NEGATIVE | AVFMT_NOTIMESTAMPS)) {
253 s->avoid_negative_ts = 0;
254 } else
255 s->avoid_negative_ts = AVFMT_AVOID_NEG_TS_MAKE_NON_NEGATIVE;
256 }
257
258 return 0;
259 }
260
261 //FIXME merge with compute_pkt_fields
262 static int compute_pkt_fields2(AVFormatContext *s, AVStream *st, AVPacket *pkt)
263 {
264 int delay = FFMAX(st->codec->has_b_frames, !!st->codec->max_b_frames);
265 int num, den, i;
266
267 av_log(s, AV_LOG_TRACE, "compute_pkt_fields2: pts:%" PRId64 " dts:%" PRId64 " cur_dts:%" PRId64 " b:%d size:%d st:%d\n",
268 pkt->pts, pkt->dts, st->cur_dts, delay, pkt->size, pkt->stream_index);
269
270 /* if(pkt->pts == AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE)
271 * return AVERROR(EINVAL);*/
272
273 /* duration field */
274 if (pkt->duration == 0) {
275 ff_compute_frame_duration(s, &num, &den, st, NULL, pkt);
276 if (den && num) {
277 pkt->duration = av_rescale(1, num * (int64_t)st->time_base.den * st->codec->ticks_per_frame, den * (int64_t)st->time_base.num);
278 }
279 }
280
281 if (pkt->pts == AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE && delay == 0)
282 pkt->pts = pkt->dts;
283
284 //calculate dts from pts
285 if (pkt->pts != AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) {
286 st->pts_buffer[0] = pkt->pts;
287 for (i = 1; i < delay + 1 && st->pts_buffer[i] == AV_NOPTS_VALUE; i++)
288 st->pts_buffer[i] = pkt->pts + (i - delay - 1) * pkt->duration;
289 for (i = 0; i<delay && st->pts_buffer[i] > st->pts_buffer[i + 1]; i++)
290 FFSWAP(int64_t, st->pts_buffer[i], st->pts_buffer[i + 1]);
291
292 pkt->dts = st->pts_buffer[0];
293 }
294
295 if (st->cur_dts && st->cur_dts != AV_NOPTS_VALUE &&
296 ((!(s->oformat->flags & AVFMT_TS_NONSTRICT) &&
297 st->cur_dts >= pkt->dts) || st->cur_dts > pkt->dts)) {
298 av_log(s, AV_LOG_ERROR,
299 "Application provided invalid, non monotonically increasing dts to muxer in stream %d: %" PRId64 " >= %" PRId64 "\n",
300 st->index, st->cur_dts, pkt->dts);
301 return AVERROR(EINVAL);
302 }
303 if (pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts < pkt->dts) {
304 av_log(s, AV_LOG_ERROR,
305 "pts %" PRId64 " < dts %" PRId64 " in stream %d\n",
306 pkt->pts, pkt->dts,
307 st->index);
308 return AVERROR(EINVAL);
309 }
310
311 av_log(s, AV_LOG_TRACE, "av_write_frame: pts2:%"PRId64" dts2:%"PRId64"\n",
312 pkt->pts, pkt->dts);
313 st->cur_dts = pkt->dts;
314
315 return 0;
316 }
317
318 /*
319 * FIXME: this function should NEVER get undefined pts/dts beside when the
320 * AVFMT_NOTIMESTAMPS is set.
321 * Those additional safety checks should be dropped once the correct checks
322 * are set in the callers.
323 */
324
325 static int write_packet(AVFormatContext *s, AVPacket *pkt)
326 {
327 int ret;
328 if (s->avoid_negative_ts > 0) {
329 AVRational time_base = s->streams[pkt->stream_index]->time_base;
330 int64_t offset = 0;
331
332 if (s->internal->offset == AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE &&
333 (pkt->dts < 0 || s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_MAKE_ZERO)) {
334 s->internal->offset = -pkt->dts;
335 s->internal->offset_timebase = time_base;
336 }
337 if (s->internal->offset != AV_NOPTS_VALUE)
338 offset = av_rescale_q(s->internal->offset, s->internal->offset_timebase, time_base);
339
340 if (pkt->dts != AV_NOPTS_VALUE)
341 pkt->dts += offset;
342 if (pkt->pts != AV_NOPTS_VALUE)
343 pkt->pts += offset;
344
345 if (pkt->dts != AV_NOPTS_VALUE && pkt->dts < 0) {
346 av_log(s, AV_LOG_WARNING,
347 "Packets poorly interleaved, failed to avoid negative "
348 "timestamp %"PRId64" in stream %d.\n"
349 "Try -max_interleave_delta 0 as a possible workaround.\n",
350 pkt->dts, pkt->stream_index);
351 }
352 }
353 ret = s->oformat->write_packet(s, pkt);
354
355 if (s->pb && ret >= 0) {
356 if (s->flags & AVFMT_FLAG_FLUSH_PACKETS)
357 avio_flush(s->pb);
358 if (s->pb->error < 0)
359 ret = s->pb->error;
360 }
361
362 return ret;
363 }
364
365 static int check_packet(AVFormatContext *s, AVPacket *pkt)
366 {
367 if (!pkt)
368 return 0;
369
370 if (pkt->stream_index < 0 || pkt->stream_index >= s->nb_streams) {
371 av_log(s, AV_LOG_ERROR, "Invalid packet stream index: %d\n",
372 pkt->stream_index);
373 return AVERROR(EINVAL);
374 }
375
376 if (s->streams[pkt->stream_index]->codec->codec_type == AVMEDIA_TYPE_ATTACHMENT) {
377 av_log(s, AV_LOG_ERROR, "Received a packet for an attachment stream.\n");
378 return AVERROR(EINVAL);
379 }
380
381 return 0;
382 }
383
384 int av_write_frame(AVFormatContext *s, AVPacket *pkt)
385 {
386 int ret;
387
388 ret = check_packet(s, pkt);
389 if (ret < 0)
390 return ret;
391
392 if (!pkt) {
393 if (s->oformat->flags & AVFMT_ALLOW_FLUSH)
394 return s->oformat->write_packet(s, pkt);
395 return 1;
396 }
397
398 ret = compute_pkt_fields2(s, s->streams[pkt->stream_index], pkt);
399
400 if (ret < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
401 return ret;
402
403 ret = write_packet(s, pkt);
404
405 if (ret >= 0)
406 s->streams[pkt->stream_index]->nb_frames++;
407 return ret;
408 }
409
410 int ff_interleave_add_packet(AVFormatContext *s, AVPacket *pkt,
411 int (*compare)(AVFormatContext *, AVPacket *, AVPacket *))
412 {
413 int ret;
414 AVPacketList **next_point, *this_pktl;
415
416 this_pktl = av_mallocz(sizeof(AVPacketList));
417 if (!this_pktl)
418 return AVERROR(ENOMEM);
419
420 if ((ret = av_packet_ref(&this_pktl->pkt, pkt)) < 0) {
421 av_free(this_pktl);
422 return ret;
423 }
424
425 if (s->streams[pkt->stream_index]->last_in_packet_buffer) {
426 next_point = &(s->streams[pkt->stream_index]->last_in_packet_buffer->next);
427 } else
428 next_point = &s->internal->packet_buffer;
429
430 if (*next_point) {
431 if (compare(s, &s->internal->packet_buffer_end->pkt, pkt)) {
432 while (!compare(s, &(*next_point)->pkt, pkt))
433 next_point = &(*next_point)->next;
434 goto next_non_null;
435 } else {
436 next_point = &(s->internal->packet_buffer_end->next);
437 }
438 }
439 assert(!*next_point);
440
441 s->internal->packet_buffer_end = this_pktl;
442 next_non_null:
443
444 this_pktl->next = *next_point;
445
446 s->streams[pkt->stream_index]->last_in_packet_buffer =
447 *next_point = this_pktl;
448
449 av_packet_unref(pkt);
450
451 return 0;
452 }
453
454 static int interleave_compare_dts(AVFormatContext *s, AVPacket *next,
455 AVPacket *pkt)
456 {
457 AVStream *st = s->streams[pkt->stream_index];
458 AVStream *st2 = s->streams[next->stream_index];
459 int comp = av_compare_ts(next->dts, st2->time_base, pkt->dts,
460 st->time_base);
461
462 if (comp == 0)
463 return pkt->stream_index < next->stream_index;
464 return comp > 0;
465 }
466
467 int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
468 AVPacket *pkt, int flush)
469 {
470 AVPacketList *pktl;
471 int stream_count = 0;
472 int i, ret;
473
474 if (pkt) {
475 if ((ret = ff_interleave_add_packet(s, pkt, interleave_compare_dts)) < 0)
476 return ret;
477 }
478
479 if (s->max_interleave_delta > 0 && s->internal->packet_buffer && !flush) {
480 AVPacket *top_pkt = &s->internal->packet_buffer->pkt;
481 int64_t delta_dts = INT64_MIN;
482 int64_t top_dts = av_rescale_q(top_pkt->dts,
483 s->streams[top_pkt->stream_index]->time_base,
484 AV_TIME_BASE_Q);
485
486 for (i = 0; i < s->nb_streams; i++) {
487 int64_t last_dts;
488 const AVPacketList *last = s->streams[i]->last_in_packet_buffer;
489
490 if (!last)
491 continue;
492
493 last_dts = av_rescale_q(last->pkt.dts,
494 s->streams[i]->time_base,
495 AV_TIME_BASE_Q);
496 delta_dts = FFMAX(delta_dts, last_dts - top_dts);
497 stream_count++;
498 }
499
500 if (delta_dts > s->max_interleave_delta) {
501 av_log(s, AV_LOG_DEBUG,
502 "Delay between the first packet and last packet in the "
503 "muxing queue is %"PRId64" > %"PRId64": forcing output\n",
504 delta_dts, s->max_interleave_delta);
505 flush = 1;
506 }
507 } else {
508 for (i = 0; i < s->nb_streams; i++)
509 stream_count += !!s->streams[i]->last_in_packet_buffer;
510 }
511
512
513 if (stream_count && (s->internal->nb_interleaved_streams == stream_count || flush)) {
514 pktl = s->internal->packet_buffer;
515 *out = pktl->pkt;
516
517 s->internal->packet_buffer = pktl->next;
518 if (!s->internal->packet_buffer)
519 s->internal->packet_buffer_end = NULL;
520
521 if (s->streams[out->stream_index]->last_in_packet_buffer == pktl)
522 s->streams[out->stream_index]->last_in_packet_buffer = NULL;
523 av_freep(&pktl);
524 return 1;
525 } else {
526 av_init_packet(out);
527 return 0;
528 }
529 }
530
531 /**
532 * Interleave an AVPacket correctly so it can be muxed.
533 * @param out the interleaved packet will be output here
534 * @param in the input packet
535 * @param flush 1 if no further packets are available as input and all
536 * remaining packets should be output
537 * @return 1 if a packet was output, 0 if no packet could be output,
538 * < 0 if an error occurred
539 */
540 static int interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *in, int flush)
541 {
542 if (s->oformat->interleave_packet) {
543 int ret = s->oformat->interleave_packet(s, out, in, flush);
544 if (in)
545 av_packet_unref(in);
546 return ret;
547 } else
548 return ff_interleave_packet_per_dts(s, out, in, flush);
549 }
550
551 int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
552 {
553 int ret, flush = 0;
554
555 ret = check_packet(s, pkt);
556 if (ret < 0)
557 goto fail;
558
559 if (pkt) {
560 AVStream *st = s->streams[pkt->stream_index];
561
562 av_log(s, AV_LOG_TRACE, "av_interleaved_write_frame size:%d dts:%" PRId64 " pts:%" PRId64 "\n",
563 pkt->size, pkt->dts, pkt->pts);
564 if ((ret = compute_pkt_fields2(s, st, pkt)) < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
565 goto fail;
566
567 if (pkt->dts == AV_NOPTS_VALUE && !(s->oformat->flags & AVFMT_NOTIMESTAMPS)) {
568 ret = AVERROR(EINVAL);
569 goto fail;
570 }
571 } else {
572 av_log(s, AV_LOG_TRACE, "av_interleaved_write_frame FLUSH\n");
573 flush = 1;
574 }
575
576 for (;; ) {
577 AVPacket opkt;
578 int ret = interleave_packet(s, &opkt, pkt, flush);
579 if (pkt) {
580 memset(pkt, 0, sizeof(*pkt));
581 av_init_packet(pkt);
582 pkt = NULL;
583 }
584 if (ret <= 0) //FIXME cleanup needed for ret<0 ?
585 return ret;
586
587 ret = write_packet(s, &opkt);
588 if (ret >= 0)
589 s->streams[opkt.stream_index]->nb_frames++;
590
591 av_packet_unref(&opkt);
592
593 if (ret < 0)
594 return ret;
595 }
596 fail:
597 av_packet_unref(pkt);
598 return ret;
599 }
600
601 int av_write_trailer(AVFormatContext *s)
602 {
603 int ret, i;
604
605 for (;; ) {
606 AVPacket pkt;
607 ret = interleave_packet(s, &pkt, NULL, 1);
608 if (ret < 0) //FIXME cleanup needed for ret<0 ?
609 goto fail;
610 if (!ret)
611 break;
612
613 ret = write_packet(s, &pkt);
614 if (ret >= 0)
615 s->streams[pkt.stream_index]->nb_frames++;
616
617 av_packet_unref(&pkt);
618
619 if (ret < 0)
620 goto fail;
621 }
622
623 if (s->oformat->write_trailer)
624 ret = s->oformat->write_trailer(s);
625
626 if (!(s->oformat->flags & AVFMT_NOFILE) && s->pb)
627 avio_flush(s->pb);
628
629 fail:
630 for (i = 0; i < s->nb_streams; i++) {
631 av_freep(&s->streams[i]->priv_data);
632 av_freep(&s->streams[i]->index_entries);
633 }
634 if (s->oformat->priv_class)
635 av_opt_free(s->priv_data);
636 av_freep(&s->priv_data);
637 return ret;
638 }
639
640 int ff_write_chained(AVFormatContext *dst, int dst_stream, AVPacket *pkt,
641 AVFormatContext *src)
642 {
643 AVPacket local_pkt;
644
645 local_pkt = *pkt;
646 local_pkt.stream_index = dst_stream;
647 if (pkt->pts != AV_NOPTS_VALUE)
648 local_pkt.pts = av_rescale_q(pkt->pts,
649 src->streams[pkt->stream_index]->time_base,
650 dst->streams[dst_stream]->time_base);
651 if (pkt->dts != AV_NOPTS_VALUE)
652 local_pkt.dts = av_rescale_q(pkt->dts,
653 src->streams[pkt->stream_index]->time_base,
654 dst->streams[dst_stream]->time_base);
655 return av_write_frame(dst, &local_pkt);
656 }