FFmpeg
Loading...
Searching...
No Matches
mpegenc.c
Go to the documentation of this file.
1/*
2 * MPEG-1/2 muxer
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
22#include "config_components.h"
23
24#include <stdint.h>
25
28#include "libavutil/fifo.h"
29#include "libavutil/log.h"
31#include "libavutil/mem.h"
32#include "libavutil/opt.h"
33
34#include "libavcodec/put_bits.h"
35
36#include "avformat.h"
37#include "avio_internal.h"
38#include "internal.h"
39#include "mpeg.h"
40#include "mux.h"
41
42#define MAX_PAYLOAD_SIZE 4096
43
51
52typedef struct StreamInfo {
54 uint8_t id;
55 int max_buffer_size; /* in bytes */
57 PacketDesc *predecode_packet; /* start of packet queue */
58 PacketDesc *last_packet; /* end of packet queue */
61 uint8_t lpcm_header[3];
67
68typedef struct MpegMuxContext {
69 const AVClass *class;
70 int packet_size; /* required packet size */
72 int pack_header_freq; /* frequency (in packets^-1) at which we send pack headers */
75 int user_mux_rate; /* bitrate in units of bits/s */
76 int mux_rate; /* bitrate in units of 50 bytes/s */
77 /* stream info */
81 int is_vcd;
83 int is_dvd;
84 int64_t last_scr; /* current system clock */
85
88
91
96
97static int put_pack_header(AVFormatContext *ctx, uint8_t *buf,
98 int64_t timestamp)
99{
100 MpegMuxContext *s = ctx->priv_data;
101 PutBitContext pb;
102
103 init_put_bits(&pb, buf, 128);
104
106 if (s->is_mpeg2)
107 put_bits(&pb, 2, 0x1);
108 else
109 put_bits(&pb, 4, 0x2);
110 put_bits(&pb, 3, (uint32_t)((timestamp >> 30) & 0x07));
111 put_bits(&pb, 1, 1);
112 put_bits(&pb, 15, (uint32_t)((timestamp >> 15) & 0x7fff));
113 put_bits(&pb, 1, 1);
114 put_bits(&pb, 15, (uint32_t)((timestamp) & 0x7fff));
115 put_bits(&pb, 1, 1);
116 if (s->is_mpeg2)
117 /* clock extension */
118 put_bits(&pb, 9, 0);
119 put_bits(&pb, 1, 1);
120 put_bits(&pb, 22, s->mux_rate);
121 put_bits(&pb, 1, 1);
122 if (s->is_mpeg2) {
123 put_bits(&pb, 1, 1);
124 put_bits(&pb, 5, 0x1f); /* reserved */
125 put_bits(&pb, 3, 0); /* stuffing length */
126 }
127 flush_put_bits(&pb);
128 return put_bytes_output(&pb);
129}
130
131static int put_system_header(AVFormatContext *ctx, uint8_t *buf, int buf_size,
132 int only_for_stream_id)
133{
134 MpegMuxContext *s = ctx->priv_data;
135 int size, i, private_stream_coded, id;
136 PutBitContext pb;
137
138 init_put_bits(&pb, buf, buf_size);
139
141 put_bits(&pb, 16, 0);
142 put_bits(&pb, 1, 1);
143
144 /* maximum bit rate of the multiplexed stream */
145 put_bits(&pb, 22, s->mux_rate);
146 put_bits(&pb, 1, 1); /* marker */
147 if (s->is_vcd && only_for_stream_id == VIDEO_ID) {
148 /* This header applies only to the video stream
149 * (see VCD standard p. IV-7) */
150 put_bits(&pb, 6, 0);
151 } else
152 put_bits(&pb, 6, s->audio_bound);
153
154 if (s->is_vcd) {
155 /* see VCD standard, p. IV-7 */
156 put_bits(&pb, 1, 0);
157 put_bits(&pb, 1, 1);
158 } else {
159 put_bits(&pb, 1, 0); /* variable bitrate */
160 put_bits(&pb, 1, 0); /* nonconstrained bitstream */
161 }
162
163 if (s->is_vcd || s->is_dvd) {
164 /* see VCD standard p IV-7 */
165 put_bits(&pb, 1, 1); /* audio locked */
166 put_bits(&pb, 1, 1); /* video locked */
167 } else {
168 put_bits(&pb, 1, 0); /* audio locked */
169 put_bits(&pb, 1, 0); /* video locked */
170 }
171
172 put_bits(&pb, 1, 1); /* marker */
173
174 if (s->is_vcd && (only_for_stream_id & 0xe0) == AUDIO_ID) {
175 /* This header applies only to the audio stream
176 * (see VCD standard p. IV-7) */
177 put_bits(&pb, 5, 0);
178 } else
179 put_bits(&pb, 5, s->video_bound);
180
181 if (s->is_dvd) {
182 put_bits(&pb, 1, 0); /* packet_rate_restriction_flag */
183 put_bits(&pb, 7, 0x7f); /* reserved byte */
184 } else
185 put_bits(&pb, 8, 0xff); /* reserved byte */
186
187 /* DVD-Video Stream_bound entries
188 * id (0xB9) video, maximum P-STD for stream 0xE0. (P-STD_buffer_bound_scale = 1)
189 * id (0xB8) audio, maximum P-STD for any MPEG audio (0xC0 to 0xC7) streams. If there are none set to 4096 (32x128). (P-STD_buffer_bound_scale = 0)
190 * id (0xBD) private stream 1 (audio other than MPEG and subpictures). (P-STD_buffer_bound_scale = 1)
191 * id (0xBF) private stream 2, NAV packs, set to 2x1024. */
192 if (s->is_dvd) {
193
194 int P_STD_max_video = 0;
195 int P_STD_max_mpeg_audio = 0;
196 int P_STD_max_mpeg_PS1 = 0;
197
198 for (i = 0; i < ctx->nb_streams; i++) {
199 StreamInfo *stream = ctx->streams[i]->priv_data;
200
201 id = stream->id;
202 if (id == 0xbd && stream->max_buffer_size > P_STD_max_mpeg_PS1) {
203 P_STD_max_mpeg_PS1 = stream->max_buffer_size;
204 } else if (id >= 0xc0 && id <= 0xc7 &&
205 stream->max_buffer_size > P_STD_max_mpeg_audio) {
206 P_STD_max_mpeg_audio = stream->max_buffer_size;
207 } else if (id == 0xe0 &&
208 stream->max_buffer_size > P_STD_max_video) {
209 P_STD_max_video = stream->max_buffer_size;
210 }
211 }
212
213 /* video */
214 put_bits(&pb, 8, 0xb9); /* stream ID */
215 put_bits(&pb, 2, 3);
216 put_bits(&pb, 1, 1);
217 put_bits(&pb, 13, P_STD_max_video / 1024);
218
219 /* audio */
220 if (P_STD_max_mpeg_audio == 0)
221 P_STD_max_mpeg_audio = 4096;
222 put_bits(&pb, 8, 0xb8); /* stream ID */
223 put_bits(&pb, 2, 3);
224 put_bits(&pb, 1, 0);
225 put_bits(&pb, 13, P_STD_max_mpeg_audio / 128);
226
227 /* private stream 1 */
228 put_bits(&pb, 8, 0xbd); /* stream ID */
229 put_bits(&pb, 2, 3);
230 put_bits(&pb, 1, 0);
231 put_bits(&pb, 13, P_STD_max_mpeg_PS1 / 128);
232
233 /* private stream 2 */
234 put_bits(&pb, 8, 0xbf); /* stream ID */
235 put_bits(&pb, 2, 3);
236 put_bits(&pb, 1, 1);
237 put_bits(&pb, 13, 2);
238 } else {
239 /* audio stream info */
240 private_stream_coded = 0;
241 for (i = 0; i < ctx->nb_streams; i++) {
242 StreamInfo *stream = ctx->streams[i]->priv_data;
243
244 /* For VCDs, only include the stream info for the stream
245 * that the pack which contains this system belongs to.
246 * (see VCD standard p. IV-7) */
247 if (!s->is_vcd || stream->id == only_for_stream_id ||
248 only_for_stream_id == 0) {
249 id = stream->id;
250 if (id < 0xc0) {
251 /* special case for private streams (AC-3 uses that) */
252 if (private_stream_coded)
253 continue;
254 private_stream_coded = 1;
255 id = 0xbd;
256 }
257 put_bits(&pb, 8, id); /* stream ID */
258 put_bits(&pb, 2, 3);
259 if (id < 0xe0) {
260 /* audio */
261 put_bits(&pb, 1, 0);
262 put_bits(&pb, 13, stream->max_buffer_size / 128);
263 } else {
264 /* video */
265 put_bits(&pb, 1, 1);
266 put_bits(&pb, 13, stream->max_buffer_size / 1024);
267 }
268 }
269 }
270 }
271
272 flush_put_bits(&pb);
273 size = put_bytes_output(&pb);
274 /* patch packet size */
275 AV_WB16(buf + 4, size - 6);
276
277 return size;
278}
279
281{
282 int buf_index, i, private_stream_coded;
283 StreamInfo *stream;
284 MpegMuxContext *s = ctx->priv_data;
285
286 if (s->is_dvd)
287 return 18; // DVD-Video system headers are 18 bytes fixed length.
288
289 buf_index = 12;
290 private_stream_coded = 0;
291 for (i = 0; i < ctx->nb_streams; i++) {
292 stream = ctx->streams[i]->priv_data;
293 if (stream->id < 0xc0) {
294 if (private_stream_coded)
295 continue;
296 private_stream_coded = 1;
297 }
298 buf_index += 3;
299 }
300 return buf_index;
301}
302
304{
305 MpegMuxContext *s = ctx->priv_data;
306 int bitrate, i, mpa_id, mpv_id, h264_id, mps_id, ac3_id, dts_id, lpcm_id, j;
307 AVStream *st;
308 StreamInfo *stream;
309 int audio_bitrate;
310 int video_bitrate;
311
312 s->packet_number = 0;
313 s->is_vcd = (CONFIG_MPEG1VCD_MUXER && ctx->oformat == &ff_mpeg1vcd_muxer.p);
314 s->is_svcd = (CONFIG_MPEG2SVCD_MUXER && ctx->oformat == &ff_mpeg2svcd_muxer.p);
315 s->is_mpeg2 = ((CONFIG_MPEG2VOB_MUXER && ctx->oformat == &ff_mpeg2vob_muxer.p) ||
316 (CONFIG_MPEG2DVD_MUXER && ctx->oformat == &ff_mpeg2dvd_muxer.p) ||
317 (CONFIG_MPEG2SVCD_MUXER && ctx->oformat == &ff_mpeg2svcd_muxer.p));
318 s->is_dvd = (CONFIG_MPEG2DVD_MUXER && ctx->oformat == &ff_mpeg2dvd_muxer.p);
319
320 if (ctx->packet_size) {
321 if (ctx->packet_size < 20 || ctx->packet_size > (1 << 23) + 10) {
322 av_log(ctx, AV_LOG_ERROR, "Invalid packet size %d\n",
323 ctx->packet_size);
324 return AVERROR(EINVAL);
325 }
326 s->packet_size = ctx->packet_size;
327 } else
328 s->packet_size = 2048;
329 if (ctx->max_delay < 0) /* Not set by the caller */
330 ctx->max_delay = AV_TIME_BASE*7/10;
331
332 s->vcd_padding_bytes_written = 0;
333 s->vcd_padding_bitrate_num = 0;
334
335 s->audio_bound = 0;
336 s->video_bound = 0;
337
338 mpa_id = AUDIO_ID;
339 ac3_id = AC3_ID;
340 dts_id = DTS_ID;
341 mpv_id = VIDEO_ID;
342 h264_id = H264_ID;
343 mps_id = SUB_ID;
344 lpcm_id = LPCM_ID;
345
346 for (i = 0; i < ctx->nb_streams; i++) {
347 st = ctx->streams[i];
348 stream = av_mallocz(sizeof(StreamInfo));
349 if (!stream)
350 return AVERROR(ENOMEM);
351 st->priv_data = stream;
352
353 avpriv_set_pts_info(st, 64, 1, 90000);
354
355 switch (st->codecpar->codec_type) {
357 if (!s->is_mpeg2 &&
363 "%s in MPEG-1 system streams is not widely supported, "
364 "consider using the vob or the dvd muxer "
365 "to force a MPEG-2 program stream.\n",
367 if (st->codecpar->codec_id == AV_CODEC_ID_AC3) {
368 stream->id = ac3_id++;
369 } else if (st->codecpar->codec_id == AV_CODEC_ID_DTS) {
370 stream->id = dts_id++;
371 } else if (st->codecpar->codec_id == AV_CODEC_ID_PCM_S16BE) {
372 stream->id = lpcm_id++;
373 for (j = 0; j < 4; j++) {
374 if (lpcm_freq_tab[j] == st->codecpar->sample_rate)
375 break;
376 }
377 if (j == 4) {
378 int sr;
379 av_log(ctx, AV_LOG_ERROR, "Invalid sampling rate for PCM stream.\n");
380 av_log(ctx, AV_LOG_INFO, "Allowed sampling rates:");
381 for (sr = 0; sr < 4; sr++)
382 av_log(ctx, AV_LOG_INFO, " %d", lpcm_freq_tab[sr]);
383 av_log(ctx, AV_LOG_INFO, "\n");
384 return AVERROR(EINVAL);
385 }
386 if (st->codecpar->ch_layout.nb_channels > 8) {
387 av_log(ctx, AV_LOG_ERROR, "At most 8 channels allowed for LPCM streams.\n");
388 return AVERROR(EINVAL);
389 }
390 stream->lpcm_header[0] = 0x0c;
391 stream->lpcm_header[1] = (st->codecpar->ch_layout.nb_channels - 1) | (j << 4);
392 stream->lpcm_header[2] = 0x80;
393 stream->lpcm_align = st->codecpar->ch_layout.nb_channels * 2;
394 } else if (st->codecpar->codec_id == AV_CODEC_ID_PCM_DVD) {
395 int freq;
396
397 switch (st->codecpar->sample_rate) {
398 case 48000: freq = 0; break;
399 case 96000: freq = 1; break;
400 case 44100: freq = 2; break;
401 case 32000: freq = 3; break;
402 default:
403 av_log(ctx, AV_LOG_ERROR, "Unsupported sample rate.\n");
404 return AVERROR(EINVAL);
405 }
406
407 stream->lpcm_header[0] = 0x0c;
408 stream->lpcm_header[1] = (freq << 4) |
409 (((st->codecpar->bits_per_coded_sample - 16) / 4) << 6) |
411 stream->lpcm_header[2] = 0x80;
412 stream->id = lpcm_id++;
414 } else if (st->codecpar->codec_id == AV_CODEC_ID_MLP ||
416 av_log(ctx, AV_LOG_ERROR, "Support for muxing audio codec %s not implemented.\n",
419 } else if (st->codecpar->codec_id != AV_CODEC_ID_MP1 &&
422 av_log(ctx, AV_LOG_ERROR, "Unsupported audio codec. Must be one of mp1, mp2, mp3, 16-bit pcm_dvd, pcm_s16be, ac3 or dts.\n");
423 return AVERROR(EINVAL);
424 } else {
425 stream->id = mpa_id++;
426 }
427
428 /* This value HAS to be used for VCD (see VCD standard, p. IV-7).
429 * Right now it is also used for everything else. */
430 stream->max_buffer_size = 4 * 1024;
431 s->audio_bound++;
432 break;
433 case AVMEDIA_TYPE_VIDEO: {
434 const AVPacketSideData *sd;
435 AVCPBProperties *props = NULL;
437 stream->id = h264_id++;
438 else
439 stream->id = mpv_id++;
440
444 if (sd)
445 props = (AVCPBProperties*)sd->data;
446 if (props && props->buffer_size)
447 stream->max_buffer_size = 6 * 1024 + props->buffer_size / 8;
448 else {
450 "VBV buffer size not set, using default size of 230KB\n"
451 "If you want the mpeg file to be compliant to some specification\n"
452 "Like DVD, VCD or others, make sure you set the correct buffer size\n");
453 // FIXME: this is probably too small as default
454 stream->max_buffer_size = 230 * 1024;
455 }
456 if (stream->max_buffer_size > 1024 * 8191) {
457 av_log(ctx, AV_LOG_WARNING, "buffer size %d, too large\n", stream->max_buffer_size);
458 stream->max_buffer_size = 1024 * 8191;
459 }
460 s->video_bound++;
461 break;
462 }
464 stream->id = mps_id++;
465 stream->max_buffer_size = 16 * 1024;
466 break;
467 default:
468 av_log(ctx, AV_LOG_ERROR, "Invalid media type %s for output stream #%d\n",
470 return AVERROR(EINVAL);
471 }
472 stream->fifo = av_fifo_alloc2(16, 1, 0);
473 if (!stream->fifo)
474 return AVERROR(ENOMEM);
475 }
476
477 /* The system header is emitted, right after the pack header (which is at
478 * most 14 bytes), into the fixed 128-byte buffer used by flush_packet().
479 * Reject configurations whose system header would not fit. */
480 if (get_system_header_size(ctx) > 128 - 14) {
482 "Too many streams to fit the MPEG program stream system header\n");
483 return AVERROR(EINVAL);
484 }
485
486 bitrate = 0;
487 audio_bitrate = 0;
488 video_bitrate = 0;
489 for (i = 0; i < ctx->nb_streams; i++) {
490 const AVPacketSideData *sd;
491 AVCPBProperties *props = NULL;
492 int codec_rate;
493 st = ctx->streams[i];
494 stream = (StreamInfo *)st->priv_data;
495
499 if (sd)
500 props = (AVCPBProperties*)sd->data;
501 if (props)
502 codec_rate = props->max_bitrate;
503 else
504 codec_rate = st->codecpar->bit_rate;
505
506 if (!codec_rate)
507 codec_rate = (1 << 21) * 8 * 50 / ctx->nb_streams;
508
509 bitrate += codec_rate;
510
511 if ((stream->id & 0xe0) == AUDIO_ID)
512 audio_bitrate += codec_rate;
513 else if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
514 video_bitrate += codec_rate;
515 }
516
517 if (s->user_mux_rate) {
518 s->mux_rate = (s->user_mux_rate + (8 * 50) - 1) / (8 * 50);
519 } else {
520 /* we increase slightly the bitrate to take into account the
521 * headers. XXX: compute it exactly */
522 bitrate += bitrate / 20;
523 bitrate += 10000;
524 s->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
525 if (s->mux_rate >= (1<<22)) {
526 av_log(ctx, AV_LOG_WARNING, "mux rate %d is too large\n", s->mux_rate);
527 s->mux_rate = (1<<22) - 1;
528 }
529 }
530
531 if (s->is_vcd) {
532 int64_t overhead_rate;
533
534 /* The VCD standard mandates that the mux_rate field is 3528
535 * (see standard p. IV-6).
536 * The value is actually "wrong", i.e. if you calculate
537 * it using the normal formula and the 75 sectors per second transfer
538 * rate you get a different value because the real pack size is 2324,
539 * not 2352. But the standard explicitly specifies that the mux_rate
540 * field in the header must have this value. */
541 // s->mux_rate = 2352 * 75 / 50; /* = 3528 */
542
543 /* The VCD standard states that the muxed stream must be
544 * exactly 75 packs / second (the data rate of a single speed cdrom).
545 * Since the video bitrate (probably 1150000 bits/sec) will be below
546 * the theoretical maximum we have to add some padding packets
547 * to make up for the lower data rate.
548 * (cf. VCD standard p. IV-6 ) */
549
550 /* Add the header overhead to the data rate.
551 * 2279 data bytes per audio pack, 2294 data bytes per video pack */
552 overhead_rate = audio_bitrate * 2294LL * (2324 - 2279);
553 overhead_rate += video_bitrate * 2279LL * (2324 - 2294);
554
555 /* Add padding so that the full bitrate is 2324*75 bytes/sec */
556 s->vcd_padding_bitrate_num = (2324LL * 75 * 8 - bitrate) * 2279 * 2294 - overhead_rate;
557#define VCD_PADDING_BITRATE_DEN (2279 * 2294)
558 }
559
560 if (s->is_vcd || s->is_mpeg2)
561 /* every packet */
562 s->pack_header_freq = 1;
563 else
564 /* every 2 seconds */
565 s->pack_header_freq = 2 * bitrate / s->packet_size / 8;
566
567 /* the above seems to make pack_header_freq zero sometimes */
568 if (s->pack_header_freq == 0)
569 s->pack_header_freq = 1;
570
571 if (s->is_mpeg2)
572 /* every 200 packets. Need to look at the spec. */
573 s->system_header_freq = s->pack_header_freq * 40;
574 else if (s->is_vcd)
575 /* the standard mandates that there are only two system headers
576 * in the whole file: one in the first packet of each stream.
577 * (see standard p. IV-7 and IV-8) */
578 s->system_header_freq = 0x7fffffff;
579 else
580 s->system_header_freq = s->pack_header_freq * 5;
581
582 for (i = 0; i < ctx->nb_streams; i++) {
583 stream = ctx->streams[i]->priv_data;
584 stream->packet_number = 0;
585 }
586 s->system_header_size = get_system_header_size(ctx);
587 s->last_scr = AV_NOPTS_VALUE;
588 return 0;
589}
590
591static inline void put_timestamp(AVIOContext *pb, int id, int64_t timestamp)
592{
593 avio_w8(pb, (id << 4) | (((timestamp >> 30) & 0x07) << 1) | 1);
594 avio_wb16(pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));
595 avio_wb16(pb, (uint16_t)((((timestamp) & 0x7fff) << 1) | 1));
596}
597
598/* return the number of padding bytes that should be inserted into
599 * the multiplexed stream. */
601{
602 MpegMuxContext *s = ctx->priv_data;
603 int pad_bytes = 0;
604
605 if (s->vcd_padding_bitrate_num > 0 && pts != AV_NOPTS_VALUE) {
606 int64_t full_pad_bytes;
607
608 // FIXME: this is wrong
609 full_pad_bytes =
610 av_rescale(s->vcd_padding_bitrate_num, pts, 90000LL * 8 * VCD_PADDING_BITRATE_DEN);
611 pad_bytes = (int)(full_pad_bytes - s->vcd_padding_bytes_written);
612
613 if (pad_bytes < 0)
614 /* might happen if we have already padded to a later timestamp. This
615 * can occur if another stream has already advanced further. */
616 pad_bytes = 0;
617 }
618
619 return pad_bytes;
620}
621
622/* Write an MPEG padding packet header. */
624 int packet_bytes)
625{
626 MpegMuxContext *s = ctx->priv_data;
627
629 avio_wb16(pb, packet_bytes - 6);
630 if (!s->is_mpeg2) {
631 avio_w8(pb, 0x0f);
632 packet_bytes -= 7;
633 } else
634 packet_bytes -= 6;
635
636 ffio_fill(pb, 0xff, packet_bytes);
637}
638
640{
641 int nb_frames = 0;
642 PacketDesc *pkt_desc = stream->premux_packet;
643
644 while (len > 0) {
645 if (pkt_desc->size == pkt_desc->unwritten_size)
646 nb_frames++;
647 len -= pkt_desc->unwritten_size;
648 pkt_desc = pkt_desc->next;
649 }
650
651 return nb_frames;
652}
653
654static int fifo_avio_wrapper(void *opaque, void *buf, size_t *nb_elems)
655{
656 avio_write(opaque, buf, *nb_elems);
657 return 0;
658}
659
660/* flush the packet on stream stream_index */
661static int flush_packet(AVFormatContext *ctx, int stream_index,
662 int64_t pts, int64_t dts, int64_t scr, int trailer_size)
663{
664 MpegMuxContext *s = ctx->priv_data;
665 StreamInfo *stream = ctx->streams[stream_index]->priv_data;
666 uint8_t *buf_ptr;
667 int size, payload_size, startcode, id, stuffing_size, header_len;
668 int packet_size;
669 uint8_t buffer[128];
670 uint8_t *buf_end = buffer + sizeof(buffer);
671 int zero_trail_bytes = 0;
672 int pad_packet_bytes = 0;
673 int pes_flags;
674 /* "general" pack without data specific to one stream? */
675 int general_pack = 0;
676 int nb_frames;
677
678 id = stream->id;
679
680 av_log(ctx, AV_LOG_TRACE, "packet ID=%2x PTS=%0.3f\n", id, pts / 90000.0);
681
682 buf_ptr = buffer;
683
684 if ((s->packet_number % s->pack_header_freq) == 0 || s->last_scr != scr) {
685 /* output pack and systems header if needed */
686 size = put_pack_header(ctx, buf_ptr, scr);
687 buf_ptr += size;
688 s->last_scr = scr;
689
690 if (s->is_vcd) {
691 /* there is exactly one system header for each stream in a VCD MPEG,
692 * One in the very first video packet and one in the very first
693 * audio packet (see VCD standard p. IV-7 and IV-8). */
694
695 if (stream->packet_number == 0) {
696 size = put_system_header(ctx, buf_ptr, buf_end - buf_ptr, id);
697 buf_ptr += size;
698 }
699 } else if (s->is_dvd) {
700 if (stream->align_iframe || s->packet_number == 0) {
701 int PES_bytes_to_fill = s->packet_size - size - 10;
702
703 if (pts != AV_NOPTS_VALUE) {
704 if (dts != pts)
705 PES_bytes_to_fill -= 5 + 5;
706 else
707 PES_bytes_to_fill -= 5;
708 }
709
710 if (stream->bytes_to_iframe == 0 || s->packet_number == 0) {
711 size = put_system_header(ctx, buf_ptr, buf_end - buf_ptr, 0);
712 buf_ptr += size;
713 size = buf_ptr - buffer;
714 avio_write(ctx->pb, buffer, size);
715
717 avio_wb16(ctx->pb, 0x03d4); // length
718 avio_w8(ctx->pb, 0x00); // substream ID, 00=PCI
719 ffio_fill(ctx->pb, 0x00, 979);
720
722 avio_wb16(ctx->pb, 0x03fa); // length
723 avio_w8(ctx->pb, 0x01); // substream ID, 01=DSI
724 ffio_fill(ctx->pb, 0x00, 1017);
725
726 memset(buffer, 0, 128);
727 buf_ptr = buffer;
728 s->packet_number++;
729 stream->align_iframe = 0;
730 // FIXME: rounding and first few bytes of each packet
731 scr += s->packet_size * 90000LL /
732 (s->mux_rate * 50LL);
733 size = put_pack_header(ctx, buf_ptr, scr);
734 s->last_scr = scr;
735 buf_ptr += size;
736 /* GOP Start */
737 } else if (stream->bytes_to_iframe < PES_bytes_to_fill) {
738 pad_packet_bytes = PES_bytes_to_fill -
739 stream->bytes_to_iframe;
740 }
741 }
742 } else {
743 if ((s->packet_number % s->system_header_freq) == 0) {
744 size = put_system_header(ctx, buf_ptr, buf_end - buf_ptr, 0);
745 buf_ptr += size;
746 }
747 }
748 }
749 size = buf_ptr - buffer;
750 avio_write(ctx->pb, buffer, size);
751
752 packet_size = s->packet_size - size;
753
754 if (s->is_vcd && (id & 0xe0) == AUDIO_ID)
755 /* The VCD standard demands that 20 zero bytes follow
756 * each audio pack (see standard p. IV-8). */
757 zero_trail_bytes += 20;
758
759 if ((s->is_vcd && stream->packet_number == 0) ||
760 (s->is_svcd && s->packet_number == 0)) {
761 /* for VCD the first pack of each stream contains only the pack header,
762 * the system header and lots of padding (see VCD standard p. IV-6).
763 * In the case of an audio pack, 20 zero bytes are also added at
764 * the end. */
765 /* For SVCD we fill the very first pack to increase compatibility with
766 * some DVD players. Not mandated by the standard. */
767 if (s->is_svcd)
768 /* the system header refers to both streams and no stream data */
769 general_pack = 1;
770 pad_packet_bytes = packet_size - zero_trail_bytes;
771 }
772
773 packet_size -= pad_packet_bytes + zero_trail_bytes;
774
775 if (packet_size > 0) {
776 size_t fifo_data;
777 /* packet header size */
778 packet_size -= 6;
779
780 /* packet header */
781 if (s->is_mpeg2) {
782 header_len = 3;
783 if (stream->packet_number == 0)
784 header_len += 3; /* PES extension */
785 header_len += 1; /* obligatory stuffing byte */
786 } else {
787 header_len = 0;
788 }
789 if (pts != AV_NOPTS_VALUE) {
790 if (dts != pts)
791 header_len += 5 + 5;
792 else
793 header_len += 5;
794 } else {
795 if (!s->is_mpeg2)
796 header_len++;
797 }
798
799 payload_size = packet_size - header_len;
800 if (id < 0xc0) {
801 startcode = PRIVATE_STREAM_1;
802 payload_size -= 1;
803 if (id >= 0x40) {
804 payload_size -= 3;
805 if (id >= 0xa0)
806 payload_size -= 3;
807 }
808 } else {
809 startcode = 0x100 + id;
810 }
811
812 stuffing_size = payload_size - av_fifo_can_read(stream->fifo);
813
814 // first byte does not fit -> reset pts/dts + stuffing
815 if (payload_size <= trailer_size && pts != AV_NOPTS_VALUE) {
816 int timestamp_len = 0;
817 if (dts != pts)
818 timestamp_len += 5;
819 if (pts != AV_NOPTS_VALUE)
820 timestamp_len += s->is_mpeg2 ? 5 : 4;
821 pts =
822 dts = AV_NOPTS_VALUE;
823 header_len -= timestamp_len;
824 if (s->is_dvd && stream->align_iframe) {
825 pad_packet_bytes += timestamp_len;
826 packet_size -= timestamp_len;
827 } else {
828 payload_size += timestamp_len;
829 }
830 stuffing_size += timestamp_len;
831 if (payload_size > trailer_size)
832 stuffing_size += payload_size - trailer_size;
833 }
834
835 // can't use padding, so use stuffing
836 if (pad_packet_bytes > 0 && pad_packet_bytes <= 7) {
837 packet_size += pad_packet_bytes;
838 payload_size += pad_packet_bytes; // undo the previous adjustment
839 if (stuffing_size < 0)
840 stuffing_size = pad_packet_bytes;
841 else
842 stuffing_size += pad_packet_bytes;
843 pad_packet_bytes = 0;
844 }
845
846 if (stuffing_size < 0)
847 stuffing_size = 0;
848
849 if (startcode == PRIVATE_STREAM_1 && id >= 0xa0) {
850 if (payload_size < av_fifo_can_read(stream->fifo))
851 stuffing_size += payload_size % stream->lpcm_align;
852 }
853
854 if (stuffing_size > 16) { /* <=16 for MPEG-1, <=32 for MPEG-2 */
855 pad_packet_bytes += stuffing_size;
856 packet_size -= stuffing_size;
857 payload_size -= stuffing_size;
858 stuffing_size = 0;
859 }
860
861 nb_frames = get_nb_frames(ctx, stream, payload_size - stuffing_size);
862
863 avio_wb32(ctx->pb, startcode);
864
865 avio_wb16(ctx->pb, packet_size);
866
867 if (!s->is_mpeg2)
868 ffio_fill(ctx->pb, 0xff, stuffing_size);
869
870 if (s->is_mpeg2) {
871 avio_w8(ctx->pb, 0x80); /* mpeg2 id */
872
873 pes_flags = 0;
874
875 if (pts != AV_NOPTS_VALUE) {
876 pes_flags |= 0x80;
877 if (dts != pts)
878 pes_flags |= 0x40;
879 }
880
881 /* Both the MPEG-2 and the SVCD standards demand that the
882 * P-STD_buffer_size field be included in the first packet of
883 * every stream. (see SVCD standard p. 26 V.2.3.1 and V.2.3.2
884 * and MPEG-2 standard 2.7.7) */
885 if (stream->packet_number == 0)
886 pes_flags |= 0x01;
887
888 avio_w8(ctx->pb, pes_flags); /* flags */
889 avio_w8(ctx->pb, header_len - 3 + stuffing_size);
890
891 if (pes_flags & 0x80) /* write pts */
892 put_timestamp(ctx->pb, (pes_flags & 0x40) ? 0x03 : 0x02, pts);
893 if (pes_flags & 0x40) /* write dts */
894 put_timestamp(ctx->pb, 0x01, dts);
895
896 if (pes_flags & 0x01) { /* write pes extension */
897 avio_w8(ctx->pb, 0x10); /* flags */
898
899 /* P-STD buffer info */
900 if ((id & 0xe0) == AUDIO_ID)
901 avio_wb16(ctx->pb, 0x4000 | stream->max_buffer_size / 128);
902 else
903 avio_wb16(ctx->pb, 0x6000 | stream->max_buffer_size / 1024);
904 }
905 } else {
906 if (pts != AV_NOPTS_VALUE) {
907 if (dts != pts) {
908 put_timestamp(ctx->pb, 0x03, pts);
909 put_timestamp(ctx->pb, 0x01, dts);
910 } else {
911 put_timestamp(ctx->pb, 0x02, pts);
912 }
913 } else {
914 avio_w8(ctx->pb, 0x0f);
915 }
916 }
917
918 if (s->is_mpeg2) {
919 /* special stuffing byte that is always written
920 * to prevent accidental generation of start codes. */
921 avio_w8(ctx->pb, 0xff);
922
923 ffio_fill(ctx->pb, 0xff, stuffing_size);
924 }
925
926 if (startcode == PRIVATE_STREAM_1) {
927 avio_w8(ctx->pb, id);
928 if (id >= 0xa0) {
929 /* LPCM (XXX: check nb_frames) */
930 avio_w8(ctx->pb, 7);
931 avio_wb16(ctx->pb, 4); /* skip 3 header bytes */
932 avio_w8(ctx->pb, stream->lpcm_header[0]);
933 avio_w8(ctx->pb, stream->lpcm_header[1]);
934 avio_w8(ctx->pb, stream->lpcm_header[2]);
935 } else if (id >= 0x40) {
936 /* AC-3 */
937 avio_w8(ctx->pb, nb_frames);
938 avio_wb16(ctx->pb, trailer_size + 1);
939 }
940 }
941
942 /* output data */
943 fifo_data = payload_size - stuffing_size;
944 av_assert0(fifo_data <= av_fifo_can_read(stream->fifo));
945 av_fifo_read_to_cb(stream->fifo, fifo_avio_wrapper, ctx->pb, &fifo_data);
946 stream->bytes_to_iframe -= fifo_data;
947 } else {
948 payload_size =
949 stuffing_size = 0;
950 }
951
952 if (pad_packet_bytes > 0)
953 put_padding_packet(ctx, ctx->pb, pad_packet_bytes);
954
955 ffio_fill(ctx->pb, 0x00, zero_trail_bytes);
956
958
959 s->packet_number++;
960
961 /* only increase the stream packet number if this pack actually contains
962 * something that is specific to this stream! I.e. a dedicated header
963 * or some data. */
964 if (!general_pack)
965 stream->packet_number++;
966
967 return payload_size - stuffing_size;
968}
969
971{
972 /* There are two ways to do this padding: writing a sector/pack
973 * of 0 values, or writing an MPEG padding pack. Both seem to
974 * work with most decoders, BUT the VCD standard only allows a 0-sector
975 * (see standard p. IV-4, IV-5).
976 * So a 0-sector it is... */
977
978 MpegMuxContext *s = ctx->priv_data;
979
980 ffio_fill(ctx->pb, 0, s->packet_size);
981
982 s->vcd_padding_bytes_written += s->packet_size;
983
985
986 /* increasing the packet number is correct. The SCR of the following packs
987 * is calculated from the packet_number and it has to include the padding
988 * sector (it represents the sector index, not the MPEG pack index)
989 * (see VCD standard p. IV-6) */
990 s->packet_number++;
991}
992
994{
995 int i;
996
997 for (i = 0; i < ctx->nb_streams; i++) {
998 AVStream *st = ctx->streams[i];
999 StreamInfo *stream = st->priv_data;
1000 PacketDesc *pkt_desc;
1001
1002 while ((pkt_desc = stream->predecode_packet) &&
1003 scr > pkt_desc->dts) { // FIXME: > vs >=
1004 if (stream->buffer_index < pkt_desc->size ||
1005 stream->predecode_packet == stream->premux_packet) {
1007 "buffer underflow st=%d bufi=%d size=%d\n",
1008 i, stream->buffer_index, pkt_desc->size);
1009 break;
1010 }
1011 stream->buffer_index -= pkt_desc->size;
1012 stream->predecode_packet = pkt_desc->next;
1013 if (!stream->predecode_packet)
1014 stream->last_packet = NULL;
1015 av_freep(&pkt_desc);
1016 }
1017 }
1018
1019 return 0;
1020}
1021
1023{
1024 MpegMuxContext *s = ctx->priv_data;
1025 AVStream *st;
1026 StreamInfo *stream;
1027 int i, avail_space = 0, es_size, trailer_size;
1028 int best_i = -1;
1029 int best_score = INT_MIN;
1030 int ignore_constraints = 0;
1031 int ignore_delay = 0;
1032 int64_t scr = s->last_scr;
1033 PacketDesc *timestamp_packet;
1034 const int64_t max_delay = av_rescale(ctx->max_delay, 90000, AV_TIME_BASE);
1035
1036retry:
1037 for (i = 0; i < ctx->nb_streams; i++) {
1038 AVStream *st = ctx->streams[i];
1039 StreamInfo *stream = st->priv_data;
1040 const size_t avail_data = av_fifo_can_read(stream->fifo);
1041 const int space = stream->max_buffer_size - stream->buffer_index;
1042 int rel_space = 1024LL * space / stream->max_buffer_size;
1043 PacketDesc *next_pkt = stream->premux_packet;
1044
1045 /* for subtitle, a single PES packet must be generated,
1046 * so we flush after every single subtitle packet */
1047 if (s->packet_size > avail_data && !flush
1049 return 0;
1050 if (avail_data == 0)
1051 continue;
1052 av_assert0(avail_data > 0);
1053
1054 if (space < s->packet_size && !ignore_constraints)
1055 continue;
1056
1057 if (next_pkt && next_pkt->dts - scr > max_delay && !ignore_delay)
1058 continue;
1059 if ( stream->predecode_packet
1060 && stream->predecode_packet->size > stream->buffer_index)
1061 rel_space += 1<<28;
1062 if (rel_space > best_score) {
1063 best_score = rel_space;
1064 best_i = i;
1065 avail_space = space;
1066 }
1067 }
1068
1069 if (best_i < 0) {
1070 int64_t best_dts = INT64_MAX;
1071 int has_premux = 0;
1072
1073 for (i = 0; i < ctx->nb_streams; i++) {
1074 AVStream *st = ctx->streams[i];
1075 StreamInfo *stream = st->priv_data;
1076 PacketDesc *pkt_desc = stream->predecode_packet;
1077 if (pkt_desc && pkt_desc->dts < best_dts)
1078 best_dts = pkt_desc->dts;
1079 has_premux |= !!stream->premux_packet;
1080 }
1081
1082 if (best_dts < INT64_MAX) {
1083 av_log(ctx, AV_LOG_TRACE, "bumping scr, scr:%f, dts:%f\n",
1084 scr / 90000.0, best_dts / 90000.0);
1085
1086 if (scr >= best_dts + 1 && !ignore_constraints) {
1088 "packet too large, ignoring buffer limits to mux it\n");
1089 ignore_constraints = 1;
1090 }
1091 scr = FFMAX(best_dts + 1, scr);
1092 if (remove_decoded_packets(ctx, scr) < 0)
1093 return -1;
1094 } else if (has_premux && flush) {
1096 "delay too large, ignoring ...\n");
1097 ignore_delay = 1;
1098 ignore_constraints = 1;
1099 } else
1100 return 0;
1101
1102 goto retry;
1103 }
1104
1105 av_assert0(best_i >= 0);
1106
1107 st = ctx->streams[best_i];
1108 stream = st->priv_data;
1109
1110 av_assert0(av_fifo_can_read(stream->fifo) > 0);
1111
1112 av_assert0(avail_space >= s->packet_size || ignore_constraints);
1113
1114 timestamp_packet = stream->premux_packet;
1115 if (timestamp_packet->unwritten_size == timestamp_packet->size) {
1116 trailer_size = 0;
1117 } else {
1118 trailer_size = timestamp_packet->unwritten_size;
1119 timestamp_packet = timestamp_packet->next;
1120 }
1121
1122 if (timestamp_packet) {
1123 av_log(ctx, AV_LOG_TRACE, "dts:%f pts:%f scr:%f stream:%d\n",
1124 timestamp_packet->dts / 90000.0,
1125 timestamp_packet->pts / 90000.0,
1126 scr / 90000.0, best_i);
1127 es_size = flush_packet(ctx, best_i, timestamp_packet->pts,
1128 timestamp_packet->dts, scr, trailer_size);
1129 } else {
1130 av_assert0(av_fifo_can_read(stream->fifo) == trailer_size);
1131 es_size = flush_packet(ctx, best_i, AV_NOPTS_VALUE, AV_NOPTS_VALUE, scr,
1132 trailer_size);
1133 }
1134
1135 if (s->is_vcd) {
1136 /* Write one or more padding sectors, if necessary, to reach
1137 * the constant overall bitrate. */
1138 int vcd_pad_bytes;
1139
1140 // FIXME: pts cannot be correct here
1141 while ((vcd_pad_bytes = get_vcd_padding_size(ctx, stream->premux_packet->pts)) >= s->packet_size) {
1143 // FIXME: rounding and first few bytes of each packet
1144 s->last_scr += s->packet_size * 90000LL / (s->mux_rate * 50LL);
1145 }
1146 }
1147
1148 stream->buffer_index += es_size;
1149 // FIXME: rounding and first few bytes of each packet
1150 s->last_scr += s->packet_size * 90000LL / (s->mux_rate * 50LL);
1151
1152 while (stream->premux_packet &&
1153 stream->premux_packet->unwritten_size <= es_size) {
1154 es_size -= stream->premux_packet->unwritten_size;
1155 stream->premux_packet = stream->premux_packet->next;
1156 }
1157 if (es_size) {
1158 av_assert0(stream->premux_packet);
1159 stream->premux_packet->unwritten_size -= es_size;
1160 }
1161
1162 if (remove_decoded_packets(ctx, s->last_scr) < 0)
1163 return -1;
1164
1165 return 1;
1166}
1167
1169{
1170 int stream_index = pkt->stream_index;
1171 int size = pkt->size;
1172 const uint8_t *buf = pkt->data;
1173 MpegMuxContext *s = ctx->priv_data;
1174 AVStream *st = ctx->streams[stream_index];
1175 StreamInfo *stream = st->priv_data;
1176 int64_t pts, dts;
1177 PacketDesc *pkt_desc;
1178 int preload, ret;
1179 size_t can_write;
1180 const int is_iframe = st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
1181 (pkt->flags & AV_PKT_FLAG_KEY);
1182
1183 preload = av_rescale(s->preload, 90000, AV_TIME_BASE);
1184
1185 pts = pkt->pts;
1186 dts = pkt->dts;
1187
1188 if (s->last_scr == AV_NOPTS_VALUE) {
1189 if (dts == AV_NOPTS_VALUE || (dts < preload && ctx->avoid_negative_ts) || s->is_dvd) {
1190 if (dts != AV_NOPTS_VALUE)
1191 s->preload += av_rescale(-dts, AV_TIME_BASE, 90000);
1192 s->last_scr = 0;
1193 } else {
1194 s->last_scr = dts - preload;
1195 s->preload = 0;
1196 }
1197 preload = av_rescale(s->preload, 90000, AV_TIME_BASE);
1198 av_log(ctx, AV_LOG_DEBUG, "First SCR: %"PRId64" First DTS: %"PRId64"\n", s->last_scr, dts + preload);
1199 }
1200
1201 if (dts != AV_NOPTS_VALUE) dts += preload;
1202 if (pts != AV_NOPTS_VALUE) pts += preload;
1203
1204 av_log(ctx, AV_LOG_TRACE, "dts:%f pts:%f flags:%d stream:%d nopts:%d\n",
1205 dts / 90000.0, pts / 90000.0, pkt->flags,
1206 pkt->stream_index, pts != AV_NOPTS_VALUE);
1207
1209 if (size < 3) {
1210 av_log(ctx, AV_LOG_ERROR, "Invalid packet size %d\n", size);
1211 return AVERROR(EINVAL);
1212 }
1213
1214 /* Skip first 3 bytes of packet data, which comprise PCM header
1215 and will be written fresh by this muxer. */
1216 buf += 3;
1217 size -= 3;
1218 }
1219
1220 /* Enlarge the FIFO before adding a new PacketDesc
1221 * in order to avoid inconsistencies on failure. */
1222 can_write = av_fifo_can_write(stream->fifo);
1223 if (can_write < size) {
1224 ret = av_fifo_grow2(stream->fifo, size - can_write);
1225 if (ret < 0)
1226 return ret;
1227 }
1228 pkt_desc = av_mallocz(sizeof(PacketDesc));
1229 if (!pkt_desc)
1230 return AVERROR(ENOMEM);
1231 if (!stream->predecode_packet) {
1232 stream->predecode_packet = pkt_desc;
1233 } else
1234 stream->last_packet->next = pkt_desc;
1235 stream->last_packet = pkt_desc;
1236 if (!stream->premux_packet)
1237 stream->premux_packet = pkt_desc;
1238 pkt_desc->pts = pts;
1239 pkt_desc->dts = dts;
1240 pkt_desc->unwritten_size =
1241 pkt_desc->size = size;
1242
1243 if (s->is_dvd) {
1244 // min VOBU length 0.4 seconds (mpucoder)
1245 if (is_iframe &&
1246 (s->packet_number == 0 || pts != AV_NOPTS_VALUE &&
1247 (pts - stream->vobu_start_pts >= 36000))) {
1248 stream->bytes_to_iframe = av_fifo_can_read(stream->fifo);
1249 stream->align_iframe = 1;
1250 stream->vobu_start_pts = pts;
1251 }
1252 }
1253
1254 av_fifo_write(stream->fifo, buf, size);
1255
1256 for (;;) {
1257 int ret = output_packet(ctx, 0);
1258 if (ret <= 0)
1259 return ret;
1260 }
1261}
1262
1264{
1265 StreamInfo *stream;
1266 int i;
1267
1268 for (;;) {
1269 int ret = output_packet(ctx, 1);
1270 if (ret < 0)
1271 return ret;
1272 else if (ret == 0)
1273 break;
1274 }
1275
1276 /* End header according to MPEG-1 systems standard. We do not write
1277 * it as it is usually not needed by decoders and because it
1278 * complicates MPEG stream concatenation. */
1279 // avio_wb32(ctx->pb, ISO_11172_END_CODE);
1280
1281 for (i = 0; i < ctx->nb_streams; i++) {
1282 stream = ctx->streams[i]->priv_data;
1283
1284 av_assert0(av_fifo_can_read(stream->fifo) == 0);
1285 }
1286 return 0;
1287}
1288
1290{
1291 for (int i = 0; i < ctx->nb_streams; i++) {
1292 StreamInfo *stream = ctx->streams[i]->priv_data;
1293 if (!stream)
1294 continue;
1295 for (PacketDesc *pkt = stream->predecode_packet; pkt; ) {
1296 PacketDesc *tmp = pkt->next;
1297 av_free(pkt);
1298 pkt = tmp;
1299 }
1300 av_fifo_freep2(&stream->fifo);
1301 }
1302}
1303
1304#define OFFSET(x) offsetof(MpegMuxContext, x)
1305#define E AV_OPT_FLAG_ENCODING_PARAM
1306static const AVOption options[] = {
1307 { "muxrate", "mux rate as bits/s", OFFSET(user_mux_rate), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, ((1<<22) - 1) * (8 * 50), E },
1308 { "preload", "initial demux-decode delay in microseconds", OFFSET(preload), AV_OPT_TYPE_INT, { .i64 = 500000 }, 0, INT_MAX, E },
1309 { NULL },
1310};
1311
1312static const AVClass mpeg_class = {
1313 .class_name = "mpeg/(s)vcd/vob/dvd muxer",
1314 .item_name = av_default_item_name,
1315 .version = LIBAVUTIL_VERSION_INT,
1316 .option = options,
1317};
1318
1319#if CONFIG_MPEG1SYSTEM_MUXER
1321 .p.name = "mpeg",
1322 .p.long_name = NULL_IF_CONFIG_SMALL("MPEG-1 Systems / MPEG program stream"),
1323 .p.mime_type = "video/mpeg",
1324 .p.extensions = "mpg,mpeg",
1325 .priv_data_size = sizeof(MpegMuxContext),
1326 .p.audio_codec = AV_CODEC_ID_MP2,
1327 .p.video_codec = AV_CODEC_ID_MPEG1VIDEO,
1328 .write_header = mpeg_mux_init,
1329 .write_packet = mpeg_mux_write_packet,
1330 .write_trailer = mpeg_mux_end,
1331 .deinit = mpeg_mux_deinit,
1332 .p.priv_class = &mpeg_class,
1333};
1334#endif
1335
1336#if CONFIG_MPEG1VCD_MUXER
1338 .p.name = "vcd",
1339 .p.long_name = NULL_IF_CONFIG_SMALL("MPEG-1 Systems / MPEG program stream (VCD)"),
1340 .p.mime_type = "video/mpeg",
1341 .priv_data_size = sizeof(MpegMuxContext),
1342 .p.audio_codec = AV_CODEC_ID_MP2,
1343 .p.video_codec = AV_CODEC_ID_MPEG1VIDEO,
1344 .write_header = mpeg_mux_init,
1345 .write_packet = mpeg_mux_write_packet,
1346 .write_trailer = mpeg_mux_end,
1347 .deinit = mpeg_mux_deinit,
1348 .p.priv_class = &mpeg_class,
1349};
1350#endif
1351
1352#if CONFIG_MPEG2VOB_MUXER
1354 .p.name = "vob",
1355 .p.long_name = NULL_IF_CONFIG_SMALL("MPEG-2 PS (VOB)"),
1356 .p.mime_type = "video/mpeg",
1357 .p.extensions = "vob",
1358 .priv_data_size = sizeof(MpegMuxContext),
1359 .p.audio_codec = AV_CODEC_ID_MP2,
1360 .p.video_codec = AV_CODEC_ID_MPEG2VIDEO,
1361 .write_header = mpeg_mux_init,
1362 .write_packet = mpeg_mux_write_packet,
1363 .write_trailer = mpeg_mux_end,
1364 .deinit = mpeg_mux_deinit,
1365 .p.priv_class = &mpeg_class,
1366};
1367#endif
1368
1369/* Same as mpeg2vob_mux except that the pack size is 2324 */
1370#if CONFIG_MPEG2SVCD_MUXER
1372 .p.name = "svcd",
1373 .p.long_name = NULL_IF_CONFIG_SMALL("MPEG-2 PS (SVCD)"),
1374 .p.mime_type = "video/mpeg",
1375 .p.extensions = "vob",
1376 .priv_data_size = sizeof(MpegMuxContext),
1377 .p.audio_codec = AV_CODEC_ID_MP2,
1378 .p.video_codec = AV_CODEC_ID_MPEG2VIDEO,
1379 .write_header = mpeg_mux_init,
1380 .write_packet = mpeg_mux_write_packet,
1381 .write_trailer = mpeg_mux_end,
1382 .deinit = mpeg_mux_deinit,
1383 .p.priv_class = &mpeg_class,
1384};
1385#endif
1386
1387/* Same as mpeg2vob_mux except the 'is_dvd' flag is set to produce NAV pkts */
1388#if CONFIG_MPEG2DVD_MUXER
1390 .p.name = "dvd",
1391 .p.long_name = NULL_IF_CONFIG_SMALL("MPEG-2 PS (DVD VOB)"),
1392 .p.mime_type = "video/mpeg",
1393 .p.extensions = "dvd",
1394 .priv_data_size = sizeof(MpegMuxContext),
1395 .p.audio_codec = AV_CODEC_ID_MP2,
1396 .p.video_codec = AV_CODEC_ID_MPEG2VIDEO,
1397 .write_header = mpeg_mux_init,
1398 .write_packet = mpeg_mux_write_packet,
1399 .write_trailer = mpeg_mux_end,
1400 .deinit = mpeg_mux_deinit,
1401 .p.priv_class = &mpeg_class,
1402};
1403#endif
const FFOutputFormat ff_mpeg2dvd_muxer
Definition mpegenc.c:93
const FFOutputFormat ff_mpeg2vob_muxer
Definition mpegenc.c:95
const FFOutputFormat ff_mpeg2svcd_muxer
Definition mpegenc.c:94
const FFOutputFormat ff_mpeg1system_muxer
const FFOutputFormat ff_mpeg1vcd_muxer
Definition mpegenc.c:92
#define EXTERN
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
#define E
Definition avdct.c:34
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:834
Main libavformat public API header.
void avio_write_marker(AVIOContext *s, int64_t time, enum AVIODataMarkerType type)
Mark the written bytestream as a specific type.
Definition aviobuf.c:464
void avio_w8(AVIOContext *s, int b)
Definition aviobuf.c:184
void avio_wb32(AVIOContext *s, unsigned int val)
Definition aviobuf.c:368
void avio_wb16(AVIOContext *s, unsigned int val)
Definition aviobuf.c:446
@ AVIO_DATA_MARKER_FLUSH_POINT
A point in the output bytestream where the underlying AVIOContext might flush the buffer depending on...
Definition avio.h:145
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition aviobuf.c:206
void ffio_fill(AVIOContext *s, int b, int64_t count)
Definition aviobuf.c:192
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
void(* flush)(AVBSFContext *ctx)
Definition dts2pts.c:610
enum AVCodecID id
Definition dts2pts.c:607
A generic FIFO API.
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
Definition utils.c:421
@ AV_CODEC_ID_PCM_DVD
Definition codec_id.h:349
@ AV_CODEC_ID_H264
Definition codec_id.h:77
@ AV_CODEC_ID_PCM_S16BE
Definition codec_id.h:331
@ AV_CODEC_ID_MP1
Definition codec_id.h:495
@ AV_CODEC_ID_MP2
Definition codec_id.h:453
@ AV_CODEC_ID_DTS
Definition codec_id.h:457
@ AV_CODEC_ID_TRUEHD
Definition codec_id.h:497
@ AV_CODEC_ID_AC3
Definition codec_id.h:456
@ AV_CODEC_ID_MLP
Definition codec_id.h:482
@ AV_CODEC_ID_MP3
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition codec_id.h:454
@ AV_CODEC_ID_MPEG1VIDEO
Definition codec_id.h:51
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
Definition codec_id.h:52
const AVPacketSideData * av_packet_side_data_get(const AVPacketSideData *sd, int nb_sd, enum AVPacketSideDataType type)
Get side information from a side data array.
Definition packet.c:570
@ AV_PKT_DATA_CPB_PROPERTIES
This side data corresponds to the AVCPBProperties struct.
Definition packet.h:142
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:650
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR(e)
Definition error.h:45
AVFifo * av_fifo_alloc2(size_t nb_elems, size_t elem_size, unsigned int flags)
Allocate and initialize an AVFifo with a given element size.
Definition fifo.c:47
void av_fifo_freep2(AVFifo **f)
Free an AVFifo and reset pointer to NULL.
Definition fifo.c:286
size_t av_fifo_can_write(const AVFifo *f)
Definition fifo.c:94
size_t av_fifo_can_read(const AVFifo *f)
Definition fifo.c:87
int av_fifo_read_to_cb(AVFifo *f, AVFifoCB write_cb, void *opaque, size_t *nb_elems)
Feed data from a FIFO into a user-provided callback.
Definition fifo.c:247
int av_fifo_grow2(AVFifo *f, size_t inc)
Enlarge an AVFifo.
Definition fifo.c:99
int av_fifo_write(AVFifo *f, const void *buf, size_t nb_elems)
Write data into a FIFO.
Definition fifo.c:188
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition log.h:236
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
const char * av_get_media_type_string(enum AVMediaType media_type)
Return a string describing the media_type enum, NULL if media_type is unknown.
Definition utils.c:28
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_SUBTITLE
Definition avutil.h:203
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
#define AV_TIME_BASE
Internal time base represented as integer.
Definition avutil.h:253
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
#define AV_WB16(p, v)
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition j2kenc.c:154
Macro definitions for various function/variable attributes.
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define FFMAX(a, b)
Definition macros.h:47
enum AVColorSpace space
Memory handling functions.
#define AUDIO_ID
Definition mpeg.h:41
#define LPCM_ID
Definition mpeg.h:46
#define AC3_ID
Definition mpeg.h:44
#define SYSTEM_HEADER_START_CODE
Definition mpeg.h:29
static const int lpcm_freq_tab[4]
Definition mpeg.h:64
#define PRIVATE_STREAM_2
Definition mpeg.h:39
#define PRIVATE_STREAM_1
Definition mpeg.h:37
#define H264_ID
Definition mpeg.h:43
#define SUB_ID
Definition mpeg.h:47
#define PADDING_STREAM
Definition mpeg.h:38
#define DTS_ID
Definition mpeg.h:45
#define VIDEO_ID
Definition mpeg.h:42
#define PACK_START_CODE
Definition mpeg.h:28
static int get_system_header_size(AVFormatContext *ctx)
Definition mpegenc.c:280
static void put_padding_packet(AVFormatContext *ctx, AVIOContext *pb, int packet_bytes)
Definition mpegenc.c:623
static int get_nb_frames(AVFormatContext *ctx, StreamInfo *stream, int len)
Definition mpegenc.c:639
static int mpeg_mux_end(AVFormatContext *ctx)
Definition mpegenc.c:1263
static int remove_decoded_packets(AVFormatContext *ctx, int64_t scr)
Definition mpegenc.c:993
static int get_vcd_padding_size(AVFormatContext *ctx, int64_t pts)
Definition mpegenc.c:600
static int mpeg_mux_write_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition mpegenc.c:1168
static int put_system_header(AVFormatContext *ctx, uint8_t *buf, int buf_size, int only_for_stream_id)
Definition mpegenc.c:131
static av_cold int mpeg_mux_init(AVFormatContext *ctx)
Definition mpegenc.c:303
static int fifo_avio_wrapper(void *opaque, void *buf, size_t *nb_elems)
Definition mpegenc.c:654
static const AVClass mpeg_class
Definition mpegenc.c:1312
static int output_packet(AVFormatContext *ctx, int flush)
Definition mpegenc.c:1022
static void mpeg_mux_deinit(AVFormatContext *ctx)
Definition mpegenc.c:1289
#define VCD_PADDING_BITRATE_DEN
static void put_timestamp(AVIOContext *pb, int id, int64_t timestamp)
Definition mpegenc.c:591
#define OFFSET(x)
Definition mpegenc.c:1304
static int put_pack_header(AVFormatContext *ctx, uint8_t *buf, int64_t timestamp)
Definition mpegenc.c:97
static int flush_packet(AVFormatContext *ctx, int stream_index, int64_t pts, int64_t dts, int64_t scr, int trailer_size)
Definition mpegenc.c:661
static void put_vcd_padding_sector(AVFormatContext *ctx)
Definition mpegenc.c:970
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
AVOptions.
bitstream writer API
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition put_bits.h:62
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition put_bits.h:153
static int put_bytes_output(const PutBitContext *s)
Definition put_bits.h:99
static av_unused void put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
Definition put_bits.h:301
This structure describes the bitrate properties of an encoded bitstream.
Definition defs.h:282
int64_t max_bitrate
Maximum bitrate of the stream, in bits per second.
Definition defs.h:287
int64_t buffer_size
The size of the buffer to which the ratecontrol is applied, in bits.
Definition defs.h:303
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
int nb_coded_side_data
Amount of entries in coded_side_data.
Definition codec_par.h:88
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition codec_par.h:113
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition codec_par.h:99
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
AVPacketSideData * coded_side_data
Additional data associated with the entire stream.
Definition codec_par.h:83
Definition fifo.c:35
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
AVOption.
Definition opt.h:428
This structure stores auxiliary information for decoding, presenting, or otherwise processing the cod...
Definition packet.h:424
uint8_t * data
Definition packet.h:425
This structure stores compressed data.
Definition packet.h:580
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
void * priv_data
Definition avformat.h:791
int audio_bound
Definition mpegenc.c:78
int64_t last_scr
Definition mpegenc.c:84
int packet_number
Definition mpegenc.c:71
int system_header_size
Definition mpegenc.c:74
int64_t vcd_padding_bytes_written
Definition mpegenc.c:87
int64_t vcd_padding_bitrate_num
Definition mpegenc.c:86
int video_bound
Definition mpegenc.c:79
int system_header_freq
Definition mpegenc.c:73
int pack_header_freq
Definition mpegenc.c:72
int packet_size
Definition mpegenc.c:70
int user_mux_rate
Definition mpegenc.c:75
int unwritten_size
Definition mpegenc.c:48
struct PacketDesc * next
Definition mpegenc.c:49
int64_t pts
Definition mpegenc.c:45
int size
Definition mpegenc.c:47
int64_t dts
Definition mpegenc.c:46
AVFifo * fifo
Definition mpegenc.c:53
int64_t vobu_start_pts
Definition mpegenc.c:65
uint8_t lpcm_header[3]
Definition mpegenc.c:61
PacketDesc * premux_packet
Definition mpegenc.c:59
int buffer_index
Definition mpegenc.c:56
uint8_t id
Definition mpegenc.c:54
int lpcm_align
Definition mpegenc.c:62
int max_buffer_size
Definition mpegenc.c:55
int align_iframe
Definition mpegenc.c:64
PacketDesc * last_packet
Definition mpegenc.c:58
PacketDesc * predecode_packet
Definition mpegenc.c:57
int packet_number
Definition mpegenc.c:60
int bytes_to_iframe
Definition mpegenc.c:63
#define av_free(p)
#define av_mallocz(s)
#define av_freep(p)
#define av_log(a,...)
static uint8_t tmp[40]
Definition aes_ctr.c:52
int64_t bitrate
Definition av1_levels.c:47
static AVFormatContext * ctx
Definition movenc.c:49
static char buffer[20]
Definition seek.c:32
static int64_t pts
int size
int len