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 /**
48 * @file
49 * muxing functions for use within libavformat
50 */
51
52 /* fraction handling */
53
54 /**
55 * f = val + (num / den) + 0.5.
56 *
57 * 'num' is normalized so that it is such as 0 <= num < den.
58 *
59 * @param f fractional number
60 * @param val integer value
61 * @param num must be >= 0
62 * @param den must be >= 1
63 */
64 static void frac_init(AVFrac *f, int64_t
val, int64_t num, int64_t den)
65 {
66 num += (den >> 1);
67 if (num >= den) {
68 val += num / den;
69 num = num % den;
70 }
72 f->num = num;
73 f->den = den;
74 }
75
76 /**
77 * Fractional addition to f: f = f + (incr / f->den).
78 *
79 * @param f fractional number
80 * @param incr increment, can be positive or negative
81 */
83 {
84 int64_t num, den;
85
86 num = f->num + incr;
87 den = f->den;
88 if (num < 0) {
89 f->val += num / den;
90 num = num % den;
91 if (num < 0) {
92 num += den;
93 f->val--;
94 }
95 } else if (num >= den) {
96 f->val += num / den;
97 num = num % den;
98 }
99 f->num = num;
100 }
101
103 {
105 int j;
106
108
109 for (j=2; j<14; j+= 1+(j>2))
110 while (q.
den / q.
num < min_precision && q.
num % j == 0)
112 while (q.
den / q.
num < min_precision && q.
den < (1<<24))
114
115 return q;
116 }
117
119 const char *format, const char *filename)
120 {
123
125 if (!s)
126 goto nomem;
127
128 if (!oformat) {
129 if (format) {
131 if (!oformat) {
132 av_log(s,
AV_LOG_ERROR,
"Requested output format '%s' is not a suitable output format\n", format);
134 goto error;
135 }
136 } else {
138 if (!oformat) {
141 filename);
142 goto error;
143 }
144 }
145 }
146
151 goto nomem;
155 }
156 } else
158
159 if (filename)
162 return 0;
163 nomem:
166 error:
169 }
170
172 {
177
178 /**
179 * Check that tag + id is in the table
180 * If neither is in the table -> OK
181 * If tag is in the table with another id -> FAIL
182 * If id is in the table with another tag -> FAIL unless strict < normal
183 */
190 return 1;
191 }
194 avctag++;
195 }
196 }
198 return 0;
200 return 0;
201 return 1;
202 }
203
204
206 {
213
214 if (options)
216
218 goto fail;
221 goto fail;
222
223 #if FF_API_LAVF_BITEXACT
226 #endif
227
228 // some sanity checks
232 goto fail;
233 }
234
238
239 #if FF_API_LAVF_CODEC_TB
243 "timebase hint to the muxer is deprecated. Set "
244 "AVStream.time_base instead.\n");
246 }
248 #endif
249
251 /* fall back on the default timebase values */
254 else
256 }
257
263 goto fail;
264 }
268 break;
274 goto fail;
275 }
278 ) {
284 "(%d/%d) and encoder layer (%d/%d)\n",
289 goto fail;
290 }
291 }
292 break;
293 }
294
301 // the current rawvideo encoding system ends up setting
302 // the wrong codec_tag for avi/mov, we override it here
304 }
307 char tagbuf[32], tagbuf2[32];
311 "Tag %s/0x%08x incompatible with output codec id '%d' (%s)\n",
314 goto fail;
315 }
316 } else
318 }
319
323 "Codec for stream %d does not use global headers "
324 "but container format requires global headers\n", i);
325
328 }
329
334 goto fail;
335 }
340 goto fail;
341 }
342 }
343
344 /* set muxer identification string */
347 } else {
349 }
350
353 }
354
355 if (options) {
357 *options = tmp;
358 }
359
360 return 0;
361
362 fail:
365 }
366
368 {
369 int i;
371
372 /* init PTS generation */
376
380 break;
383 break;
384 default:
385 break;
386 }
388 if (den <= 0)
390
392 }
393 }
394
395 return 0;
396 }
397
399 {
401
404
407 if (ret >= 0 && s->
pb && s->
pb->
error < 0)
409 if (ret < 0)
413 }
414
417
422 } else
424 }
425
426 return 0;
427 }
428
429 #define AV_PKT_FLAG_UNCODED_FRAME 0x2000
430
431 /* Note: using sizeof(AVFrame) from outside lavu is unsafe in general, but
432 it is only being used internally to this file as a consistency check.
433 The value is chosen to be very unlikely to appear on its own and to cause
434 immediate failure if used anywhere as a real size. */
435 #define UNCODED_FRAME_PACKET_SIZE (INT_MIN / 3 * 2 + (int)sizeof(AVFrame))
436
437
438 //FIXME merge with compute_pkt_fields
440 {
442 int num, den, i;
444
445 av_dlog(s,
"compute_pkt_fields2: pts:%s dts:%s cur_dts:%s b:%d size:%d st:%d\n",
447
452 }
453
454 /* duration field */
457 if (den && num) {
459 }
460 }
461
464
465 //XXX/FIXME this is a temporary hack until all encoders output pts
467 static int warned;
468 if (!warned) {
470 warned = 1;
471 }
473 // pkt->pts= st->cur_dts;
474 pkt->
pts = st->pts.val;
475 }
476
477 //calculate dts from pts
484
486 }
487
493 "Application provided invalid, non monotonically increasing dts to muxer in stream %d: %s >= %s\n",
496 }
499 "pts (%s) < dts (%s) in stream %d\n",
503 }
504
505 av_dlog(s,
"av_write_frame: pts2:%s dts2:%s\n",
508 st->pts.val = pkt->
dts;
509
510 /* update pts */
516
517 /* HACK/FIXME, we skip the initial 0 size packets as they are most
518 * likely equal to the encoder delay, but it would be better if we
519 * had the real timestamps from the encoder */
520 if (frame_size >= 0 && (pkt->
size || st->pts.num != st->pts.den >> 1 || st->pts.val)) {
522 }
523 break;
526 break;
527 }
528 return 0;
529 }
530
531 /**
532 * Make timestamps non negative, move side data from payload to internal struct, call muxer, and restore
533 * sidedata.
534 *
535 * FIXME: this function should NEVER get undefined pts/dts beside when the
536 * AVFMT_NOTIMESTAMPS is set.
537 * Those additional safety checks should be dropped once the correct checks
538 * are set in the callers.
539 */
541 {
543
547
552 }
553
557
562 }
563
570 }
571
576
580 "Packets poorly interleaved, failed to avoid negative "
581 "timestamp %s in stream %d.\n"
582 "Try -max_interleave_delta 0 as a possible workaround.\n",
585 );
586 }
587 }
588
595 } else {
597 }
598
601
602 if (did_split)
604
606 }
607
609 {
610 if (!pkt)
611 return 0;
612
617 }
618
622 }
623
624 return 0;
625 }
626
628 {
630
632 if (ret < 0)
634
635 if (!pkt) {
640 if (ret >= 0 && s->
pb && s->
pb->
error < 0)
643 }
644 return 1;
645 }
646
648
651
653 if (ret >= 0 && s->
pb && s->
pb->
error < 0)
655
656 if (ret >= 0)
659 }
660
661 #define CHUNK_START 0x1000
662
665 {
670
672 if (!this_pktl)
675 #if FF_API_DESTRUCT_PACKET
677 pkt->
destruct =
NULL;
// do not free original but only the copy
679 #endif
686 } else {
687 // Duplicate the packet if it uses non-allocated memory
691 }
692 }
693
696 } else {
698 }
699
700 if (chunked) {
710 int64_t syncto =
av_rescale(pkt->
dts + syncoffset, 1, max)*max - syncoffset;
711
713 } else
715 }
716 }
717 if (*next_point) {
719 goto next_non_null;
720
722 while ( *next_point
723 && ((chunked && !((*next_point)->pkt.flags&
CHUNK_START))
724 || !compare(s, &(*next_point)->pkt, pkt)))
725 next_point = &(*next_point)->
next;
727 goto next_non_null;
728 } else {
730 }
731 }
733
735 next_non_null:
736
737 this_pktl->
next = *next_point;
738
740 *next_point = this_pktl;
741
742 return 0;
743 }
744
747 {
755 if (ts == ts2) {
758 ts2=0;
759 }
760 comp= (ts>ts2) - (ts<ts2);
761 }
762
763 if (comp == 0)
765 return comp > 0;
766 }
767
770 {
772 int stream_count = 0;
773 int noninterleaved_count = 0;
775
776 if (pkt) {
779 }
780
783 ++stream_count;
787 ++noninterleaved_count;
788 }
789 }
790
792 flush = 1;
793
796 !flush &&
798 ) {
800 int64_t delta_dts = INT64_MIN;
804
806 int64_t last_dts;
808
809 if (!last)
810 continue;
811
815 delta_dts =
FFMAX(delta_dts, last_dts - top_dts);
816 }
817
820 "Delay between the first packet and last packet in the "
821 "muxing queue is %"PRId64" > %"PRId64": forcing output\n",
823 flush = 1;
824 }
825 }
826
827 if (stream_count && flush) {
832
836
840
841 return 1;
842 } else {
844 return 0;
845 }
846 }
847
848 /**
849 * Interleave an AVPacket correctly so it can be muxed.
850 * @param out the interleaved packet will be output here
851 * @param in the input packet
852 * @param flush 1 if no further packets are available as input and all
853 * remaining packets should be output
854 * @return 1 if a packet was output, 0 if no packet could be output,
855 * < 0 if an error occurred
856 */
858 {
861 if (in)
864 } else
866 }
867
869 {
871
873 if (ret < 0)
874 goto fail;
875
876 if (pkt) {
878
879 av_dlog(s,
"av_interleaved_write_frame size:%d dts:%s pts:%s\n",
882 goto fail;
883
886 goto fail;
887 }
888 } else {
889 av_dlog(s,
"av_interleaved_write_frame FLUSH\n");
890 flush = 1;
891 }
892
893 for (;; ) {
896 if (pkt) {
897 memset(pkt, 0, sizeof(*pkt));
900 }
901 if (ret <= 0) //FIXME cleanup needed for ret<0 ?
903
905 if (ret >= 0)
907
909
910 if (ret < 0)
914 }
915 fail:
918 }
919
921 {
923
924 for (;; ) {
927 if (ret < 0)
928 goto fail;
929 if (!ret)
930 break;
931
933 if (ret >= 0)
935
937
938 if (ret < 0)
939 goto fail;
941 goto fail;
942 }
943
944 fail:
946 if (ret >= 0) {
948 } else {
950 }
951
954 if (ret == 0)
959 }
964 }
965
967 int64_t *dts, int64_t *wall)
968 {
972 return 0;
973 }
974
977 {
980
995
1001 }
1002
1005 {
1007
1011
1012 if (!frame) {
1014 } else {
1017 pkt.
data = (
void *)frame;
1024 }
1025
1028 }
1029
1032 {
1034 }
1035
1038 {
1040 }
1041
1043 {
1049 }