Mercurial > libavformat.hg
comparison mpegenc.c @ 2176:50322a49fa2b libavformat
split mpeg ps and variants muxer and demuxer, I'll clean more in a few minutes, lpcm freq tab is left static const in mpeg.h for now until we have more code in common
author | bcoudurier |
---|---|
date | Thu, 21 Jun 2007 09:39:29 +0000 |
parents | mpeg.c@3804e39efbfd |
children | fa17c3c6e546 |
comparison
equal
deleted
inserted
replaced
2175:9438e2e7faf2 | 2176:50322a49fa2b |
---|---|
1 /* | |
2 * MPEG1/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 "avformat.h" | |
23 #include "bitstream.h" | |
24 #include "fifo.h" | |
25 #include "mpeg.h" | |
26 | |
27 #define MAX_PAYLOAD_SIZE 4096 | |
28 //#define DEBUG_SEEK | |
29 | |
30 #undef NDEBUG | |
31 #include <assert.h> | |
32 | |
33 typedef struct PacketDesc { | |
34 int64_t pts; | |
35 int64_t dts; | |
36 int size; | |
37 int unwritten_size; | |
38 int flags; | |
39 struct PacketDesc *next; | |
40 } PacketDesc; | |
41 | |
42 typedef struct { | |
43 AVFifoBuffer fifo; | |
44 uint8_t id; | |
45 int max_buffer_size; /* in bytes */ | |
46 int buffer_index; | |
47 PacketDesc *predecode_packet; | |
48 PacketDesc *premux_packet; | |
49 PacketDesc **next_packet; | |
50 int packet_number; | |
51 uint8_t lpcm_header[3]; | |
52 int lpcm_align; | |
53 int bytes_to_iframe; | |
54 int align_iframe; | |
55 int64_t vobu_start_pts; | |
56 } StreamInfo; | |
57 | |
58 typedef struct { | |
59 int packet_size; /* required packet size */ | |
60 int packet_number; | |
61 int pack_header_freq; /* frequency (in packets^-1) at which we send pack headers */ | |
62 int system_header_freq; | |
63 int system_header_size; | |
64 int mux_rate; /* bitrate in units of 50 bytes/s */ | |
65 /* stream info */ | |
66 int audio_bound; | |
67 int video_bound; | |
68 int is_mpeg2; | |
69 int is_vcd; | |
70 int is_svcd; | |
71 int is_dvd; | |
72 int64_t last_scr; /* current system clock */ | |
73 | |
74 double vcd_padding_bitrate; //FIXME floats | |
75 int64_t vcd_padding_bytes_written; | |
76 | |
77 } MpegMuxContext; | |
78 | |
79 #ifdef CONFIG_MUXERS | |
80 AVOutputFormat mpeg1system_muxer; | |
81 AVOutputFormat mpeg1vcd_muxer; | |
82 AVOutputFormat mpeg2vob_muxer; | |
83 AVOutputFormat mpeg2svcd_muxer; | |
84 AVOutputFormat mpeg2dvd_muxer; | |
85 | |
86 static int put_pack_header(AVFormatContext *ctx, | |
87 uint8_t *buf, int64_t timestamp) | |
88 { | |
89 MpegMuxContext *s = ctx->priv_data; | |
90 PutBitContext pb; | |
91 | |
92 init_put_bits(&pb, buf, 128); | |
93 | |
94 put_bits(&pb, 32, PACK_START_CODE); | |
95 if (s->is_mpeg2) { | |
96 put_bits(&pb, 2, 0x1); | |
97 } else { | |
98 put_bits(&pb, 4, 0x2); | |
99 } | |
100 put_bits(&pb, 3, (uint32_t)((timestamp >> 30) & 0x07)); | |
101 put_bits(&pb, 1, 1); | |
102 put_bits(&pb, 15, (uint32_t)((timestamp >> 15) & 0x7fff)); | |
103 put_bits(&pb, 1, 1); | |
104 put_bits(&pb, 15, (uint32_t)((timestamp) & 0x7fff)); | |
105 put_bits(&pb, 1, 1); | |
106 if (s->is_mpeg2) { | |
107 /* clock extension */ | |
108 put_bits(&pb, 9, 0); | |
109 } | |
110 put_bits(&pb, 1, 1); | |
111 put_bits(&pb, 22, s->mux_rate); | |
112 put_bits(&pb, 1, 1); | |
113 if (s->is_mpeg2) { | |
114 put_bits(&pb, 1, 1); | |
115 put_bits(&pb, 5, 0x1f); /* reserved */ | |
116 put_bits(&pb, 3, 0); /* stuffing length */ | |
117 } | |
118 flush_put_bits(&pb); | |
119 return pbBufPtr(&pb) - pb.buf; | |
120 } | |
121 | |
122 static int put_system_header(AVFormatContext *ctx, uint8_t *buf,int only_for_stream_id) | |
123 { | |
124 MpegMuxContext *s = ctx->priv_data; | |
125 int size, i, private_stream_coded, id; | |
126 PutBitContext pb; | |
127 | |
128 init_put_bits(&pb, buf, 128); | |
129 | |
130 put_bits(&pb, 32, SYSTEM_HEADER_START_CODE); | |
131 put_bits(&pb, 16, 0); | |
132 put_bits(&pb, 1, 1); | |
133 | |
134 put_bits(&pb, 22, s->mux_rate); /* maximum bit rate of the multiplexed stream */ | |
135 put_bits(&pb, 1, 1); /* marker */ | |
136 if (s->is_vcd && only_for_stream_id==VIDEO_ID) { | |
137 /* This header applies only to the video stream (see VCD standard p. IV-7)*/ | |
138 put_bits(&pb, 6, 0); | |
139 } else | |
140 put_bits(&pb, 6, s->audio_bound); | |
141 | |
142 if (s->is_vcd) { | |
143 /* see VCD standard, p. IV-7*/ | |
144 put_bits(&pb, 1, 0); | |
145 put_bits(&pb, 1, 1); | |
146 } else { | |
147 put_bits(&pb, 1, 0); /* variable bitrate*/ | |
148 put_bits(&pb, 1, 0); /* non constrainted bit stream */ | |
149 } | |
150 | |
151 if (s->is_vcd || s->is_dvd) { | |
152 /* see VCD standard p IV-7 */ | |
153 put_bits(&pb, 1, 1); /* audio locked */ | |
154 put_bits(&pb, 1, 1); /* video locked */ | |
155 } else { | |
156 put_bits(&pb, 1, 0); /* audio locked */ | |
157 put_bits(&pb, 1, 0); /* video locked */ | |
158 } | |
159 | |
160 put_bits(&pb, 1, 1); /* marker */ | |
161 | |
162 if (s->is_vcd && only_for_stream_id==AUDIO_ID) { | |
163 /* This header applies only to the audio stream (see VCD standard p. IV-7)*/ | |
164 put_bits(&pb, 5, 0); | |
165 } else | |
166 put_bits(&pb, 5, s->video_bound); | |
167 | |
168 if (s->is_dvd) { | |
169 put_bits(&pb, 1, 0); /* packet_rate_restriction_flag */ | |
170 put_bits(&pb, 7, 0x7f); /* reserved byte */ | |
171 } else | |
172 put_bits(&pb, 8, 0xff); /* reserved byte */ | |
173 | |
174 /* DVD-Video Stream_bound entries | |
175 id (0xB9) video, maximum P-STD for stream 0xE0. (P-STD_buffer_bound_scale = 1) | |
176 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) | |
177 id (0xBD) private stream 1 (audio other than MPEG and subpictures). (P-STD_buffer_bound_scale = 1) | |
178 id (0xBF) private stream 2, NAV packs, set to 2x1024. */ | |
179 if (s->is_dvd) { | |
180 | |
181 int P_STD_max_video = 0; | |
182 int P_STD_max_mpeg_audio = 0; | |
183 int P_STD_max_mpeg_PS1 = 0; | |
184 | |
185 for(i=0;i<ctx->nb_streams;i++) { | |
186 StreamInfo *stream = ctx->streams[i]->priv_data; | |
187 | |
188 id = stream->id; | |
189 if (id == 0xbd && stream->max_buffer_size > P_STD_max_mpeg_PS1) { | |
190 P_STD_max_mpeg_PS1 = stream->max_buffer_size; | |
191 } else if (id >= 0xc0 && id <= 0xc7 && stream->max_buffer_size > P_STD_max_mpeg_audio) { | |
192 P_STD_max_mpeg_audio = stream->max_buffer_size; | |
193 } else if (id == 0xe0 && stream->max_buffer_size > P_STD_max_video) { | |
194 P_STD_max_video = stream->max_buffer_size; | |
195 } | |
196 } | |
197 | |
198 /* video */ | |
199 put_bits(&pb, 8, 0xb9); /* stream ID */ | |
200 put_bits(&pb, 2, 3); | |
201 put_bits(&pb, 1, 1); | |
202 put_bits(&pb, 13, P_STD_max_video / 1024); | |
203 | |
204 /* audio */ | |
205 if (P_STD_max_mpeg_audio == 0) | |
206 P_STD_max_mpeg_audio = 4096; | |
207 put_bits(&pb, 8, 0xb8); /* stream ID */ | |
208 put_bits(&pb, 2, 3); | |
209 put_bits(&pb, 1, 0); | |
210 put_bits(&pb, 13, P_STD_max_mpeg_audio / 128); | |
211 | |
212 /* private stream 1 */ | |
213 put_bits(&pb, 8, 0xbd); /* stream ID */ | |
214 put_bits(&pb, 2, 3); | |
215 put_bits(&pb, 1, 0); | |
216 put_bits(&pb, 13, P_STD_max_mpeg_PS1 / 128); | |
217 | |
218 /* private stream 2 */ | |
219 put_bits(&pb, 8, 0xbf); /* stream ID */ | |
220 put_bits(&pb, 2, 3); | |
221 put_bits(&pb, 1, 1); | |
222 put_bits(&pb, 13, 2); | |
223 } | |
224 else { | |
225 /* audio stream info */ | |
226 private_stream_coded = 0; | |
227 for(i=0;i<ctx->nb_streams;i++) { | |
228 StreamInfo *stream = ctx->streams[i]->priv_data; | |
229 | |
230 | |
231 /* For VCDs, only include the stream info for the stream | |
232 that the pack which contains this system belongs to. | |
233 (see VCD standard p. IV-7) */ | |
234 if ( !s->is_vcd || stream->id==only_for_stream_id | |
235 || only_for_stream_id==0) { | |
236 | |
237 id = stream->id; | |
238 if (id < 0xc0) { | |
239 /* special case for private streams (AC3 use that) */ | |
240 if (private_stream_coded) | |
241 continue; | |
242 private_stream_coded = 1; | |
243 id = 0xbd; | |
244 } | |
245 put_bits(&pb, 8, id); /* stream ID */ | |
246 put_bits(&pb, 2, 3); | |
247 if (id < 0xe0) { | |
248 /* audio */ | |
249 put_bits(&pb, 1, 0); | |
250 put_bits(&pb, 13, stream->max_buffer_size / 128); | |
251 } else { | |
252 /* video */ | |
253 put_bits(&pb, 1, 1); | |
254 put_bits(&pb, 13, stream->max_buffer_size / 1024); | |
255 } | |
256 } | |
257 } | |
258 } | |
259 | |
260 flush_put_bits(&pb); | |
261 size = pbBufPtr(&pb) - pb.buf; | |
262 /* patch packet size */ | |
263 buf[4] = (size - 6) >> 8; | |
264 buf[5] = (size - 6) & 0xff; | |
265 | |
266 return size; | |
267 } | |
268 | |
269 static int get_system_header_size(AVFormatContext *ctx) | |
270 { | |
271 int buf_index, i, private_stream_coded; | |
272 StreamInfo *stream; | |
273 MpegMuxContext *s = ctx->priv_data; | |
274 | |
275 if (s->is_dvd) | |
276 return 18; // DVD-Video system headers are 18 bytes fixed length. | |
277 | |
278 buf_index = 12; | |
279 private_stream_coded = 0; | |
280 for(i=0;i<ctx->nb_streams;i++) { | |
281 stream = ctx->streams[i]->priv_data; | |
282 if (stream->id < 0xc0) { | |
283 if (private_stream_coded) | |
284 continue; | |
285 private_stream_coded = 1; | |
286 } | |
287 buf_index += 3; | |
288 } | |
289 return buf_index; | |
290 } | |
291 | |
292 static int mpeg_mux_init(AVFormatContext *ctx) | |
293 { | |
294 MpegMuxContext *s = ctx->priv_data; | |
295 int bitrate, i, mpa_id, mpv_id, mps_id, ac3_id, dts_id, lpcm_id, j; | |
296 AVStream *st; | |
297 StreamInfo *stream; | |
298 int audio_bitrate; | |
299 int video_bitrate; | |
300 | |
301 s->packet_number = 0; | |
302 s->is_vcd = (ctx->oformat == &mpeg1vcd_muxer); | |
303 s->is_svcd = (ctx->oformat == &mpeg2svcd_muxer); | |
304 s->is_mpeg2 = (ctx->oformat == &mpeg2vob_muxer || ctx->oformat == &mpeg2svcd_muxer || ctx->oformat == &mpeg2dvd_muxer); | |
305 s->is_dvd = (ctx->oformat == &mpeg2dvd_muxer); | |
306 | |
307 if(ctx->packet_size) | |
308 s->packet_size = ctx->packet_size; | |
309 else | |
310 s->packet_size = 2048; | |
311 | |
312 s->vcd_padding_bytes_written = 0; | |
313 s->vcd_padding_bitrate=0; | |
314 | |
315 s->audio_bound = 0; | |
316 s->video_bound = 0; | |
317 mpa_id = AUDIO_ID; | |
318 ac3_id = AC3_ID; | |
319 dts_id = DTS_ID; | |
320 mpv_id = VIDEO_ID; | |
321 mps_id = SUB_ID; | |
322 lpcm_id = LPCM_ID; | |
323 for(i=0;i<ctx->nb_streams;i++) { | |
324 st = ctx->streams[i]; | |
325 stream = av_mallocz(sizeof(StreamInfo)); | |
326 if (!stream) | |
327 goto fail; | |
328 st->priv_data = stream; | |
329 | |
330 av_set_pts_info(st, 64, 1, 90000); | |
331 | |
332 switch(st->codec->codec_type) { | |
333 case CODEC_TYPE_AUDIO: | |
334 if (st->codec->codec_id == CODEC_ID_AC3) { | |
335 stream->id = ac3_id++; | |
336 } else if (st->codec->codec_id == CODEC_ID_DTS) { | |
337 stream->id = dts_id++; | |
338 } else if (st->codec->codec_id == CODEC_ID_PCM_S16BE) { | |
339 stream->id = lpcm_id++; | |
340 for(j = 0; j < 4; j++) { | |
341 if (lpcm_freq_tab[j] == st->codec->sample_rate) | |
342 break; | |
343 } | |
344 if (j == 4) | |
345 goto fail; | |
346 if (st->codec->channels > 8) | |
347 return -1; | |
348 stream->lpcm_header[0] = 0x0c; | |
349 stream->lpcm_header[1] = (st->codec->channels - 1) | (j << 4); | |
350 stream->lpcm_header[2] = 0x80; | |
351 stream->lpcm_align = st->codec->channels * 2; | |
352 } else { | |
353 stream->id = mpa_id++; | |
354 } | |
355 | |
356 /* This value HAS to be used for VCD (see VCD standard, p. IV-7). | |
357 Right now it is also used for everything else.*/ | |
358 stream->max_buffer_size = 4 * 1024; | |
359 s->audio_bound++; | |
360 break; | |
361 case CODEC_TYPE_VIDEO: | |
362 stream->id = mpv_id++; | |
363 if (st->codec->rc_buffer_size) | |
364 stream->max_buffer_size = 6*1024 + st->codec->rc_buffer_size/8; | |
365 else | |
366 stream->max_buffer_size = 230*1024; //FIXME this is probably too small as default | |
367 #if 0 | |
368 /* see VCD standard, p. IV-7*/ | |
369 stream->max_buffer_size = 46 * 1024; | |
370 else | |
371 /* This value HAS to be used for SVCD (see SVCD standard, p. 26 V.2.3.2). | |
372 Right now it is also used for everything else.*/ | |
373 stream->max_buffer_size = 230 * 1024; | |
374 #endif | |
375 s->video_bound++; | |
376 break; | |
377 case CODEC_TYPE_SUBTITLE: | |
378 stream->id = mps_id++; | |
379 stream->max_buffer_size = 16 * 1024; | |
380 break; | |
381 default: | |
382 return -1; | |
383 } | |
384 av_fifo_init(&stream->fifo, 16); | |
385 } | |
386 bitrate = 0; | |
387 audio_bitrate = 0; | |
388 video_bitrate = 0; | |
389 for(i=0;i<ctx->nb_streams;i++) { | |
390 int codec_rate; | |
391 st = ctx->streams[i]; | |
392 stream = (StreamInfo*) st->priv_data; | |
393 | |
394 if(st->codec->rc_max_rate || stream->id==VIDEO_ID) | |
395 codec_rate= st->codec->rc_max_rate; | |
396 else | |
397 codec_rate= st->codec->bit_rate; | |
398 | |
399 if(!codec_rate) | |
400 codec_rate= (1<<21)*8*50/ctx->nb_streams; | |
401 | |
402 bitrate += codec_rate; | |
403 | |
404 if (stream->id==AUDIO_ID) | |
405 audio_bitrate += codec_rate; | |
406 else if (stream->id==VIDEO_ID) | |
407 video_bitrate += codec_rate; | |
408 } | |
409 | |
410 if(ctx->mux_rate){ | |
411 s->mux_rate= (ctx->mux_rate + (8 * 50) - 1) / (8 * 50); | |
412 } else { | |
413 /* we increase slightly the bitrate to take into account the | |
414 headers. XXX: compute it exactly */ | |
415 bitrate += bitrate*5/100; | |
416 bitrate += 10000; | |
417 s->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50); | |
418 } | |
419 | |
420 if (s->is_vcd) { | |
421 double overhead_rate; | |
422 | |
423 /* The VCD standard mandates that the mux_rate field is 3528 | |
424 (see standard p. IV-6). | |
425 The value is actually "wrong", i.e. if you calculate | |
426 it using the normal formula and the 75 sectors per second transfer | |
427 rate you get a different value because the real pack size is 2324, | |
428 not 2352. But the standard explicitly specifies that the mux_rate | |
429 field in the header must have this value.*/ | |
430 // s->mux_rate=2352 * 75 / 50; /* = 3528*/ | |
431 | |
432 /* The VCD standard states that the muxed stream must be | |
433 exactly 75 packs / second (the data rate of a single speed cdrom). | |
434 Since the video bitrate (probably 1150000 bits/sec) will be below | |
435 the theoretical maximum we have to add some padding packets | |
436 to make up for the lower data rate. | |
437 (cf. VCD standard p. IV-6 )*/ | |
438 | |
439 /* Add the header overhead to the data rate. | |
440 2279 data bytes per audio pack, 2294 data bytes per video pack*/ | |
441 overhead_rate = ((audio_bitrate / 8.0) / 2279) * (2324 - 2279); | |
442 overhead_rate += ((video_bitrate / 8.0) / 2294) * (2324 - 2294); | |
443 overhead_rate *= 8; | |
444 | |
445 /* Add padding so that the full bitrate is 2324*75 bytes/sec */ | |
446 s->vcd_padding_bitrate = 2324 * 75 * 8 - (bitrate + overhead_rate); | |
447 } | |
448 | |
449 if (s->is_vcd || s->is_mpeg2) | |
450 /* every packet */ | |
451 s->pack_header_freq = 1; | |
452 else | |
453 /* every 2 seconds */ | |
454 s->pack_header_freq = 2 * bitrate / s->packet_size / 8; | |
455 | |
456 /* the above seems to make pack_header_freq zero sometimes */ | |
457 if (s->pack_header_freq == 0) | |
458 s->pack_header_freq = 1; | |
459 | |
460 if (s->is_mpeg2) | |
461 /* every 200 packets. Need to look at the spec. */ | |
462 s->system_header_freq = s->pack_header_freq * 40; | |
463 else if (s->is_vcd) | |
464 /* the standard mandates that there are only two system headers | |
465 in the whole file: one in the first packet of each stream. | |
466 (see standard p. IV-7 and IV-8) */ | |
467 s->system_header_freq = 0x7fffffff; | |
468 else | |
469 s->system_header_freq = s->pack_header_freq * 5; | |
470 | |
471 for(i=0;i<ctx->nb_streams;i++) { | |
472 stream = ctx->streams[i]->priv_data; | |
473 stream->packet_number = 0; | |
474 } | |
475 s->system_header_size = get_system_header_size(ctx); | |
476 s->last_scr = 0; | |
477 return 0; | |
478 fail: | |
479 for(i=0;i<ctx->nb_streams;i++) { | |
480 av_free(ctx->streams[i]->priv_data); | |
481 } | |
482 return AVERROR(ENOMEM); | |
483 } | |
484 | |
485 static inline void put_timestamp(ByteIOContext *pb, int id, int64_t timestamp) | |
486 { | |
487 put_byte(pb, | |
488 (id << 4) | | |
489 (((timestamp >> 30) & 0x07) << 1) | | |
490 1); | |
491 put_be16(pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1)); | |
492 put_be16(pb, (uint16_t)((((timestamp) & 0x7fff) << 1) | 1)); | |
493 } | |
494 | |
495 | |
496 /* return the number of padding bytes that should be inserted into | |
497 the multiplexed stream.*/ | |
498 static int get_vcd_padding_size(AVFormatContext *ctx, int64_t pts) | |
499 { | |
500 MpegMuxContext *s = ctx->priv_data; | |
501 int pad_bytes = 0; | |
502 | |
503 if (s->vcd_padding_bitrate > 0 && pts!=AV_NOPTS_VALUE) | |
504 { | |
505 int64_t full_pad_bytes; | |
506 | |
507 full_pad_bytes = (int64_t)((s->vcd_padding_bitrate * (pts / 90000.0)) / 8.0); //FIXME this is wrong | |
508 pad_bytes = (int) (full_pad_bytes - s->vcd_padding_bytes_written); | |
509 | |
510 if (pad_bytes<0) | |
511 /* might happen if we have already padded to a later timestamp. This | |
512 can occur if another stream has already advanced further.*/ | |
513 pad_bytes=0; | |
514 } | |
515 | |
516 return pad_bytes; | |
517 } | |
518 | |
519 | |
520 #if 0 /* unused, remove? */ | |
521 /* return the exact available payload size for the next packet for | |
522 stream 'stream_index'. 'pts' and 'dts' are only used to know if | |
523 timestamps are needed in the packet header. */ | |
524 static int get_packet_payload_size(AVFormatContext *ctx, int stream_index, | |
525 int64_t pts, int64_t dts) | |
526 { | |
527 MpegMuxContext *s = ctx->priv_data; | |
528 int buf_index; | |
529 StreamInfo *stream; | |
530 | |
531 stream = ctx->streams[stream_index]->priv_data; | |
532 | |
533 buf_index = 0; | |
534 if (((s->packet_number % s->pack_header_freq) == 0)) { | |
535 /* pack header size */ | |
536 if (s->is_mpeg2) | |
537 buf_index += 14; | |
538 else | |
539 buf_index += 12; | |
540 | |
541 if (s->is_vcd) { | |
542 /* there is exactly one system header for each stream in a VCD MPEG, | |
543 One in the very first video packet and one in the very first | |
544 audio packet (see VCD standard p. IV-7 and IV-8).*/ | |
545 | |
546 if (stream->packet_number==0) | |
547 /* The system headers refer only to the stream they occur in, | |
548 so they have a constant size.*/ | |
549 buf_index += 15; | |
550 | |
551 } else { | |
552 if ((s->packet_number % s->system_header_freq) == 0) | |
553 buf_index += s->system_header_size; | |
554 } | |
555 } | |
556 | |
557 if ((s->is_vcd && stream->packet_number==0) | |
558 || (s->is_svcd && s->packet_number==0)) | |
559 /* the first pack of each stream contains only the pack header, | |
560 the system header and some padding (see VCD standard p. IV-6) | |
561 Add the padding size, so that the actual payload becomes 0.*/ | |
562 buf_index += s->packet_size - buf_index; | |
563 else { | |
564 /* packet header size */ | |
565 buf_index += 6; | |
566 if (s->is_mpeg2) { | |
567 buf_index += 3; | |
568 if (stream->packet_number==0) | |
569 buf_index += 3; /* PES extension */ | |
570 buf_index += 1; /* obligatory stuffing byte */ | |
571 } | |
572 if (pts != AV_NOPTS_VALUE) { | |
573 if (dts != pts) | |
574 buf_index += 5 + 5; | |
575 else | |
576 buf_index += 5; | |
577 | |
578 } else { | |
579 if (!s->is_mpeg2) | |
580 buf_index++; | |
581 } | |
582 | |
583 if (stream->id < 0xc0) { | |
584 /* AC3/LPCM private data header */ | |
585 buf_index += 4; | |
586 if (stream->id >= 0xa0) { | |
587 int n; | |
588 buf_index += 3; | |
589 /* NOTE: we round the payload size to an integer number of | |
590 LPCM samples */ | |
591 n = (s->packet_size - buf_index) % stream->lpcm_align; | |
592 if (n) | |
593 buf_index += (stream->lpcm_align - n); | |
594 } | |
595 } | |
596 | |
597 if (s->is_vcd && stream->id == AUDIO_ID) | |
598 /* The VCD standard demands that 20 zero bytes follow | |
599 each audio packet (see standard p. IV-8).*/ | |
600 buf_index+=20; | |
601 } | |
602 return s->packet_size - buf_index; | |
603 } | |
604 #endif | |
605 | |
606 /* Write an MPEG padding packet header. */ | |
607 static void put_padding_packet(AVFormatContext *ctx, ByteIOContext *pb,int packet_bytes) | |
608 { | |
609 MpegMuxContext *s = ctx->priv_data; | |
610 int i; | |
611 | |
612 put_be32(pb, PADDING_STREAM); | |
613 put_be16(pb, packet_bytes - 6); | |
614 if (!s->is_mpeg2) { | |
615 put_byte(pb, 0x0f); | |
616 packet_bytes -= 7; | |
617 } else | |
618 packet_bytes -= 6; | |
619 | |
620 for(i=0;i<packet_bytes;i++) | |
621 put_byte(pb, 0xff); | |
622 } | |
623 | |
624 static int get_nb_frames(AVFormatContext *ctx, StreamInfo *stream, int len){ | |
625 int nb_frames=0; | |
626 PacketDesc *pkt_desc= stream->premux_packet; | |
627 | |
628 while(len>0){ | |
629 if(pkt_desc->size == pkt_desc->unwritten_size) | |
630 nb_frames++; | |
631 len -= pkt_desc->unwritten_size; | |
632 pkt_desc= pkt_desc->next; | |
633 } | |
634 | |
635 return nb_frames; | |
636 } | |
637 | |
638 /* flush the packet on stream stream_index */ | |
639 static int flush_packet(AVFormatContext *ctx, int stream_index, | |
640 int64_t pts, int64_t dts, int64_t scr, int trailer_size) | |
641 { | |
642 MpegMuxContext *s = ctx->priv_data; | |
643 StreamInfo *stream = ctx->streams[stream_index]->priv_data; | |
644 uint8_t *buf_ptr; | |
645 int size, payload_size, startcode, id, stuffing_size, i, header_len; | |
646 int packet_size; | |
647 uint8_t buffer[128]; | |
648 int zero_trail_bytes = 0; | |
649 int pad_packet_bytes = 0; | |
650 int pes_flags; | |
651 int general_pack = 0; /*"general" pack without data specific to one stream?*/ | |
652 int nb_frames; | |
653 | |
654 id = stream->id; | |
655 | |
656 #if 0 | |
657 printf("packet ID=%2x PTS=%0.3f\n", | |
658 id, pts / 90000.0); | |
659 #endif | |
660 | |
661 buf_ptr = buffer; | |
662 | |
663 if ((s->packet_number % s->pack_header_freq) == 0 || s->last_scr != scr) { | |
664 /* output pack and systems header if needed */ | |
665 size = put_pack_header(ctx, buf_ptr, scr); | |
666 buf_ptr += size; | |
667 s->last_scr= scr; | |
668 | |
669 if (s->is_vcd) { | |
670 /* there is exactly one system header for each stream in a VCD MPEG, | |
671 One in the very first video packet and one in the very first | |
672 audio packet (see VCD standard p. IV-7 and IV-8).*/ | |
673 | |
674 if (stream->packet_number==0) { | |
675 size = put_system_header(ctx, buf_ptr, id); | |
676 buf_ptr += size; | |
677 } | |
678 } else if (s->is_dvd) { | |
679 if (stream->align_iframe || s->packet_number == 0){ | |
680 int PES_bytes_to_fill = s->packet_size - size - 10; | |
681 | |
682 if (pts != AV_NOPTS_VALUE) { | |
683 if (dts != pts) | |
684 PES_bytes_to_fill -= 5 + 5; | |
685 else | |
686 PES_bytes_to_fill -= 5; | |
687 } | |
688 | |
689 if (stream->bytes_to_iframe == 0 || s->packet_number == 0) { | |
690 size = put_system_header(ctx, buf_ptr, 0); | |
691 buf_ptr += size; | |
692 size = buf_ptr - buffer; | |
693 put_buffer(&ctx->pb, buffer, size); | |
694 | |
695 put_be32(&ctx->pb, PRIVATE_STREAM_2); | |
696 put_be16(&ctx->pb, 0x03d4); // length | |
697 put_byte(&ctx->pb, 0x00); // substream ID, 00=PCI | |
698 for (i = 0; i < 979; i++) | |
699 put_byte(&ctx->pb, 0x00); | |
700 | |
701 put_be32(&ctx->pb, PRIVATE_STREAM_2); | |
702 put_be16(&ctx->pb, 0x03fa); // length | |
703 put_byte(&ctx->pb, 0x01); // substream ID, 01=DSI | |
704 for (i = 0; i < 1017; i++) | |
705 put_byte(&ctx->pb, 0x00); | |
706 | |
707 memset(buffer, 0, 128); | |
708 buf_ptr = buffer; | |
709 s->packet_number++; | |
710 stream->align_iframe = 0; | |
711 scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet | |
712 size = put_pack_header(ctx, buf_ptr, scr); | |
713 s->last_scr= scr; | |
714 buf_ptr += size; | |
715 /* GOP Start */ | |
716 } else if (stream->bytes_to_iframe < PES_bytes_to_fill) { | |
717 pad_packet_bytes = PES_bytes_to_fill - stream->bytes_to_iframe; | |
718 } | |
719 } | |
720 } else { | |
721 if ((s->packet_number % s->system_header_freq) == 0) { | |
722 size = put_system_header(ctx, buf_ptr, 0); | |
723 buf_ptr += size; | |
724 } | |
725 } | |
726 } | |
727 size = buf_ptr - buffer; | |
728 put_buffer(&ctx->pb, buffer, size); | |
729 | |
730 packet_size = s->packet_size - size; | |
731 | |
732 if (s->is_vcd && id == AUDIO_ID) | |
733 /* The VCD standard demands that 20 zero bytes follow | |
734 each audio pack (see standard p. IV-8).*/ | |
735 zero_trail_bytes += 20; | |
736 | |
737 if ((s->is_vcd && stream->packet_number==0) | |
738 || (s->is_svcd && s->packet_number==0)) { | |
739 /* for VCD the first pack of each stream contains only the pack header, | |
740 the system header and lots of padding (see VCD standard p. IV-6). | |
741 In the case of an audio pack, 20 zero bytes are also added at | |
742 the end.*/ | |
743 /* For SVCD we fill the very first pack to increase compatibility with | |
744 some DVD players. Not mandated by the standard.*/ | |
745 if (s->is_svcd) | |
746 general_pack = 1; /* the system header refers to both streams and no stream data*/ | |
747 pad_packet_bytes = packet_size - zero_trail_bytes; | |
748 } | |
749 | |
750 packet_size -= pad_packet_bytes + zero_trail_bytes; | |
751 | |
752 if (packet_size > 0) { | |
753 | |
754 /* packet header size */ | |
755 packet_size -= 6; | |
756 | |
757 /* packet header */ | |
758 if (s->is_mpeg2) { | |
759 header_len = 3; | |
760 if (stream->packet_number==0) | |
761 header_len += 3; /* PES extension */ | |
762 header_len += 1; /* obligatory stuffing byte */ | |
763 } else { | |
764 header_len = 0; | |
765 } | |
766 if (pts != AV_NOPTS_VALUE) { | |
767 if (dts != pts) | |
768 header_len += 5 + 5; | |
769 else | |
770 header_len += 5; | |
771 } else { | |
772 if (!s->is_mpeg2) | |
773 header_len++; | |
774 } | |
775 | |
776 payload_size = packet_size - header_len; | |
777 if (id < 0xc0) { | |
778 startcode = PRIVATE_STREAM_1; | |
779 payload_size -= 1; | |
780 if (id >= 0x40) { | |
781 payload_size -= 3; | |
782 if (id >= 0xa0) | |
783 payload_size -= 3; | |
784 } | |
785 } else { | |
786 startcode = 0x100 + id; | |
787 } | |
788 | |
789 stuffing_size = payload_size - av_fifo_size(&stream->fifo); | |
790 | |
791 // first byte does not fit -> reset pts/dts + stuffing | |
792 if(payload_size <= trailer_size && pts != AV_NOPTS_VALUE){ | |
793 int timestamp_len=0; | |
794 if(dts != pts) | |
795 timestamp_len += 5; | |
796 if(pts != AV_NOPTS_VALUE) | |
797 timestamp_len += s->is_mpeg2 ? 5 : 4; | |
798 pts=dts= AV_NOPTS_VALUE; | |
799 header_len -= timestamp_len; | |
800 if (s->is_dvd && stream->align_iframe) { | |
801 pad_packet_bytes += timestamp_len; | |
802 packet_size -= timestamp_len; | |
803 } else { | |
804 payload_size += timestamp_len; | |
805 } | |
806 stuffing_size += timestamp_len; | |
807 if(payload_size > trailer_size) | |
808 stuffing_size += payload_size - trailer_size; | |
809 } | |
810 | |
811 if (pad_packet_bytes > 0 && pad_packet_bytes <= 7) { // can't use padding, so use stuffing | |
812 packet_size += pad_packet_bytes; | |
813 payload_size += pad_packet_bytes; // undo the previous adjustment | |
814 if (stuffing_size < 0) { | |
815 stuffing_size = pad_packet_bytes; | |
816 } else { | |
817 stuffing_size += pad_packet_bytes; | |
818 } | |
819 pad_packet_bytes = 0; | |
820 } | |
821 | |
822 if (stuffing_size < 0) | |
823 stuffing_size = 0; | |
824 if (stuffing_size > 16) { /*<=16 for MPEG-1, <=32 for MPEG-2*/ | |
825 pad_packet_bytes += stuffing_size; | |
826 packet_size -= stuffing_size; | |
827 payload_size -= stuffing_size; | |
828 stuffing_size = 0; | |
829 } | |
830 | |
831 nb_frames= get_nb_frames(ctx, stream, payload_size - stuffing_size); | |
832 | |
833 put_be32(&ctx->pb, startcode); | |
834 | |
835 put_be16(&ctx->pb, packet_size); | |
836 | |
837 if (!s->is_mpeg2) | |
838 for(i=0;i<stuffing_size;i++) | |
839 put_byte(&ctx->pb, 0xff); | |
840 | |
841 if (s->is_mpeg2) { | |
842 put_byte(&ctx->pb, 0x80); /* mpeg2 id */ | |
843 | |
844 pes_flags=0; | |
845 | |
846 if (pts != AV_NOPTS_VALUE) { | |
847 pes_flags |= 0x80; | |
848 if (dts != pts) | |
849 pes_flags |= 0x40; | |
850 } | |
851 | |
852 /* Both the MPEG-2 and the SVCD standards demand that the | |
853 P-STD_buffer_size field be included in the first packet of | |
854 every stream. (see SVCD standard p. 26 V.2.3.1 and V.2.3.2 | |
855 and MPEG-2 standard 2.7.7) */ | |
856 if (stream->packet_number == 0) | |
857 pes_flags |= 0x01; | |
858 | |
859 put_byte(&ctx->pb, pes_flags); /* flags */ | |
860 put_byte(&ctx->pb, header_len - 3 + stuffing_size); | |
861 | |
862 if (pes_flags & 0x80) /*write pts*/ | |
863 put_timestamp(&ctx->pb, (pes_flags & 0x40) ? 0x03 : 0x02, pts); | |
864 if (pes_flags & 0x40) /*write dts*/ | |
865 put_timestamp(&ctx->pb, 0x01, dts); | |
866 | |
867 if (pes_flags & 0x01) { /*write pes extension*/ | |
868 put_byte(&ctx->pb, 0x10); /* flags */ | |
869 | |
870 /* P-STD buffer info */ | |
871 if (id == AUDIO_ID) | |
872 put_be16(&ctx->pb, 0x4000 | stream->max_buffer_size/128); | |
873 else | |
874 put_be16(&ctx->pb, 0x6000 | stream->max_buffer_size/1024); | |
875 } | |
876 | |
877 } else { | |
878 if (pts != AV_NOPTS_VALUE) { | |
879 if (dts != pts) { | |
880 put_timestamp(&ctx->pb, 0x03, pts); | |
881 put_timestamp(&ctx->pb, 0x01, dts); | |
882 } else { | |
883 put_timestamp(&ctx->pb, 0x02, pts); | |
884 } | |
885 } else { | |
886 put_byte(&ctx->pb, 0x0f); | |
887 } | |
888 } | |
889 | |
890 if (s->is_mpeg2) { | |
891 /* special stuffing byte that is always written | |
892 to prevent accidental generation of start codes. */ | |
893 put_byte(&ctx->pb, 0xff); | |
894 | |
895 for(i=0;i<stuffing_size;i++) | |
896 put_byte(&ctx->pb, 0xff); | |
897 } | |
898 | |
899 if (startcode == PRIVATE_STREAM_1) { | |
900 put_byte(&ctx->pb, id); | |
901 if (id >= 0xa0) { | |
902 /* LPCM (XXX: check nb_frames) */ | |
903 put_byte(&ctx->pb, 7); | |
904 put_be16(&ctx->pb, 4); /* skip 3 header bytes */ | |
905 put_byte(&ctx->pb, stream->lpcm_header[0]); | |
906 put_byte(&ctx->pb, stream->lpcm_header[1]); | |
907 put_byte(&ctx->pb, stream->lpcm_header[2]); | |
908 } else if (id >= 0x40) { | |
909 /* AC3 */ | |
910 put_byte(&ctx->pb, nb_frames); | |
911 put_be16(&ctx->pb, trailer_size+1); | |
912 } | |
913 } | |
914 | |
915 /* output data */ | |
916 if(av_fifo_generic_read(&stream->fifo, payload_size - stuffing_size, &put_buffer, &ctx->pb) < 0) | |
917 return -1; | |
918 stream->bytes_to_iframe -= payload_size - stuffing_size; | |
919 }else{ | |
920 payload_size= | |
921 stuffing_size= 0; | |
922 } | |
923 | |
924 if (pad_packet_bytes > 0) | |
925 put_padding_packet(ctx,&ctx->pb, pad_packet_bytes); | |
926 | |
927 for(i=0;i<zero_trail_bytes;i++) | |
928 put_byte(&ctx->pb, 0x00); | |
929 | |
930 put_flush_packet(&ctx->pb); | |
931 | |
932 s->packet_number++; | |
933 | |
934 /* only increase the stream packet number if this pack actually contains | |
935 something that is specific to this stream! I.e. a dedicated header | |
936 or some data.*/ | |
937 if (!general_pack) | |
938 stream->packet_number++; | |
939 | |
940 return payload_size - stuffing_size; | |
941 } | |
942 | |
943 static void put_vcd_padding_sector(AVFormatContext *ctx) | |
944 { | |
945 /* There are two ways to do this padding: writing a sector/pack | |
946 of 0 values, or writing an MPEG padding pack. Both seem to | |
947 work with most decoders, BUT the VCD standard only allows a 0-sector | |
948 (see standard p. IV-4, IV-5). | |
949 So a 0-sector it is...*/ | |
950 | |
951 MpegMuxContext *s = ctx->priv_data; | |
952 int i; | |
953 | |
954 for(i=0;i<s->packet_size;i++) | |
955 put_byte(&ctx->pb, 0); | |
956 | |
957 s->vcd_padding_bytes_written += s->packet_size; | |
958 | |
959 put_flush_packet(&ctx->pb); | |
960 | |
961 /* increasing the packet number is correct. The SCR of the following packs | |
962 is calculated from the packet_number and it has to include the padding | |
963 sector (it represents the sector index, not the MPEG pack index) | |
964 (see VCD standard p. IV-6)*/ | |
965 s->packet_number++; | |
966 } | |
967 | |
968 #if 0 /* unused, remove? */ | |
969 static int64_t get_vcd_scr(AVFormatContext *ctx,int stream_index,int64_t pts) | |
970 { | |
971 MpegMuxContext *s = ctx->priv_data; | |
972 int64_t scr; | |
973 | |
974 /* Since the data delivery rate is constant, SCR is computed | |
975 using the formula C + i * 1200 where C is the start constant | |
976 and i is the pack index. | |
977 It is recommended that SCR 0 is at the beginning of the VCD front | |
978 margin (a sequence of empty Form 2 sectors on the CD). | |
979 It is recommended that the front margin is 30 sectors long, so | |
980 we use C = 30*1200 = 36000 | |
981 (Note that even if the front margin is not 30 sectors the file | |
982 will still be correct according to the standard. It just won't have | |
983 the "recommended" value).*/ | |
984 scr = 36000 + s->packet_number * 1200; | |
985 | |
986 return scr; | |
987 } | |
988 #endif | |
989 | |
990 static int remove_decoded_packets(AVFormatContext *ctx, int64_t scr){ | |
991 // MpegMuxContext *s = ctx->priv_data; | |
992 int i; | |
993 | |
994 for(i=0; i<ctx->nb_streams; i++){ | |
995 AVStream *st = ctx->streams[i]; | |
996 StreamInfo *stream = st->priv_data; | |
997 PacketDesc *pkt_desc; | |
998 | |
999 while((pkt_desc= stream->predecode_packet) | |
1000 && scr > pkt_desc->dts){ //FIXME > vs >= | |
1001 if(stream->buffer_index < pkt_desc->size || | |
1002 stream->predecode_packet == stream->premux_packet){ | |
1003 av_log(ctx, AV_LOG_ERROR, | |
1004 "buffer underflow i=%d bufi=%d size=%d\n", | |
1005 i, stream->buffer_index, pkt_desc->size); | |
1006 break; | |
1007 } | |
1008 stream->buffer_index -= pkt_desc->size; | |
1009 | |
1010 stream->predecode_packet= pkt_desc->next; | |
1011 av_freep(&pkt_desc); | |
1012 } | |
1013 } | |
1014 | |
1015 return 0; | |
1016 } | |
1017 | |
1018 static int output_packet(AVFormatContext *ctx, int flush){ | |
1019 MpegMuxContext *s = ctx->priv_data; | |
1020 AVStream *st; | |
1021 StreamInfo *stream; | |
1022 int i, avail_space, es_size, trailer_size; | |
1023 int best_i= -1; | |
1024 int best_score= INT_MIN; | |
1025 int ignore_constraints=0; | |
1026 int64_t scr= s->last_scr; | |
1027 PacketDesc *timestamp_packet; | |
1028 const int64_t max_delay= av_rescale(ctx->max_delay, 90000, AV_TIME_BASE); | |
1029 | |
1030 retry: | |
1031 for(i=0; i<ctx->nb_streams; i++){ | |
1032 AVStream *st = ctx->streams[i]; | |
1033 StreamInfo *stream = st->priv_data; | |
1034 const int avail_data= av_fifo_size(&stream->fifo); | |
1035 const int space= stream->max_buffer_size - stream->buffer_index; | |
1036 int rel_space= 1024*space / stream->max_buffer_size; | |
1037 PacketDesc *next_pkt= stream->premux_packet; | |
1038 | |
1039 /* for subtitle, a single PES packet must be generated, | |
1040 so we flush after every single subtitle packet */ | |
1041 if(s->packet_size > avail_data && !flush | |
1042 && st->codec->codec_type != CODEC_TYPE_SUBTITLE) | |
1043 return 0; | |
1044 if(avail_data==0) | |
1045 continue; | |
1046 assert(avail_data>0); | |
1047 | |
1048 if(space < s->packet_size && !ignore_constraints) | |
1049 continue; | |
1050 | |
1051 if(next_pkt && next_pkt->dts - scr > max_delay) | |
1052 continue; | |
1053 | |
1054 if(rel_space > best_score){ | |
1055 best_score= rel_space; | |
1056 best_i = i; | |
1057 avail_space= space; | |
1058 } | |
1059 } | |
1060 | |
1061 if(best_i < 0){ | |
1062 int64_t best_dts= INT64_MAX; | |
1063 | |
1064 for(i=0; i<ctx->nb_streams; i++){ | |
1065 AVStream *st = ctx->streams[i]; | |
1066 StreamInfo *stream = st->priv_data; | |
1067 PacketDesc *pkt_desc= stream->predecode_packet; | |
1068 if(pkt_desc && pkt_desc->dts < best_dts) | |
1069 best_dts= pkt_desc->dts; | |
1070 } | |
1071 | |
1072 #if 0 | |
1073 av_log(ctx, AV_LOG_DEBUG, "bumping scr, scr:%f, dts:%f\n", | |
1074 scr/90000.0, best_dts/90000.0); | |
1075 #endif | |
1076 if(best_dts == INT64_MAX) | |
1077 return 0; | |
1078 | |
1079 if(scr >= best_dts+1 && !ignore_constraints){ | |
1080 av_log(ctx, AV_LOG_ERROR, "packet too large, ignoring buffer limits to mux it\n"); | |
1081 ignore_constraints= 1; | |
1082 } | |
1083 scr= FFMAX(best_dts+1, scr); | |
1084 if(remove_decoded_packets(ctx, scr) < 0) | |
1085 return -1; | |
1086 goto retry; | |
1087 } | |
1088 | |
1089 assert(best_i >= 0); | |
1090 | |
1091 st = ctx->streams[best_i]; | |
1092 stream = st->priv_data; | |
1093 | |
1094 assert(av_fifo_size(&stream->fifo) > 0); | |
1095 | |
1096 assert(avail_space >= s->packet_size || ignore_constraints); | |
1097 | |
1098 timestamp_packet= stream->premux_packet; | |
1099 if(timestamp_packet->unwritten_size == timestamp_packet->size){ | |
1100 trailer_size= 0; | |
1101 }else{ | |
1102 trailer_size= timestamp_packet->unwritten_size; | |
1103 timestamp_packet= timestamp_packet->next; | |
1104 } | |
1105 | |
1106 if(timestamp_packet){ | |
1107 //av_log(ctx, AV_LOG_DEBUG, "dts:%f pts:%f scr:%f stream:%d\n", timestamp_packet->dts/90000.0, timestamp_packet->pts/90000.0, scr/90000.0, best_i); | |
1108 es_size= flush_packet(ctx, best_i, timestamp_packet->pts, timestamp_packet->dts, scr, trailer_size); | |
1109 }else{ | |
1110 assert(av_fifo_size(&stream->fifo) == trailer_size); | |
1111 es_size= flush_packet(ctx, best_i, AV_NOPTS_VALUE, AV_NOPTS_VALUE, scr, trailer_size); | |
1112 } | |
1113 | |
1114 if (s->is_vcd) { | |
1115 /* Write one or more padding sectors, if necessary, to reach | |
1116 the constant overall bitrate.*/ | |
1117 int vcd_pad_bytes; | |
1118 | |
1119 while((vcd_pad_bytes = get_vcd_padding_size(ctx,stream->premux_packet->pts) ) >= s->packet_size){ //FIXME pts cannot be correct here | |
1120 put_vcd_padding_sector(ctx); | |
1121 s->last_scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet | |
1122 } | |
1123 } | |
1124 | |
1125 stream->buffer_index += es_size; | |
1126 s->last_scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet | |
1127 | |
1128 while(stream->premux_packet && stream->premux_packet->unwritten_size <= es_size){ | |
1129 es_size -= stream->premux_packet->unwritten_size; | |
1130 stream->premux_packet= stream->premux_packet->next; | |
1131 } | |
1132 if(es_size) | |
1133 stream->premux_packet->unwritten_size -= es_size; | |
1134 | |
1135 if(remove_decoded_packets(ctx, s->last_scr) < 0) | |
1136 return -1; | |
1137 | |
1138 return 1; | |
1139 } | |
1140 | |
1141 static int mpeg_mux_write_packet(AVFormatContext *ctx, AVPacket *pkt) | |
1142 { | |
1143 MpegMuxContext *s = ctx->priv_data; | |
1144 int stream_index= pkt->stream_index; | |
1145 int size= pkt->size; | |
1146 uint8_t *buf= pkt->data; | |
1147 AVStream *st = ctx->streams[stream_index]; | |
1148 StreamInfo *stream = st->priv_data; | |
1149 int64_t pts, dts; | |
1150 PacketDesc *pkt_desc; | |
1151 const int preload= av_rescale(ctx->preload, 90000, AV_TIME_BASE); | |
1152 const int is_iframe = st->codec->codec_type == CODEC_TYPE_VIDEO && (pkt->flags & PKT_FLAG_KEY); | |
1153 | |
1154 pts= pkt->pts; | |
1155 dts= pkt->dts; | |
1156 | |
1157 if(pts != AV_NOPTS_VALUE) pts += preload; | |
1158 if(dts != AV_NOPTS_VALUE) dts += preload; | |
1159 | |
1160 //av_log(ctx, AV_LOG_DEBUG, "dts:%f pts:%f flags:%d stream:%d nopts:%d\n", dts/90000.0, pts/90000.0, pkt->flags, pkt->stream_index, pts != AV_NOPTS_VALUE); | |
1161 if (!stream->premux_packet) | |
1162 stream->next_packet = &stream->premux_packet; | |
1163 *stream->next_packet= | |
1164 pkt_desc= av_mallocz(sizeof(PacketDesc)); | |
1165 pkt_desc->pts= pts; | |
1166 pkt_desc->dts= dts; | |
1167 pkt_desc->unwritten_size= | |
1168 pkt_desc->size= size; | |
1169 if(!stream->predecode_packet) | |
1170 stream->predecode_packet= pkt_desc; | |
1171 stream->next_packet= &pkt_desc->next; | |
1172 | |
1173 av_fifo_realloc(&stream->fifo, av_fifo_size(&stream->fifo) + size + 1); | |
1174 | |
1175 if (s->is_dvd){ | |
1176 if (is_iframe && (s->packet_number == 0 || (pts - stream->vobu_start_pts >= 36000))) { // min VOBU length 0.4 seconds (mpucoder) | |
1177 stream->bytes_to_iframe = av_fifo_size(&stream->fifo); | |
1178 stream->align_iframe = 1; | |
1179 stream->vobu_start_pts = pts; | |
1180 } else { | |
1181 stream->align_iframe = 0; | |
1182 } | |
1183 } | |
1184 | |
1185 av_fifo_write(&stream->fifo, buf, size); | |
1186 | |
1187 for(;;){ | |
1188 int ret= output_packet(ctx, 0); | |
1189 if(ret<=0) | |
1190 return ret; | |
1191 } | |
1192 } | |
1193 | |
1194 static int mpeg_mux_end(AVFormatContext *ctx) | |
1195 { | |
1196 // MpegMuxContext *s = ctx->priv_data; | |
1197 StreamInfo *stream; | |
1198 int i; | |
1199 | |
1200 for(;;){ | |
1201 int ret= output_packet(ctx, 1); | |
1202 if(ret<0) | |
1203 return ret; | |
1204 else if(ret==0) | |
1205 break; | |
1206 } | |
1207 | |
1208 /* End header according to MPEG1 systems standard. We do not write | |
1209 it as it is usually not needed by decoders and because it | |
1210 complicates MPEG stream concatenation. */ | |
1211 //put_be32(&ctx->pb, ISO_11172_END_CODE); | |
1212 //put_flush_packet(&ctx->pb); | |
1213 | |
1214 for(i=0;i<ctx->nb_streams;i++) { | |
1215 stream = ctx->streams[i]->priv_data; | |
1216 | |
1217 assert(av_fifo_size(&stream->fifo) == 0); | |
1218 av_fifo_free(&stream->fifo); | |
1219 } | |
1220 return 0; | |
1221 } | |
1222 #endif //CONFIG_MUXERS | |
1223 | |
1224 #ifdef CONFIG_MPEG1SYSTEM_MUXER | |
1225 AVOutputFormat mpeg1system_muxer = { | |
1226 "mpeg", | |
1227 "MPEG1 System format", | |
1228 "video/mpeg", | |
1229 "mpg,mpeg", | |
1230 sizeof(MpegMuxContext), | |
1231 CODEC_ID_MP2, | |
1232 CODEC_ID_MPEG1VIDEO, | |
1233 mpeg_mux_init, | |
1234 mpeg_mux_write_packet, | |
1235 mpeg_mux_end, | |
1236 }; | |
1237 #endif | |
1238 #ifdef CONFIG_MPEG1VCD_MUXER | |
1239 AVOutputFormat mpeg1vcd_muxer = { | |
1240 "vcd", | |
1241 "MPEG1 System format (VCD)", | |
1242 "video/mpeg", | |
1243 NULL, | |
1244 sizeof(MpegMuxContext), | |
1245 CODEC_ID_MP2, | |
1246 CODEC_ID_MPEG1VIDEO, | |
1247 mpeg_mux_init, | |
1248 mpeg_mux_write_packet, | |
1249 mpeg_mux_end, | |
1250 }; | |
1251 #endif | |
1252 #ifdef CONFIG_MPEG2VOB_MUXER | |
1253 AVOutputFormat mpeg2vob_muxer = { | |
1254 "vob", | |
1255 "MPEG2 PS format (VOB)", | |
1256 "video/mpeg", | |
1257 "vob", | |
1258 sizeof(MpegMuxContext), | |
1259 CODEC_ID_MP2, | |
1260 CODEC_ID_MPEG2VIDEO, | |
1261 mpeg_mux_init, | |
1262 mpeg_mux_write_packet, | |
1263 mpeg_mux_end, | |
1264 }; | |
1265 #endif | |
1266 | |
1267 /* Same as mpeg2vob_mux except that the pack size is 2324 */ | |
1268 #ifdef CONFIG_MPEG2SVCD_MUXER | |
1269 AVOutputFormat mpeg2svcd_muxer = { | |
1270 "svcd", | |
1271 "MPEG2 PS format (VOB)", | |
1272 "video/mpeg", | |
1273 "vob", | |
1274 sizeof(MpegMuxContext), | |
1275 CODEC_ID_MP2, | |
1276 CODEC_ID_MPEG2VIDEO, | |
1277 mpeg_mux_init, | |
1278 mpeg_mux_write_packet, | |
1279 mpeg_mux_end, | |
1280 }; | |
1281 #endif | |
1282 | |
1283 /* Same as mpeg2vob_mux except the 'is_dvd' flag is set to produce NAV pkts */ | |
1284 #ifdef CONFIG_MPEG2DVD_MUXER | |
1285 AVOutputFormat mpeg2dvd_muxer = { | |
1286 "dvd", | |
1287 "MPEG2 PS format (DVD VOB)", | |
1288 "video/mpeg", | |
1289 "dvd", | |
1290 sizeof(MpegMuxContext), | |
1291 CODEC_ID_MP2, | |
1292 CODEC_ID_MPEG2VIDEO, | |
1293 mpeg_mux_init, | |
1294 mpeg_mux_write_packet, | |
1295 mpeg_mux_end, | |
1296 }; | |
1297 #endif |