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