win32 fixes
[libav.git] / libav / mpeg.c
1 /*
2 * Output a MPEG1 multiplexed video/audio stream
3 * Copyright (c) 2000 Gerard Lantau.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 #include "avformat.h"
20
21 #define MAX_PAYLOAD_SIZE 4096
22 #define NB_STREAMS 2
23
24 typedef struct {
25 UINT8 buffer[MAX_PAYLOAD_SIZE];
26 int buffer_ptr;
27 UINT8 id;
28 int max_buffer_size; /* in bytes */
29 int packet_number;
30 float pts;
31 INT64 start_pts;
32 } StreamInfo;
33
34 typedef struct {
35 int packet_size; /* required packet size */
36 int packet_data_max_size; /* maximum data size inside a packet */
37 int packet_number;
38 int pack_header_freq; /* frequency (in packets^-1) at which we send pack headers */
39 int system_header_freq;
40 int mux_rate; /* bitrate in units of 50 bytes/s */
41 /* stream info */
42 int audio_bound;
43 int video_bound;
44 } MpegMuxContext;
45
46 #define PACK_START_CODE ((unsigned int)0x000001ba)
47 #define SYSTEM_HEADER_START_CODE ((unsigned int)0x000001bb)
48 #define PACKET_START_CODE_MASK ((unsigned int)0xffffff00)
49 #define PACKET_START_CODE_PREFIX ((unsigned int)0x00000100)
50 #define ISO_11172_END_CODE ((unsigned int)0x000001b9)
51
52 /* mpeg2 */
53 #define PROGRAM_STREAM_MAP 0x1bc
54 #define PRIVATE_STREAM_1 0x1bd
55 #define PADDING_STREAM 0x1be
56 #define PRIVATE_STREAM_2 0x1bf
57
58
59 #define AUDIO_ID 0xc0
60 #define VIDEO_ID 0xe0
61
62 static int put_pack_header(AVFormatContext *ctx,
63 UINT8 *buf, INT64 timestamp)
64 {
65 MpegMuxContext *s = ctx->priv_data;
66 PutBitContext pb;
67
68 init_put_bits(&pb, buf, 128, NULL, NULL);
69
70 put_bits(&pb, 32, PACK_START_CODE);
71 put_bits(&pb, 4, 0x2);
72 put_bits(&pb, 3, (UINT32)((timestamp >> 30) & 0x07));
73 put_bits(&pb, 1, 1);
74 put_bits(&pb, 15, (UINT32)((timestamp >> 15) & 0x7fff));
75 put_bits(&pb, 1, 1);
76 put_bits(&pb, 15, (UINT32)((timestamp) & 0x7fff));
77 put_bits(&pb, 1, 1);
78 put_bits(&pb, 1, 1);
79 put_bits(&pb, 22, s->mux_rate);
80 put_bits(&pb, 1, 1);
81
82 flush_put_bits(&pb);
83 return pb.buf_ptr - pb.buf;
84 }
85
86 static int put_system_header(AVFormatContext *ctx, UINT8 *buf)
87 {
88 MpegMuxContext *s = ctx->priv_data;
89 int size, rate_bound, i, private_stream_coded, id;
90 PutBitContext pb;
91
92 init_put_bits(&pb, buf, 128, NULL, NULL);
93
94 put_bits(&pb, 32, SYSTEM_HEADER_START_CODE);
95 put_bits(&pb, 16, 0);
96 put_bits(&pb, 1, 1);
97
98 rate_bound = s->mux_rate; /* maximum bit rate of the multiplexed stream */
99 put_bits(&pb, 22, rate_bound);
100 put_bits(&pb, 1, 1); /* marker */
101 put_bits(&pb, 6, s->audio_bound);
102
103 put_bits(&pb, 1, 1); /* variable bitrate */
104 put_bits(&pb, 1, 1); /* non constrainted bit stream */
105
106 put_bits(&pb, 1, 0); /* audio locked */
107 put_bits(&pb, 1, 0); /* video locked */
108 put_bits(&pb, 1, 1); /* marker */
109
110 put_bits(&pb, 5, s->video_bound);
111 put_bits(&pb, 8, 0xff); /* reserved byte */
112
113 /* audio stream info */
114 private_stream_coded = 0;
115 for(i=0;i<ctx->nb_streams;i++) {
116 StreamInfo *stream = ctx->streams[i]->priv_data;
117 id = stream->id;
118 if (id < 0xc0) {
119 /* special case for private streams (AC3 use that) */
120 if (private_stream_coded)
121 continue;
122 private_stream_coded = 1;
123 id = 0xbd;
124 }
125 put_bits(&pb, 8, id); /* stream ID */
126 put_bits(&pb, 2, 3);
127 if (id < 0xe0) {
128 /* audio */
129 put_bits(&pb, 1, 0);
130 put_bits(&pb, 13, stream->max_buffer_size / 128);
131 } else {
132 /* video */
133 put_bits(&pb, 1, 1);
134 put_bits(&pb, 13, stream->max_buffer_size / 1024);
135 }
136 }
137 flush_put_bits(&pb);
138 size = pb.buf_ptr - pb.buf;
139 /* patch packet size */
140 buf[4] = (size - 6) >> 8;
141 buf[5] = (size - 6) & 0xff;
142
143 return size;
144 }
145
146 static int mpeg_mux_init(AVFormatContext *ctx)
147 {
148 MpegMuxContext *s;
149 int bitrate, i, mpa_id, mpv_id, ac3_id;
150 AVStream *st;
151 StreamInfo *stream;
152
153 s = malloc(sizeof(MpegMuxContext));
154 if (!s)
155 return -1;
156 memset(s, 0, sizeof(MpegMuxContext));
157 ctx->priv_data = s;
158 s->packet_number = 0;
159
160 /* XXX: hardcoded */
161 s->packet_size = 2048;
162 /* startcode(4) + length(2) + flags(1) */
163 s->packet_data_max_size = s->packet_size - 7;
164 s->audio_bound = 0;
165 s->video_bound = 0;
166 mpa_id = AUDIO_ID;
167 ac3_id = 0x80;
168 mpv_id = VIDEO_ID;
169 for(i=0;i<ctx->nb_streams;i++) {
170 st = ctx->streams[i];
171 stream = av_mallocz(sizeof(StreamInfo));
172 if (!stream)
173 goto fail;
174 st->priv_data = stream;
175
176 switch(st->codec.codec_type) {
177 case CODEC_TYPE_AUDIO:
178 if (st->codec.codec_id == CODEC_ID_AC3)
179 stream->id = ac3_id++;
180 else
181 stream->id = mpa_id++;
182 stream->max_buffer_size = 4 * 1024;
183 s->audio_bound++;
184 break;
185 case CODEC_TYPE_VIDEO:
186 stream->id = mpv_id++;
187 stream->max_buffer_size = 46 * 1024;
188 s->video_bound++;
189 break;
190 }
191 }
192
193 /* we increase slightly the bitrate to take into account the
194 headers. XXX: compute it exactly */
195 bitrate = 2000;
196 for(i=0;i<ctx->nb_streams;i++) {
197 st = ctx->streams[i];
198 bitrate += st->codec.bit_rate;
199 }
200 s->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
201 /* every 2 seconds */
202 s->pack_header_freq = 2 * bitrate / s->packet_size / 8;
203 /* every 10 seconds */
204 s->system_header_freq = s->pack_header_freq * 5;
205
206 for(i=0;i<ctx->nb_streams;i++) {
207 stream = ctx->streams[i]->priv_data;
208 stream->buffer_ptr = 0;
209 stream->packet_number = 0;
210 stream->pts = 0;
211 stream->start_pts = -1;
212 }
213 return 0;
214 fail:
215 for(i=0;i<ctx->nb_streams;i++) {
216 free(ctx->streams[i]->priv_data);
217 }
218 free(s);
219 return -ENOMEM;
220 }
221
222 /* flush the packet on stream stream_index */
223 static void flush_packet(AVFormatContext *ctx, int stream_index)
224 {
225 MpegMuxContext *s = ctx->priv_data;
226 StreamInfo *stream = ctx->streams[stream_index]->priv_data;
227 UINT8 *buf_ptr;
228 int size, payload_size, startcode, id, len, stuffing_size, i;
229 INT64 timestamp;
230 UINT8 buffer[128];
231
232 id = stream->id;
233 timestamp = stream->start_pts;
234
235 #if 0
236 printf("packet ID=%2x PTS=%0.3f\n",
237 id, timestamp / 90000.0);
238 #endif
239
240 buf_ptr = buffer;
241 if ((s->packet_number % s->pack_header_freq) == 0) {
242 /* output pack and systems header if needed */
243 size = put_pack_header(ctx, buf_ptr, timestamp);
244 buf_ptr += size;
245 if ((s->packet_number % s->system_header_freq) == 0) {
246 size = put_system_header(ctx, buf_ptr);
247 buf_ptr += size;
248 }
249 }
250 size = buf_ptr - buffer;
251 put_buffer(&ctx->pb, buffer, size);
252
253 /* packet header */
254 payload_size = s->packet_size - (size + 6 + 5);
255 if (id < 0xc0) {
256 startcode = PRIVATE_STREAM_1;
257 payload_size -= 4;
258 } else {
259 startcode = 0x100 + id;
260 }
261 stuffing_size = payload_size - stream->buffer_ptr;
262 if (stuffing_size < 0)
263 stuffing_size = 0;
264
265 put_be32(&ctx->pb, startcode);
266
267 put_be16(&ctx->pb, payload_size + 5);
268 /* stuffing */
269 for(i=0;i<stuffing_size;i++)
270 put_byte(&ctx->pb, 0xff);
271
272 /* presentation time stamp */
273 put_byte(&ctx->pb,
274 (0x02 << 4) |
275 (((timestamp >> 30) & 0x07) << 1) |
276 1);
277 put_be16(&ctx->pb, (UINT16)((((timestamp >> 15) & 0x7fff) << 1) | 1));
278 put_be16(&ctx->pb, (UINT16)((((timestamp) & 0x7fff) << 1) | 1));
279
280 if (startcode == PRIVATE_STREAM_1) {
281 put_byte(&ctx->pb, id);
282 if (id >= 0x80 && id <= 0xbf) {
283 /* XXX: need to check AC3 spec */
284 put_byte(&ctx->pb, 1);
285 put_byte(&ctx->pb, 0);
286 put_byte(&ctx->pb, 2);
287 }
288 }
289
290 /* output data */
291 put_buffer(&ctx->pb, stream->buffer, payload_size - stuffing_size);
292 put_flush_packet(&ctx->pb);
293
294 /* preserve remaining data */
295 len = stream->buffer_ptr - payload_size;
296 if (len < 0)
297 len = 0;
298 memmove(stream->buffer, stream->buffer + stream->buffer_ptr - len, len);
299 stream->buffer_ptr = len;
300
301 s->packet_number++;
302 stream->packet_number++;
303 stream->start_pts = -1;
304 }
305
306 static int mpeg_mux_write_packet(AVFormatContext *ctx,
307 int stream_index, UINT8 *buf, int size)
308 {
309 MpegMuxContext *s = ctx->priv_data;
310 AVStream *st = ctx->streams[stream_index];
311 StreamInfo *stream = st->priv_data;
312 int len;
313
314 while (size > 0) {
315 /* set pts */
316 if (stream->start_pts == -1)
317 stream->start_pts = stream->pts * 90000.0;
318 len = s->packet_data_max_size - stream->buffer_ptr;
319 if (len > size)
320 len = size;
321 memcpy(stream->buffer + stream->buffer_ptr, buf, len);
322 stream->buffer_ptr += len;
323 buf += len;
324 size -= len;
325 while (stream->buffer_ptr >= s->packet_data_max_size) {
326 /* output the packet */
327 if (stream->start_pts == -1)
328 stream->start_pts = stream->pts * 90000.0;
329 flush_packet(ctx, stream_index);
330 }
331 }
332
333 if (st->codec.codec_type == CODEC_TYPE_AUDIO) {
334 stream->pts += (float)st->codec.frame_size / st->codec.sample_rate;
335 } else {
336 stream->pts += FRAME_RATE_BASE / (float)st->codec.frame_rate;
337 }
338 return 0;
339 }
340
341 static int mpeg_mux_end(AVFormatContext *ctx)
342 {
343 StreamInfo *stream;
344 int i;
345
346 /* flush each packet */
347 for(i=0;i<ctx->nb_streams;i++) {
348 stream = ctx->streams[i]->priv_data;
349 if (stream->buffer_ptr > 0)
350 flush_packet(ctx, i);
351 }
352
353 /* write the end header */
354 put_be32(&ctx->pb, ISO_11172_END_CODE);
355 put_flush_packet(&ctx->pb);
356 return 0;
357 }
358
359 /*********************************************/
360 /* demux code */
361
362 #define MAX_SYNC_SIZE 100000
363
364 typedef struct MpegDemuxContext {
365 int header_state;
366 int mux_rate; /* 50 byte/s unit */
367 } MpegDemuxContext;
368
369 static int find_start_code(ByteIOContext *pb, int *size_ptr,
370 UINT32 *header_state)
371 {
372 unsigned int state, v;
373 int val, n;
374
375 state = *header_state;
376 n = *size_ptr;
377 while (n > 0) {
378 if (url_feof(pb))
379 break;
380 v = get_byte(pb);
381 n--;
382 if (state == 0x000001) {
383 state = ((state << 8) | v) & 0xffffff;
384 val = state;
385 goto found;
386 }
387 state = ((state << 8) | v) & 0xffffff;
388 }
389 val = -1;
390 found:
391 *header_state = state;
392 *size_ptr = n;
393 return val;
394 }
395
396 static int mpeg_mux_read_header(AVFormatContext *s,
397 AVFormatParameters *ap)
398 {
399 MpegDemuxContext *m;
400 int size, startcode, c, rate_bound, audio_bound, video_bound, mux_rate, val;
401 int codec_id, n, i, type;
402 AVStream *st;
403
404 m = av_mallocz(sizeof(MpegDemuxContext));
405 if (!m)
406 return -ENOMEM;
407 s->priv_data = m;
408
409 /* search first pack header */
410 m->header_state = 0xff;
411 size = MAX_SYNC_SIZE;
412 for(;;) {
413 while (size > 0) {
414 startcode = find_start_code(&s->pb, &size, &m->header_state);
415 if (startcode == PACK_START_CODE)
416 goto found;
417 }
418 return -ENODATA;
419 found:
420 /* search system header just after pack header */
421 /* parse pack header */
422 get_byte(&s->pb); /* ts1 */
423 get_be16(&s->pb); /* ts2 */
424 get_be16(&s->pb); /* ts3 */
425
426 mux_rate = get_byte(&s->pb) << 16;
427 mux_rate |= get_byte(&s->pb) << 8;
428 mux_rate |= get_byte(&s->pb);
429 mux_rate &= (1 << 22) - 1;
430 m->mux_rate = mux_rate;
431
432 startcode = find_start_code(&s->pb, &size, &m->header_state);
433 if (startcode == SYSTEM_HEADER_START_CODE)
434 break;
435 }
436 size = get_be16(&s->pb);
437 rate_bound = get_byte(&s->pb) << 16;
438 rate_bound |= get_byte(&s->pb) << 8;
439 rate_bound |= get_byte(&s->pb);
440 rate_bound = (rate_bound >> 1) & ((1 << 22) - 1);
441 audio_bound = get_byte(&s->pb) >> 2;
442 video_bound = get_byte(&s->pb) & 0x1f;
443 get_byte(&s->pb); /* reserved byte */
444 #if 0
445 printf("mux_rate=%d kbit/s\n", (m->mux_rate * 50 * 8) / 1000);
446 printf("rate_bound=%d\n", rate_bound);
447 printf("audio_bound=%d\n", audio_bound);
448 printf("video_bound=%d\n", video_bound);
449 #endif
450 size -= 6;
451 s->nb_streams = 0;
452 while (size > 0) {
453 c = get_byte(&s->pb);
454 size--;
455 if ((c & 0x80) == 0)
456 break;
457 val = get_be16(&s->pb);
458 size -= 2;
459 if (c >= 0xc0 && c <= 0xdf) {
460 /* mpeg audio stream */
461 type = CODEC_TYPE_AUDIO;
462 codec_id = CODEC_ID_MP2;
463 n = 1;
464 c = c | 0x100;
465 } else if (c >= 0xe0 && c <= 0xef) {
466 type = CODEC_TYPE_VIDEO;
467 codec_id = CODEC_ID_MPEG1VIDEO;
468 n = 1;
469 c = c | 0x100;
470 } else if (c == 0xb8) {
471 /* all audio streams */
472 /* XXX: hack for DVD: we force AC3, although we do not
473 know that this codec will be used */
474 type = CODEC_TYPE_AUDIO;
475 codec_id = CODEC_ID_AC3;
476 n = audio_bound;
477 c = 0x80;
478 /* c = 0x1c0; */
479 } else if (c == 0xb9) {
480 /* all video streams */
481 type = CODEC_TYPE_VIDEO;
482 codec_id = CODEC_ID_MPEG1VIDEO;
483 n = video_bound;
484 c = 0x1e0;
485 } else {
486 type = 0;
487 codec_id = 0;
488 n = 0;
489 }
490 for(i=0;i<n;i++) {
491 st = av_mallocz(sizeof(AVStream));
492 if (!st)
493 return -ENOMEM;
494 s->streams[s->nb_streams++] = st;
495 st->id = c + i;
496 st->codec.codec_type = type;
497 st->codec.codec_id = codec_id;
498 }
499 }
500
501 return 0;
502 }
503
504 static INT64 get_pts(ByteIOContext *pb, int c)
505 {
506 INT64 pts;
507 int val;
508
509 if (c < 0)
510 c = get_byte(pb);
511 pts = (INT64)((c >> 1) & 0x07) << 30;
512 val = get_be16(pb);
513 pts |= (INT64)(val >> 1) << 15;
514 val = get_be16(pb);
515 pts |= (INT64)(val >> 1);
516 return pts;
517 }
518
519 static int mpeg_mux_read_packet(AVFormatContext *s,
520 AVPacket *pkt)
521 {
522 MpegDemuxContext *m = s->priv_data;
523 AVStream *st;
524 int len, size, startcode, i, c, flags, header_len;
525 INT64 pts, dts;
526
527 /* next start code (should be immediately after */
528 redo:
529 m->header_state = 0xff;
530 size = MAX_SYNC_SIZE;
531 startcode = find_start_code(&s->pb, &size, &m->header_state);
532 // printf("startcode=%x pos=0x%Lx\n", startcode, url_ftell(&s->pb));
533 if (startcode < 0)
534 return -EIO;
535 if (startcode == PACK_START_CODE)
536 goto redo;
537 if (startcode == SYSTEM_HEADER_START_CODE)
538 goto redo;
539 if (startcode == PADDING_STREAM ||
540 startcode == PRIVATE_STREAM_2) {
541 /* skip them */
542 len = get_be16(&s->pb);
543 url_fskip(&s->pb, len);
544 goto redo;
545 }
546 /* find matching stream */
547 if (!((startcode >= 0x1c0 && startcode <= 0x1df) ||
548 (startcode >= 0x1e0 && startcode <= 0x1ef) ||
549 (startcode == 0x1bd)))
550 goto redo;
551
552 len = get_be16(&s->pb);
553 pts = 0;
554 dts = 0;
555 /* stuffing */
556 for(;;) {
557 c = get_byte(&s->pb);
558 len--;
559 /* XXX: for mpeg1, should test only bit 7 */
560 if (c != 0xff)
561 break;
562 }
563 if ((c & 0xc0) == 0x40) {
564 /* buffer scale & size */
565 get_byte(&s->pb);
566 c = get_byte(&s->pb);
567 len -= 2;
568 }
569 if ((c & 0xf0) == 0x20) {
570 pts = get_pts(&s->pb, c);
571 len -= 4;
572 dts = pts;
573 } else if ((c & 0xf0) == 0x30) {
574 pts = get_pts(&s->pb, c);
575 dts = get_pts(&s->pb, -1);
576 len -= 9;
577 } else if ((c & 0xc0) == 0x80) {
578 /* mpeg 2 PES */
579 if ((c & 0x30) != 0) {
580 fprintf(stderr, "Encrypted multiplex not handled\n");
581 return -EIO;
582 }
583 flags = get_byte(&s->pb);
584 header_len = get_byte(&s->pb);
585 len -= 2;
586 if (header_len > len)
587 goto redo;
588 if ((flags & 0xc0) == 0x40) {
589 pts = get_pts(&s->pb, -1);
590 dts = pts;
591 header_len -= 5;
592 len -= 5;
593 } if ((flags & 0xc0) == 0xc0) {
594 pts = get_pts(&s->pb, -1);
595 dts = get_pts(&s->pb, -1);
596 header_len -= 10;
597 len -= 10;
598 }
599 len -= header_len;
600 while (header_len > 0) {
601 get_byte(&s->pb);
602 header_len--;
603 }
604 }
605 if (startcode == 0x1bd) {
606 startcode = get_byte(&s->pb);
607 len--;
608 if (startcode >= 0x80 && startcode <= 0xbf) {
609 /* audio: skip header */
610 get_byte(&s->pb);
611 get_byte(&s->pb);
612 get_byte(&s->pb);
613 len -= 3;
614 }
615 }
616
617 /* now find stream */
618 for(i=0;i<s->nb_streams;i++) {
619 st = s->streams[i];
620 if (st->id == startcode)
621 goto found;
622 }
623 /* skip packet */
624 url_fskip(&s->pb, len);
625 goto redo;
626 found:
627 av_new_packet(pkt, len);
628 get_buffer(&s->pb, pkt->data, pkt->size);
629 pkt->pts = pts;
630 pkt->stream_index = i;
631 return 0;
632 }
633
634 static int mpeg_mux_read_close(AVFormatContext *s)
635 {
636 MpegDemuxContext *m = s->priv_data;
637 free(m);
638 return 0;
639 }
640
641 AVFormat mpeg_mux_format = {
642 "mpeg",
643 "MPEG multiplex format",
644 "video/x-mpeg",
645 "mpg,mpeg,vob",
646 CODEC_ID_MP2,
647 CODEC_ID_MPEG1VIDEO,
648 mpeg_mux_init,
649 mpeg_mux_write_packet,
650 mpeg_mux_end,
651
652 mpeg_mux_read_header,
653 mpeg_mux_read_packet,
654 mpeg_mux_read_close,
655 };