1 /*
2 * muxing functions for use within FFmpeg
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
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
42 #include <stdarg.h>
43 #if CONFIG_NETWORK
45 #endif
46
47 #undef NDEBUG
48 #include <assert.h>
49
50 /**
51 * @file
52 * muxing functions for use within libavformat
53 */
54
55 /* fraction handling */
56
57 /**
58 * f = val + (num / den) + 0.5.
59 *
60 * 'num' is normalized so that it is such as 0 <= num < den.
61 *
62 * @param f fractional number
63 * @param val integer value
64 * @param num must be >= 0
65 * @param den must be >= 1
66 */
68 {
69 num += (den >> 1);
70 if (num >= den) {
71 val += num / den;
72 num = num % den;
73 }
77 }
78
79 /**
80 * Fractional addition to f: f = f + (incr / f->den).
81 *
82 * @param f fractional number
83 * @param incr increment, can be positive or negative
84 */
86 {
87 int64_t num, den;
88
91 if (num < 0) {
93 num = num % den;
94 if (num < 0) {
95 num += den;
97 }
98 } else if (num >= den) {
100 num = num % den;
101 }
103 }
104
106 {
108 int j;
109
112 } else {
114 }
115 for (j=2; j<14; j+= 1+(j>2))
116 while (q.
den / q.
num < min_precission && q.
num % j == 0)
118 while (q.
den / q.
num < min_precission && q.
den < (1<<24))
120
121 return q;
122 }
123
125 const char *format, const char *filename)
126 {
129
130 *avctx = NULL;
131 if (!s)
132 goto nomem;
133
134 if (!oformat) {
135 if (format) {
137 if (!oformat) {
138 av_log(s,
AV_LOG_ERROR,
"Requested output format '%s' is not a suitable output format\n", format);
140 goto error;
141 }
142 } else {
144 if (!oformat) {
147 filename);
148 goto error;
149 }
150 }
151 }
152
157 goto nomem;
161 }
162 } else
164
165 if (filename)
168 return 0;
169 nomem:
172 error:
175 }
176
177 #if FF_API_ALLOC_OUTPUT_CONTEXT
180 {
183 return ret < 0 ? NULL : avctx;
184 }
185 #endif
186
188 {
192 unsigned int tag = 0;
193
194 /**
195 * Check that tag + id is in the table
196 * If neither is in the table -> OK
197 * If tag is in the table with another id -> FAIL
198 * If id is in the table with another tag -> FAIL unless strict < normal
199 */
206 return 1;
207 }
210 avctag++;
211 }
212 }
214 return 0;
216 return 0;
217 return 1;
218 }
219
220
222 {
228
229 if (options)
231
233 goto fail;
236 goto fail;
237
238 // some sanity checks
242 goto fail;
243 }
244
248
254 goto fail;
255 }
259 break;
265 goto fail;
266 }
267
272 goto fail;
273 }
276 ) {
282 "(%d/%d) and encoder layer (%d/%d)\n",
287 goto fail;
288 }
289 }
290 break;
291 }
292
299 // the current rawvideo encoding system ends up setting
300 // the wrong codec_tag for avi/mov, we override it here
302 }
305 char tagbuf[32], tagbuf2[32];
309 "Tag %s/0x%08x incompatible with output codec id '%d' (%s)\n",
312 goto fail;
313 }
314 } else
316 }
317
321 "Codec for stream %d does not use global headers "
322 "but container format requires global headers\n", i);
323 }
324
329 goto fail;
330 }
335 goto fail;
336 }
337 }
338
339 /* set muxer identification string */
342 }
343
344 if (options) {
346 *options = tmp;
347 }
348
349 return 0;
350
351 fail:
354 }
355
357 {
358 int i;
360
361 /* init PTS generation */
365
369 break;
372 break;
373 default:
374 break;
375 }
377 if (den <= 0)
379
381 }
382 }
383
384 return 0;
385 }
386
388 {
390
393
396 if (ret >= 0 && s->
pb && s->
pb->
error < 0)
398 if (ret < 0)
400 }
401
404
408 } else
410 }
411
412 return 0;
413 }
414
415 //FIXME merge with compute_pkt_fields
417 {
420
421 av_dlog(s,
"compute_pkt_fields2: pts:%s dts:%s cur_dts:%s b:%d size:%d st:%d\n",
423
424 /* duration field */
427 if (den && num) {
429 }
430 }
431
434
435 //XXX/FIXME this is a temporary hack until all encoders output pts
437 static int warned;
438 if (!warned) {
440 warned = 1;
441 }
443 // pkt->pts= st->cur_dts;
445 }
446
447 //calculate dts from pts
454
456 }
457
462 "Application provided invalid, non monotonically increasing dts to muxer in stream %d: %s >= %s\n",
465 }
470 }
471
472 av_dlog(s,
"av_write_frame: pts2:%s dts2:%s\n",
476
477 /* update pts */
481
482 /* HACK/FIXME, we skip the initial 0 size packets as they are most
483 * likely equal to the encoder delay, but it would be better if we
484 * had the real timestamps from the encoder */
487 }
488 break;
491 break;
492 default:
493 break;
494 }
495 return 0;
496 }
497
498 /**
499 * Make timestamps non negative, move side data from payload to internal struct, call muxer, and restore
500 * sidedata.
501 *
502 * FIXME: this function should NEVER get undefined pts/dts beside when the
503 * AVFMT_NOTIMESTAMPS is set.
504 * Those additional safety checks should be dropped once the correct checks
505 * are set in the callers.
506 */
508 {
510
514
518 }
519
520 if (s->
offset && !offset) {
526 }
527
532
534 }
535
538
541
542 if (did_split)
544
546 }
547
549 {
551
552 if (!pkt) {
557 if (ret >= 0 && s->
pb && s->
pb->
error < 0)
560 }
561 return 1;
562 }
563
565
568
570 if (ret >= 0 && s->
pb && s->
pb->
error < 0)
572
573 if (ret >= 0)
576 }
577
578 #define CHUNK_START 0x1000
579
582 {
586
588 if (!this_pktl)
591 #if FF_API_DESTRUCT_PACKET
593 pkt->destruct = NULL; // do not free original but only the copy
595 #endif
597 av_dup_packet(&this_pktl->
pkt);
// duplicate the packet if it uses non-allocated memory
599
602 } else {
604 }
605
606 if (chunked) {
616 int64_t syncto =
av_rescale(pkt->
dts + syncoffset, 1, max)*max - syncoffset;
617
619 } else
621 }
622 }
623 if (*next_point) {
625 goto next_non_null;
626
628 while ( *next_point
629 && ((chunked && !((*next_point)->pkt.flags&
CHUNK_START))
630 || !compare(s, &(*next_point)->pkt, pkt)))
631 next_point = &(*next_point)->
next;
633 goto next_non_null;
634 } else {
636 }
637 }
639
641 next_non_null:
642
643 this_pktl->
next = *next_point;
644
646 *next_point = this_pktl;
647 return 0;
648 }
649
652 {
660 if (ts == ts2) {
663 ts2=0;
664 }
665 comp= (ts>ts2) - (ts<ts2);
666 }
667
668 if (comp == 0)
670 return comp > 0;
671 }
672
675 {
677 int stream_count = 0, noninterleaved_count = 0;
678 int64_t delta_dts_max = 0;
680
681 if (pkt) {
683 if (ret < 0)
685 }
686
689 ++stream_count;
691 ++noninterleaved_count;
692 }
693 }
694
696 flush = 1;
697 } else if (!flush) {
700 int64_t delta_dts =
707 delta_dts_max=
FFMAX(delta_dts_max, delta_dts);
708 }
709 }
710 if (s->
nb_streams == stream_count+noninterleaved_count &&
713 flush = 1;
714 }
715 }
716 if (stream_count && flush) {
721
725
729
730 return 1;
731 } else {
733 return 0;
734 }
735 }
736
737 /**
738 * Interleave an AVPacket correctly so it can be muxed.
739 * @param out the interleaved packet will be output here
740 * @param in the input packet
741 * @param flush 1 if no further packets are available as input and all
742 * remaining packets should be output
743 * @return 1 if a packet was output, 0 if no packet could be output,
744 * < 0 if an error occurred
745 */
747 {
750 if (in)
753 } else
755 }
756
758 {
760
761 if (pkt) {
763
764 //FIXME/XXX/HACK drop zero sized packets
766 return 0;
767
768 av_dlog(s,
"av_interleaved_write_frame size:%d dts:%s pts:%s\n",
772
775 } else {
776 av_dlog(s,
"av_interleaved_write_frame FLUSH\n");
777 flush = 1;
778 }
779
780 for (;; ) {
783 if (ret <= 0) //FIXME cleanup needed for ret<0 ?
785
787 if (ret >= 0)
789
791 pkt = NULL;
792
793 if (ret < 0)
797 }
798 }
799
801 {
803
804 for (;; ) {
807 if (ret < 0) //FIXME cleanup needed for ret<0 ?
808 goto fail;
809 if (!ret)
810 break;
811
813 if (ret >= 0)
815
817
818 if (ret < 0)
819 goto fail;
821 goto fail;
822 }
823
826
827 fail:
830 if (ret == 0)
835 }
840 }
841
843 int64_t *dts, int64_t *wall)
844 {
848 return 0;
849 }
850
853 {
855
871 }