Mercurial > libavformat.hg
annotate raw.c @ 100:fa3ebf0d0270 libavformat
H264 decoder & demuxer
author | michaelni |
---|---|
date | Fri, 04 Apr 2003 14:42:28 +0000 |
parents | 25062c9b1f86 |
children | 7500e14259a6 |
rev | line source |
---|---|
0 | 1 /* |
2 * RAW encoder and decoder | |
3 * Copyright (c) 2001 Fabrice Bellard. | |
4 * | |
5 * This library is free software; you can redistribute it and/or | |
6 * modify it under the terms of the GNU Lesser General Public | |
7 * License as published by the Free Software Foundation; either | |
8 * version 2 of the License, or (at your option) any later version. | |
9 * | |
10 * This library 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 GNU | |
13 * Lesser General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU Lesser General Public | |
16 * License along with this library; if not, write to the Free Software | |
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 */ | |
19 #include "avformat.h" | |
20 | |
21 /* simple formats */ | |
64 | 22 static int raw_write_header(struct AVFormatContext *s) |
0 | 23 { |
24 return 0; | |
25 } | |
26 | |
64 | 27 static int raw_write_packet(struct AVFormatContext *s, int stream_index, |
28 unsigned char *buf, int size, int force_pts) | |
0 | 29 { |
30 put_buffer(&s->pb, buf, size); | |
31 put_flush_packet(&s->pb); | |
32 return 0; | |
33 } | |
34 | |
64 | 35 static int raw_write_trailer(struct AVFormatContext *s) |
0 | 36 { |
37 return 0; | |
38 } | |
39 | |
40 /* raw input */ | |
65 | 41 static int raw_read_header(AVFormatContext *s, AVFormatParameters *ap) |
0 | 42 { |
43 AVStream *st; | |
44 int id; | |
45 | |
46 st = av_new_stream(s, 0); | |
47 if (!st) | |
48 return AVERROR_NOMEM; | |
49 if (ap) { | |
50 id = s->iformat->value; | |
51 if (id == CODEC_ID_RAWVIDEO) { | |
52 st->codec.codec_type = CODEC_TYPE_VIDEO; | |
53 } else { | |
54 st->codec.codec_type = CODEC_TYPE_AUDIO; | |
55 } | |
56 st->codec.codec_id = id; | |
57 | |
58 switch(st->codec.codec_type) { | |
59 case CODEC_TYPE_AUDIO: | |
60 st->codec.sample_rate = ap->sample_rate; | |
61 st->codec.channels = ap->channels; | |
62 break; | |
63 case CODEC_TYPE_VIDEO: | |
85
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
65
diff
changeset
|
64 st->codec.frame_rate = ap->frame_rate; |
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
65
diff
changeset
|
65 st->codec.frame_rate_base = ap->frame_rate_base; |
0 | 66 st->codec.width = ap->width; |
67 st->codec.height = ap->height; | |
68 break; | |
69 default: | |
70 return -1; | |
71 } | |
72 } else { | |
73 return -1; | |
74 } | |
75 return 0; | |
76 } | |
77 | |
78 #define RAW_PACKET_SIZE 1024 | |
79 | |
64 | 80 static int raw_read_packet(AVFormatContext *s, AVPacket *pkt) |
0 | 81 { |
82 int ret, size; | |
28 | 83 // AVStream *st = s->streams[0]; |
0 | 84 |
85 size= RAW_PACKET_SIZE; | |
86 | |
87 if (av_new_packet(pkt, size) < 0) | |
88 return -EIO; | |
89 | |
90 pkt->stream_index = 0; | |
91 ret = get_buffer(&s->pb, pkt->data, size); | |
92 if (ret <= 0) { | |
93 av_free_packet(pkt); | |
94 return -EIO; | |
95 } | |
96 /* note: we need to modify the packet size here to handle the last | |
97 packet */ | |
98 pkt->size = ret; | |
99 return ret; | |
100 } | |
101 | |
64 | 102 static int raw_read_close(AVFormatContext *s) |
0 | 103 { |
104 return 0; | |
105 } | |
106 | |
107 /* mp3 read */ | |
108 static int mp3_read_header(AVFormatContext *s, | |
109 AVFormatParameters *ap) | |
110 { | |
111 AVStream *st; | |
112 | |
113 st = av_new_stream(s, 0); | |
114 if (!st) | |
115 return AVERROR_NOMEM; | |
116 | |
117 st->codec.codec_type = CODEC_TYPE_AUDIO; | |
118 st->codec.codec_id = CODEC_ID_MP2; | |
119 /* the parameters will be extracted from the compressed bitstream */ | |
120 return 0; | |
121 } | |
122 | |
63 | 123 /* ac3 read */ |
124 static int ac3_read_header(AVFormatContext *s, | |
125 AVFormatParameters *ap) | |
126 { | |
127 AVStream *st; | |
128 | |
129 st = av_new_stream(s, 0); | |
130 if (!st) | |
131 return AVERROR_NOMEM; | |
132 | |
133 st->codec.codec_type = CODEC_TYPE_AUDIO; | |
134 st->codec.codec_id = CODEC_ID_AC3; | |
135 /* the parameters will be extracted from the compressed bitstream */ | |
136 return 0; | |
137 } | |
138 | |
0 | 139 /* mpeg1/h263 input */ |
140 static int video_read_header(AVFormatContext *s, | |
141 AVFormatParameters *ap) | |
142 { | |
143 AVStream *st; | |
144 | |
145 st = av_new_stream(s, 0); | |
146 if (!st) | |
147 return AVERROR_NOMEM; | |
148 | |
149 st->codec.codec_type = CODEC_TYPE_VIDEO; | |
150 st->codec.codec_id = s->iformat->value; | |
151 /* for mjpeg, specify frame rate */ | |
152 /* for mpeg4 specify it too (most mpeg4 streams dont have the fixed_vop_rate set ...)*/ | |
153 if (st->codec.codec_id == CODEC_ID_MJPEG || st->codec.codec_id == CODEC_ID_MPEG4) { | |
154 if (ap) { | |
85
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
65
diff
changeset
|
155 st->codec.frame_rate = ap->frame_rate; |
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
65
diff
changeset
|
156 st->codec.frame_rate_base = ap->frame_rate_base; |
0 | 157 } else { |
85
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
65
diff
changeset
|
158 st->codec.frame_rate = 25; |
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
65
diff
changeset
|
159 st->codec.frame_rate_base = 1; |
0 | 160 } |
161 } | |
162 return 0; | |
163 } | |
164 | |
165 #define SEQ_START_CODE 0x000001b3 | |
166 #define GOP_START_CODE 0x000001b8 | |
167 #define PICTURE_START_CODE 0x00000100 | |
168 | |
169 /* XXX: improve that by looking at several start codes */ | |
170 static int mpegvideo_probe(AVProbeData *p) | |
171 { | |
49 | 172 int code; |
173 const uint8_t *d; | |
0 | 174 |
175 /* we search the first start code. If it is a sequence, gop or | |
176 picture start code then we decide it is an mpeg video | |
177 stream. We do not send highest value to give a chance to mpegts */ | |
49 | 178 /* NOTE: the search range was restricted to avoid too many false |
179 detections */ | |
180 | |
181 if (p->buf_size < 6) | |
182 return 0; | |
183 d = p->buf; | |
184 code = (d[0] << 24) | (d[1] << 16) | (d[2] << 8) | (d[3]); | |
185 if ((code & 0xffffff00) == 0x100) { | |
186 if (code == SEQ_START_CODE || | |
187 code == GOP_START_CODE || | |
188 code == PICTURE_START_CODE) | |
189 return 50 - 1; | |
190 else | |
191 return 0; | |
0 | 192 } |
193 return 0; | |
194 } | |
195 | |
196 AVInputFormat mp3_iformat = { | |
197 "mp3", | |
198 "MPEG audio", | |
199 0, | |
200 NULL, | |
201 mp3_read_header, | |
202 raw_read_packet, | |
203 raw_read_close, | |
204 .extensions = "mp2,mp3", /* XXX: use probe */ | |
205 }; | |
206 | |
207 AVOutputFormat mp2_oformat = { | |
208 "mp2", | |
209 "MPEG audio layer 2", | |
210 "audio/x-mpeg", | |
211 "mp2,mp3", | |
212 0, | |
213 CODEC_ID_MP2, | |
214 0, | |
215 raw_write_header, | |
216 raw_write_packet, | |
217 raw_write_trailer, | |
218 }; | |
219 | |
220 | |
221 AVInputFormat ac3_iformat = { | |
222 "ac3", | |
223 "raw ac3", | |
224 0, | |
225 NULL, | |
63 | 226 ac3_read_header, |
0 | 227 raw_read_packet, |
228 raw_read_close, | |
229 .extensions = "ac3", | |
230 }; | |
231 | |
232 AVOutputFormat ac3_oformat = { | |
233 "ac3", | |
234 "raw ac3", | |
235 "audio/x-ac3", | |
236 "ac3", | |
237 0, | |
238 CODEC_ID_AC3, | |
239 0, | |
240 raw_write_header, | |
241 raw_write_packet, | |
242 raw_write_trailer, | |
243 }; | |
244 | |
245 AVOutputFormat h263_oformat = { | |
246 "h263", | |
247 "raw h263", | |
248 "video/x-h263", | |
249 "h263", | |
250 0, | |
251 0, | |
252 CODEC_ID_H263, | |
253 raw_write_header, | |
254 raw_write_packet, | |
255 raw_write_trailer, | |
256 }; | |
257 | |
258 AVInputFormat m4v_iformat = { | |
259 "m4v", | |
260 "raw MPEG4 video format", | |
261 0, | |
262 NULL /*mpegvideo_probe*/, | |
263 video_read_header, | |
264 raw_read_packet, | |
265 raw_read_close, | |
266 .extensions = "m4v", //FIXME remove after writing mpeg4_probe | |
267 .value = CODEC_ID_MPEG4, | |
268 }; | |
269 | |
270 AVOutputFormat m4v_oformat = { | |
271 "m4v", | |
272 "raw MPEG4 video format", | |
273 NULL, | |
274 "m4v", | |
275 0, | |
276 CODEC_ID_NONE, | |
277 CODEC_ID_MPEG4, | |
278 raw_write_header, | |
279 raw_write_packet, | |
280 raw_write_trailer, | |
281 }; | |
282 | |
100 | 283 AVInputFormat h264_iformat = { |
284 "h264", | |
285 "raw H264 video format", | |
286 0, | |
287 NULL /*mpegvideo_probe*/, | |
288 video_read_header, | |
289 raw_read_packet, | |
290 raw_read_close, | |
291 .extensions = "h26l,h264", //FIXME remove after writing mpeg4_probe | |
292 .value = CODEC_ID_H264, | |
293 }; | |
294 | |
295 AVOutputFormat h264_oformat = { | |
296 "h264", | |
297 "raw H264 video format", | |
298 NULL, | |
299 "h264", | |
300 0, | |
301 CODEC_ID_NONE, | |
302 CODEC_ID_H264, | |
303 raw_write_header, | |
304 raw_write_packet, | |
305 raw_write_trailer, | |
306 }; | |
307 | |
0 | 308 AVInputFormat mpegvideo_iformat = { |
309 "mpegvideo", | |
310 "MPEG video", | |
311 0, | |
312 mpegvideo_probe, | |
313 video_read_header, | |
314 raw_read_packet, | |
315 raw_read_close, | |
316 .value = CODEC_ID_MPEG1VIDEO, | |
317 }; | |
318 | |
319 AVOutputFormat mpeg1video_oformat = { | |
320 "mpeg1video", | |
321 "MPEG video", | |
322 "video/x-mpeg", | |
323 "mpg,mpeg", | |
324 0, | |
325 0, | |
326 CODEC_ID_MPEG1VIDEO, | |
327 raw_write_header, | |
328 raw_write_packet, | |
329 raw_write_trailer, | |
330 }; | |
331 | |
332 AVInputFormat mjpeg_iformat = { | |
333 "mjpeg", | |
334 "MJPEG video", | |
335 0, | |
336 NULL, | |
337 video_read_header, | |
338 raw_read_packet, | |
339 raw_read_close, | |
340 .extensions = "mjpg,mjpeg", | |
341 .value = CODEC_ID_MJPEG, | |
342 }; | |
343 | |
344 AVOutputFormat mjpeg_oformat = { | |
345 "mjpeg", | |
346 "MJPEG video", | |
347 "video/x-mjpeg", | |
348 "mjpg,mjpeg", | |
349 0, | |
350 0, | |
351 CODEC_ID_MJPEG, | |
352 raw_write_header, | |
353 raw_write_packet, | |
354 raw_write_trailer, | |
355 }; | |
356 | |
357 /* pcm formats */ | |
358 | |
359 #define PCMDEF(name, long_name, ext, codec) \ | |
360 AVInputFormat pcm_ ## name ## _iformat = {\ | |
361 #name,\ | |
362 long_name,\ | |
363 0,\ | |
364 NULL,\ | |
365 raw_read_header,\ | |
366 raw_read_packet,\ | |
367 raw_read_close,\ | |
368 .extensions = ext,\ | |
369 .value = codec,\ | |
370 };\ | |
371 \ | |
372 AVOutputFormat pcm_ ## name ## _oformat = {\ | |
373 #name,\ | |
374 long_name,\ | |
375 NULL,\ | |
376 ext,\ | |
377 0,\ | |
378 codec,\ | |
379 0,\ | |
380 raw_write_header,\ | |
381 raw_write_packet,\ | |
382 raw_write_trailer,\ | |
383 }; | |
384 | |
385 #ifdef WORDS_BIGENDIAN | |
386 #define BE_DEF(s) s | |
387 #define LE_DEF(s) NULL | |
388 #else | |
389 #define BE_DEF(s) NULL | |
390 #define LE_DEF(s) s | |
391 #endif | |
392 | |
393 | |
394 PCMDEF(s16le, "pcm signed 16 bit little endian format", | |
395 LE_DEF("sw"), CODEC_ID_PCM_S16LE) | |
396 | |
397 PCMDEF(s16be, "pcm signed 16 bit big endian format", | |
398 BE_DEF("sw"), CODEC_ID_PCM_S16BE) | |
399 | |
400 PCMDEF(u16le, "pcm unsigned 16 bit little endian format", | |
401 LE_DEF("uw"), CODEC_ID_PCM_U16LE) | |
402 | |
403 PCMDEF(u16be, "pcm unsigned 16 bit big endian format", | |
404 BE_DEF("uw"), CODEC_ID_PCM_U16BE) | |
405 | |
406 PCMDEF(s8, "pcm signed 8 bit format", | |
407 "sb", CODEC_ID_PCM_S8) | |
408 | |
409 PCMDEF(u8, "pcm unsigned 8 bit format", | |
410 "ub", CODEC_ID_PCM_U8) | |
411 | |
412 PCMDEF(mulaw, "pcm mu law format", | |
413 "ul", CODEC_ID_PCM_MULAW) | |
414 | |
415 PCMDEF(alaw, "pcm A law format", | |
416 "al", CODEC_ID_PCM_ALAW) | |
417 | |
64 | 418 static int rawvideo_read_packet(AVFormatContext *s, AVPacket *pkt) |
0 | 419 { |
420 int packet_size, ret, width, height; | |
421 AVStream *st = s->streams[0]; | |
422 | |
423 width = st->codec.width; | |
424 height = st->codec.height; | |
425 | |
426 switch(st->codec.pix_fmt) { | |
427 case PIX_FMT_YUV420P: | |
428 packet_size = (width * height * 3) / 2; | |
429 break; | |
430 case PIX_FMT_YUV422: | |
431 packet_size = (width * height * 2); | |
432 break; | |
433 case PIX_FMT_BGR24: | |
434 case PIX_FMT_RGB24: | |
435 packet_size = (width * height * 3); | |
436 break; | |
437 default: | |
438 av_abort(); | |
439 break; | |
440 } | |
441 | |
442 if (av_new_packet(pkt, packet_size) < 0) | |
443 return -EIO; | |
444 | |
445 pkt->stream_index = 0; | |
446 #if 0 | |
447 /* bypass buffered I/O */ | |
448 ret = url_read(url_fileno(&s->pb), pkt->data, pkt->size); | |
449 #else | |
450 ret = get_buffer(&s->pb, pkt->data, pkt->size); | |
451 #endif | |
452 if (ret != pkt->size) { | |
453 av_free_packet(pkt); | |
454 return -EIO; | |
455 } else { | |
456 return 0; | |
457 } | |
458 } | |
459 | |
460 AVInputFormat rawvideo_iformat = { | |
461 "rawvideo", | |
462 "raw video format", | |
463 0, | |
464 NULL, | |
465 raw_read_header, | |
466 rawvideo_read_packet, | |
467 raw_read_close, | |
468 .extensions = "yuv", | |
469 .value = CODEC_ID_RAWVIDEO, | |
470 }; | |
471 | |
472 AVOutputFormat rawvideo_oformat = { | |
473 "rawvideo", | |
474 "raw video format", | |
475 NULL, | |
476 "yuv", | |
477 0, | |
478 CODEC_ID_NONE, | |
479 CODEC_ID_RAWVIDEO, | |
480 raw_write_header, | |
481 raw_write_packet, | |
482 raw_write_trailer, | |
483 }; | |
484 | |
485 static int null_write_packet(struct AVFormatContext *s, | |
486 int stream_index, | |
487 unsigned char *buf, int size, int force_pts) | |
488 { | |
489 return 0; | |
490 } | |
491 | |
492 AVOutputFormat null_oformat = { | |
493 "null", | |
494 "null video format", | |
495 NULL, | |
496 NULL, | |
497 0, | |
498 #ifdef WORDS_BIGENDIAN | |
499 CODEC_ID_PCM_S16BE, | |
500 #else | |
501 CODEC_ID_PCM_S16LE, | |
502 #endif | |
503 CODEC_ID_RAWVIDEO, | |
504 raw_write_header, | |
505 null_write_packet, | |
506 raw_write_trailer, | |
507 .flags = AVFMT_NOFILE | AVFMT_RAWPICTURE, | |
508 }; | |
509 | |
510 int raw_init(void) | |
511 { | |
512 av_register_input_format(&mp3_iformat); | |
513 av_register_output_format(&mp2_oformat); | |
514 | |
515 av_register_input_format(&ac3_iformat); | |
516 av_register_output_format(&ac3_oformat); | |
517 | |
518 av_register_output_format(&h263_oformat); | |
519 | |
520 av_register_input_format(&m4v_iformat); | |
521 av_register_output_format(&m4v_oformat); | |
100 | 522 |
523 av_register_input_format(&h264_iformat); | |
524 av_register_output_format(&h264_oformat); | |
0 | 525 |
526 av_register_input_format(&mpegvideo_iformat); | |
527 av_register_output_format(&mpeg1video_oformat); | |
528 | |
529 av_register_input_format(&mjpeg_iformat); | |
530 av_register_output_format(&mjpeg_oformat); | |
531 | |
532 av_register_input_format(&pcm_s16le_iformat); | |
533 av_register_output_format(&pcm_s16le_oformat); | |
534 av_register_input_format(&pcm_s16be_iformat); | |
535 av_register_output_format(&pcm_s16be_oformat); | |
536 av_register_input_format(&pcm_u16le_iformat); | |
537 av_register_output_format(&pcm_u16le_oformat); | |
538 av_register_input_format(&pcm_u16be_iformat); | |
539 av_register_output_format(&pcm_u16be_oformat); | |
540 av_register_input_format(&pcm_s8_iformat); | |
541 av_register_output_format(&pcm_s8_oformat); | |
542 av_register_input_format(&pcm_u8_iformat); | |
543 av_register_output_format(&pcm_u8_oformat); | |
544 av_register_input_format(&pcm_mulaw_iformat); | |
545 av_register_output_format(&pcm_mulaw_oformat); | |
546 av_register_input_format(&pcm_alaw_iformat); | |
547 av_register_output_format(&pcm_alaw_oformat); | |
548 | |
549 av_register_input_format(&rawvideo_iformat); | |
550 av_register_output_format(&rawvideo_oformat); | |
551 | |
552 av_register_output_format(&null_oformat); | |
553 return 0; | |
554 } |