support for DV aspect ratio and erroneous audio patch by (Dan Dennedy (dan at dennedy...
[libav.git] / libavformat / raw.c
CommitLineData
de6d9b64
FB
1/*
2 * RAW encoder and decoder
19720f15 3 * Copyright (c) 2001 Fabrice Bellard.
de6d9b64 4 *
19720f15
FB
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.
de6d9b64 9 *
19720f15 10 * This library is distributed in the hope that it will be useful,
de6d9b64 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19720f15
FB
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
de6d9b64 14 *
19720f15
FB
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
de6d9b64 18 */
de6d9b64
FB
19#include "avformat.h"
20
21/* simple formats */
5c91a675 22static int raw_write_header(struct AVFormatContext *s)
de6d9b64
FB
23{
24 return 0;
25}
26
5c91a675
ZK
27static int raw_write_packet(struct AVFormatContext *s, int stream_index,
28 unsigned char *buf, int size, int force_pts)
de6d9b64
FB
29{
30 put_buffer(&s->pb, buf, size);
31 put_flush_packet(&s->pb);
32 return 0;
33}
34
5c91a675 35static int raw_write_trailer(struct AVFormatContext *s)
de6d9b64
FB
36{
37 return 0;
38}
39
40/* raw input */
0c1a9eda 41static int raw_read_header(AVFormatContext *s, AVFormatParameters *ap)
de6d9b64
FB
42{
43 AVStream *st;
c9a65ca8 44 int id;
de6d9b64 45
c9a65ca8 46 st = av_new_stream(s, 0);
de6d9b64 47 if (!st)
c9a65ca8 48 return AVERROR_NOMEM;
de6d9b64 49 if (ap) {
c9a65ca8
FB
50 id = s->iformat->value;
51 if (id == CODEC_ID_RAWVIDEO) {
de6d9b64 52 st->codec.codec_type = CODEC_TYPE_VIDEO;
de6d9b64 53 } else {
c9a65ca8 54 st->codec.codec_type = CODEC_TYPE_AUDIO;
de6d9b64 55 }
c9a65ca8
FB
56 st->codec.codec_id = id;
57
de6d9b64
FB
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;
de6d9b64
FB
62 break;
63 case CODEC_TYPE_VIDEO:
14bea432
MN
64 st->codec.frame_rate = ap->frame_rate;
65 st->codec.frame_rate_base = ap->frame_rate_base;
de6d9b64
FB
66 st->codec.width = ap->width;
67 st->codec.height = ap->height;
68 break;
69 default:
27e084bd 70 return -1;
de6d9b64
FB
71 }
72 } else {
27e084bd 73 return -1;
de6d9b64
FB
74 }
75 return 0;
76}
77
2e93e3aa 78#define RAW_PACKET_SIZE 1024
de6d9b64 79
5c91a675 80static int raw_read_packet(AVFormatContext *s, AVPacket *pkt)
de6d9b64 81{
7866eeff 82 int ret, size;
2a6874fd 83 // AVStream *st = s->streams[0];
7866eeff 84
231dd3f3 85 size= RAW_PACKET_SIZE;
de6d9b64 86
7866eeff 87 if (av_new_packet(pkt, size) < 0)
de6d9b64
FB
88 return -EIO;
89
90 pkt->stream_index = 0;
7866eeff 91 ret = get_buffer(&s->pb, pkt->data, size);
2e93e3aa 92 if (ret <= 0) {
de6d9b64 93 av_free_packet(pkt);
2e93e3aa
FB
94 return -EIO;
95 }
96 /* note: we need to modify the packet size here to handle the last
97 packet */
98 pkt->size = ret;
de6d9b64
FB
99 return ret;
100}
101
5c91a675 102static int raw_read_close(AVFormatContext *s)
de6d9b64
FB
103{
104 return 0;
105}
106
107/* mp3 read */
108static int mp3_read_header(AVFormatContext *s,
109 AVFormatParameters *ap)
110{
111 AVStream *st;
112
c9a65ca8 113 st = av_new_stream(s, 0);
de6d9b64 114 if (!st)
c9a65ca8 115 return AVERROR_NOMEM;
de6d9b64
FB
116
117 st->codec.codec_type = CODEC_TYPE_AUDIO;
118 st->codec.codec_id = CODEC_ID_MP2;
27e084bd 119 /* the parameters will be extracted from the compressed bitstream */
de6d9b64
FB
120 return 0;
121}
122
cd66005d
FB
123/* ac3 read */
124static 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
de6d9b64
FB
139/* mpeg1/h263 input */
140static int video_read_header(AVFormatContext *s,
141 AVFormatParameters *ap)
142{
143 AVStream *st;
144
c9a65ca8 145 st = av_new_stream(s, 0);
de6d9b64 146 if (!st)
c9a65ca8 147 return AVERROR_NOMEM;
de6d9b64
FB
148
149 st->codec.codec_type = CODEC_TYPE_VIDEO;
c9a65ca8 150 st->codec.codec_id = s->iformat->value;
27e084bd 151 /* for mjpeg, specify frame rate */
7866eeff
MN
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) {
27e084bd 154 if (ap) {
14bea432
MN
155 st->codec.frame_rate = ap->frame_rate;
156 st->codec.frame_rate_base = ap->frame_rate_base;
27e084bd 157 } else {
14bea432
MN
158 st->codec.frame_rate = 25;
159 st->codec.frame_rate_base = 1;
27e084bd
FB
160 }
161 }
de6d9b64
FB
162 return 0;
163}
164
c9a65ca8
FB
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 */
170static int mpegvideo_probe(AVProbeData *p)
171{
fa777321
FB
172 int code;
173 const uint8_t *d;
c9a65ca8
FB
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 */
fa777321
FB
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;
c9a65ca8
FB
192 }
193 return 0;
194}
195
196AVInputFormat mp3_iformat = {
197 "mp3",
de6d9b64 198 "MPEG audio",
c9a65ca8
FB
199 0,
200 NULL,
201 mp3_read_header,
202 raw_read_packet,
203 raw_read_close,
bb76a117 204 .extensions = "mp2,mp3", /* XXX: use probe */
c9a65ca8
FB
205};
206
207AVOutputFormat mp2_oformat = {
208 "mp2",
209 "MPEG audio layer 2",
de6d9b64
FB
210 "audio/x-mpeg",
211 "mp2,mp3",
c9a65ca8 212 0,
de6d9b64
FB
213 CODEC_ID_MP2,
214 0,
215 raw_write_header,
216 raw_write_packet,
217 raw_write_trailer,
c9a65ca8 218};
de6d9b64 219
c9a65ca8
FB
220
221AVInputFormat ac3_iformat = {
222 "ac3",
223 "raw ac3",
224 0,
225 NULL,
cd66005d 226 ac3_read_header,
de6d9b64
FB
227 raw_read_packet,
228 raw_read_close,
bb76a117 229 .extensions = "ac3",
de6d9b64
FB
230};
231
c9a65ca8 232AVOutputFormat ac3_oformat = {
de6d9b64
FB
233 "ac3",
234 "raw ac3",
235 "audio/x-ac3",
236 "ac3",
c9a65ca8 237 0,
de6d9b64
FB
238 CODEC_ID_AC3,
239 0,
240 raw_write_header,
241 raw_write_packet,
242 raw_write_trailer,
243};
244
c9a65ca8 245AVOutputFormat h263_oformat = {
de6d9b64
FB
246 "h263",
247 "raw h263",
248 "video/x-h263",
249 "h263",
250 0,
c9a65ca8 251 0,
de6d9b64
FB
252 CODEC_ID_H263,
253 raw_write_header,
254 raw_write_packet,
255 raw_write_trailer,
c9a65ca8
FB
256};
257
7866eeff
MN
258AVInputFormat m4v_iformat = {
259 "m4v",
260 "raw MPEG4 video format",
261 0,
2f0f5b20 262 NULL /*mpegvideo_probe*/,
7866eeff
MN
263 video_read_header,
264 raw_read_packet,
265 raw_read_close,
bb76a117
MR
266 .extensions = "m4v", //FIXME remove after writing mpeg4_probe
267 .value = CODEC_ID_MPEG4,
7866eeff
MN
268};
269
89b3d7c9
MK
270AVOutputFormat 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
c9a65ca8
FB
283AVInputFormat mpegvideo_iformat = {
284 "mpegvideo",
285 "MPEG video",
286 0,
287 mpegvideo_probe,
de6d9b64
FB
288 video_read_header,
289 raw_read_packet,
290 raw_read_close,
bb76a117 291 .value = CODEC_ID_MPEG1VIDEO,
de6d9b64
FB
292};
293
c9a65ca8
FB
294AVOutputFormat mpeg1video_oformat = {
295 "mpeg1video",
de6d9b64
FB
296 "MPEG video",
297 "video/x-mpeg",
298 "mpg,mpeg",
299 0,
c9a65ca8 300 0,
de6d9b64
FB
301 CODEC_ID_MPEG1VIDEO,
302 raw_write_header,
303 raw_write_packet,
304 raw_write_trailer,
de6d9b64
FB
305};
306
c9a65ca8 307AVInputFormat mjpeg_iformat = {
27e084bd
FB
308 "mjpeg",
309 "MJPEG video",
27e084bd 310 0,
c9a65ca8 311 NULL,
27e084bd
FB
312 video_read_header,
313 raw_read_packet,
314 raw_read_close,
bb76a117
MR
315 .extensions = "mjpg,mjpeg",
316 .value = CODEC_ID_MJPEG,
27e084bd
FB
317};
318
c9a65ca8
FB
319AVOutputFormat mjpeg_oformat = {
320 "mjpeg",
321 "MJPEG video",
322 "video/x-mjpeg",
323 "mjpg,mjpeg",
5ed8fafc 324 0,
5ed8fafc 325 0,
c9a65ca8 326 CODEC_ID_MJPEG,
5ed8fafc
FB
327 raw_write_header,
328 raw_write_packet,
329 raw_write_trailer,
5ed8fafc
FB
330};
331
c9a65ca8 332/* pcm formats */
5ed8fafc 333
c9a65ca8
FB
334#define PCMDEF(name, long_name, ext, codec) \
335AVInputFormat pcm_ ## name ## _iformat = {\
336 #name,\
337 long_name,\
338 0,\
339 NULL,\
340 raw_read_header,\
341 raw_read_packet,\
342 raw_read_close,\
bb76a117
MR
343 .extensions = ext,\
344 .value = codec,\
c9a65ca8
FB
345};\
346\
347AVOutputFormat pcm_ ## name ## _oformat = {\
348 #name,\
349 long_name,\
350 NULL,\
351 ext,\
352 0,\
353 codec,\
354 0,\
355 raw_write_header,\
356 raw_write_packet,\
357 raw_write_trailer,\
5ed8fafc
FB
358};
359
5ed8fafc 360#ifdef WORDS_BIGENDIAN
c9a65ca8
FB
361#define BE_DEF(s) s
362#define LE_DEF(s) NULL
5ed8fafc 363#else
c9a65ca8
FB
364#define BE_DEF(s) NULL
365#define LE_DEF(s) s
5ed8fafc 366#endif
5ed8fafc 367
5ed8fafc 368
c9a65ca8
FB
369PCMDEF(s16le, "pcm signed 16 bit little endian format",
370 LE_DEF("sw"), CODEC_ID_PCM_S16LE)
5ed8fafc 371
c9a65ca8
FB
372PCMDEF(s16be, "pcm signed 16 bit big endian format",
373 BE_DEF("sw"), CODEC_ID_PCM_S16BE)
5ed8fafc 374
c9a65ca8
FB
375PCMDEF(u16le, "pcm unsigned 16 bit little endian format",
376 LE_DEF("uw"), CODEC_ID_PCM_U16LE)
5ed8fafc 377
c9a65ca8
FB
378PCMDEF(u16be, "pcm unsigned 16 bit big endian format",
379 BE_DEF("uw"), CODEC_ID_PCM_U16BE)
5ed8fafc 380
c9a65ca8
FB
381PCMDEF(s8, "pcm signed 8 bit format",
382 "sb", CODEC_ID_PCM_S8)
5ed8fafc 383
c9a65ca8
FB
384PCMDEF(u8, "pcm unsigned 8 bit format",
385 "ub", CODEC_ID_PCM_U8)
5ed8fafc 386
c9a65ca8
FB
387PCMDEF(mulaw, "pcm mu law format",
388 "ul", CODEC_ID_PCM_MULAW)
de6d9b64 389
c9a65ca8
FB
390PCMDEF(alaw, "pcm A law format",
391 "al", CODEC_ID_PCM_ALAW)
de6d9b64 392
5c91a675 393static int rawvideo_read_packet(AVFormatContext *s, AVPacket *pkt)
de6d9b64
FB
394{
395 int packet_size, ret, width, height;
396 AVStream *st = s->streams[0];
397
398 width = st->codec.width;
399 height = st->codec.height;
400
401 switch(st->codec.pix_fmt) {
402 case PIX_FMT_YUV420P:
403 packet_size = (width * height * 3) / 2;
404 break;
405 case PIX_FMT_YUV422:
406 packet_size = (width * height * 2);
407 break;
408 case PIX_FMT_BGR24:
409 case PIX_FMT_RGB24:
410 packet_size = (width * height * 3);
411 break;
412 default:
42343f7e 413 av_abort();
de6d9b64
FB
414 break;
415 }
416
417 if (av_new_packet(pkt, packet_size) < 0)
418 return -EIO;
419
420 pkt->stream_index = 0;
c2c2cd2d 421#if 0
de6d9b64
FB
422 /* bypass buffered I/O */
423 ret = url_read(url_fileno(&s->pb), pkt->data, pkt->size);
c2c2cd2d
MN
424#else
425 ret = get_buffer(&s->pb, pkt->data, pkt->size);
426#endif
de6d9b64
FB
427 if (ret != pkt->size) {
428 av_free_packet(pkt);
429 return -EIO;
430 } else {
431 return 0;
432 }
433}
434
c9a65ca8
FB
435AVInputFormat rawvideo_iformat = {
436 "rawvideo",
437 "raw video format",
438 0,
439 NULL,
440 raw_read_header,
441 rawvideo_read_packet,
442 raw_read_close,
bb76a117
MR
443 .extensions = "yuv",
444 .value = CODEC_ID_RAWVIDEO,
c9a65ca8
FB
445};
446
447AVOutputFormat rawvideo_oformat = {
de6d9b64
FB
448 "rawvideo",
449 "raw video format",
450 NULL,
451 "yuv",
c9a65ca8 452 0,
de6d9b64
FB
453 CODEC_ID_NONE,
454 CODEC_ID_RAWVIDEO,
455 raw_write_header,
456 raw_write_packet,
457 raw_write_trailer,
de6d9b64 458};
c9a65ca8 459
87bdd3e5
FB
460static int null_write_packet(struct AVFormatContext *s,
461 int stream_index,
462 unsigned char *buf, int size, int force_pts)
463{
464 return 0;
465}
466
467AVOutputFormat null_oformat = {
468 "null",
469 "null video format",
470 NULL,
471 NULL,
472 0,
473#ifdef WORDS_BIGENDIAN
474 CODEC_ID_PCM_S16BE,
475#else
476 CODEC_ID_PCM_S16LE,
477#endif
478 CODEC_ID_RAWVIDEO,
479 raw_write_header,
480 null_write_packet,
481 raw_write_trailer,
b1d89f82 482 .flags = AVFMT_NOFILE | AVFMT_RAWPICTURE,
87bdd3e5
FB
483};
484
c9a65ca8
FB
485int raw_init(void)
486{
487 av_register_input_format(&mp3_iformat);
488 av_register_output_format(&mp2_oformat);
489
490 av_register_input_format(&ac3_iformat);
491 av_register_output_format(&ac3_oformat);
492
493 av_register_output_format(&h263_oformat);
89b3d7c9 494
7866eeff 495 av_register_input_format(&m4v_iformat);
89b3d7c9 496 av_register_output_format(&m4v_oformat);
c9a65ca8
FB
497
498 av_register_input_format(&mpegvideo_iformat);
499 av_register_output_format(&mpeg1video_oformat);
500
501 av_register_input_format(&mjpeg_iformat);
502 av_register_output_format(&mjpeg_oformat);
503
504 av_register_input_format(&pcm_s16le_iformat);
505 av_register_output_format(&pcm_s16le_oformat);
506 av_register_input_format(&pcm_s16be_iformat);
507 av_register_output_format(&pcm_s16be_oformat);
508 av_register_input_format(&pcm_u16le_iformat);
509 av_register_output_format(&pcm_u16le_oformat);
510 av_register_input_format(&pcm_u16be_iformat);
511 av_register_output_format(&pcm_u16be_oformat);
512 av_register_input_format(&pcm_s8_iformat);
513 av_register_output_format(&pcm_s8_oformat);
514 av_register_input_format(&pcm_u8_iformat);
515 av_register_output_format(&pcm_u8_oformat);
516 av_register_input_format(&pcm_mulaw_iformat);
517 av_register_output_format(&pcm_mulaw_oformat);
518 av_register_input_format(&pcm_alaw_iformat);
519 av_register_output_format(&pcm_alaw_oformat);
520
521 av_register_input_format(&rawvideo_iformat);
522 av_register_output_format(&rawvideo_oformat);
87bdd3e5
FB
523
524 av_register_output_format(&null_oformat);
c9a65ca8
FB
525 return 0;
526}