FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/mpegenc.c
Date: 2022-11-26 13:19:19
Exec Total Coverage
Lines: 499 699 71.4%
Branches: 265 457 58.0%

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