FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/mpegenc.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 507 708 71.6%
Functions: 14 16 87.5%
Branches: 268 461 58.1%

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