handle odd image sizes when using subsampled chroma (useful for JPEG images)
[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 */
22int raw_write_header(struct AVFormatContext *s)
23{
24 return 0;
25}
26
27int raw_write_packet(struct AVFormatContext *s,
28 int stream_index,
10bb7023 29 unsigned char *buf, int size, int force_pts)
de6d9b64
FB
30{
31 put_buffer(&s->pb, buf, size);
32 put_flush_packet(&s->pb);
33 return 0;
34}
35
36int raw_write_trailer(struct AVFormatContext *s)
37{
38 return 0;
39}
40
41/* raw input */
42static int raw_read_header(AVFormatContext *s,
43 AVFormatParameters *ap)
44{
45 AVStream *st;
c9a65ca8 46 int id;
de6d9b64 47
c9a65ca8 48 st = av_new_stream(s, 0);
de6d9b64 49 if (!st)
c9a65ca8 50 return AVERROR_NOMEM;
de6d9b64 51 if (ap) {
c9a65ca8
FB
52 id = s->iformat->value;
53 if (id == CODEC_ID_RAWVIDEO) {
de6d9b64 54 st->codec.codec_type = CODEC_TYPE_VIDEO;
de6d9b64 55 } else {
c9a65ca8 56 st->codec.codec_type = CODEC_TYPE_AUDIO;
de6d9b64 57 }
c9a65ca8
FB
58 st->codec.codec_id = id;
59
de6d9b64
FB
60 switch(st->codec.codec_type) {
61 case CODEC_TYPE_AUDIO:
62 st->codec.sample_rate = ap->sample_rate;
63 st->codec.channels = ap->channels;
de6d9b64
FB
64 break;
65 case CODEC_TYPE_VIDEO:
66 st->codec.frame_rate = ap->frame_rate;
67 st->codec.width = ap->width;
68 st->codec.height = ap->height;
69 break;
70 default:
27e084bd 71 return -1;
de6d9b64
FB
72 }
73 } else {
27e084bd 74 return -1;
de6d9b64
FB
75 }
76 return 0;
77}
78
2e93e3aa 79#define RAW_PACKET_SIZE 1024
de6d9b64
FB
80
81int raw_read_packet(AVFormatContext *s,
82 AVPacket *pkt)
83{
7866eeff 84 int ret, size;
2a6874fd 85 // AVStream *st = s->streams[0];
7866eeff 86
231dd3f3 87 size= RAW_PACKET_SIZE;
de6d9b64 88
7866eeff 89 if (av_new_packet(pkt, size) < 0)
de6d9b64
FB
90 return -EIO;
91
92 pkt->stream_index = 0;
7866eeff 93 ret = get_buffer(&s->pb, pkt->data, size);
2e93e3aa 94 if (ret <= 0) {
de6d9b64 95 av_free_packet(pkt);
2e93e3aa
FB
96 return -EIO;
97 }
98 /* note: we need to modify the packet size here to handle the last
99 packet */
100 pkt->size = ret;
de6d9b64
FB
101 return ret;
102}
103
104int raw_read_close(AVFormatContext *s)
105{
106 return 0;
107}
108
109/* mp3 read */
110static int mp3_read_header(AVFormatContext *s,
111 AVFormatParameters *ap)
112{
113 AVStream *st;
114
c9a65ca8 115 st = av_new_stream(s, 0);
de6d9b64 116 if (!st)
c9a65ca8 117 return AVERROR_NOMEM;
de6d9b64
FB
118
119 st->codec.codec_type = CODEC_TYPE_AUDIO;
120 st->codec.codec_id = CODEC_ID_MP2;
27e084bd 121 /* the parameters will be extracted from the compressed bitstream */
de6d9b64
FB
122 return 0;
123}
124
125/* mpeg1/h263 input */
126static int video_read_header(AVFormatContext *s,
127 AVFormatParameters *ap)
128{
129 AVStream *st;
130
c9a65ca8 131 st = av_new_stream(s, 0);
de6d9b64 132 if (!st)
c9a65ca8 133 return AVERROR_NOMEM;
de6d9b64
FB
134
135 st->codec.codec_type = CODEC_TYPE_VIDEO;
c9a65ca8 136 st->codec.codec_id = s->iformat->value;
27e084bd 137 /* for mjpeg, specify frame rate */
7866eeff
MN
138 /* for mpeg4 specify it too (most mpeg4 streams dont have the fixed_vop_rate set ...)*/
139 if (st->codec.codec_id == CODEC_ID_MJPEG || st->codec.codec_id == CODEC_ID_MPEG4) {
27e084bd
FB
140 if (ap) {
141 st->codec.frame_rate = ap->frame_rate;
142 } else {
143 st->codec.frame_rate = 25 * FRAME_RATE_BASE;
144 }
145 }
de6d9b64
FB
146 return 0;
147}
148
c9a65ca8
FB
149#define SEQ_START_CODE 0x000001b3
150#define GOP_START_CODE 0x000001b8
151#define PICTURE_START_CODE 0x00000100
152
153/* XXX: improve that by looking at several start codes */
154static int mpegvideo_probe(AVProbeData *p)
155{
156 int code, c, i;
157 code = 0xff;
158
159 /* we search the first start code. If it is a sequence, gop or
160 picture start code then we decide it is an mpeg video
161 stream. We do not send highest value to give a chance to mpegts */
162 for(i=0;i<p->buf_size;i++) {
163 c = p->buf[i];
164 code = (code << 8) | c;
165 if ((code & 0xffffff00) == 0x100) {
166 if (code == SEQ_START_CODE ||
167 code == GOP_START_CODE ||
168 code == PICTURE_START_CODE)
b24cf828 169 return 50 - 1;
c9a65ca8
FB
170 else
171 return 0;
172 }
173 }
174 return 0;
175}
176
177AVInputFormat mp3_iformat = {
178 "mp3",
de6d9b64 179 "MPEG audio",
c9a65ca8
FB
180 0,
181 NULL,
182 mp3_read_header,
183 raw_read_packet,
184 raw_read_close,
bb76a117 185 .extensions = "mp2,mp3", /* XXX: use probe */
c9a65ca8
FB
186};
187
188AVOutputFormat mp2_oformat = {
189 "mp2",
190 "MPEG audio layer 2",
de6d9b64
FB
191 "audio/x-mpeg",
192 "mp2,mp3",
c9a65ca8 193 0,
de6d9b64
FB
194 CODEC_ID_MP2,
195 0,
196 raw_write_header,
197 raw_write_packet,
198 raw_write_trailer,
c9a65ca8 199};
de6d9b64 200
c9a65ca8
FB
201
202AVInputFormat ac3_iformat = {
203 "ac3",
204 "raw ac3",
205 0,
206 NULL,
207 raw_read_header,
de6d9b64
FB
208 raw_read_packet,
209 raw_read_close,
bb76a117
MR
210 .extensions = "ac3",
211 .value = CODEC_ID_AC3,
de6d9b64
FB
212};
213
c9a65ca8 214AVOutputFormat ac3_oformat = {
de6d9b64
FB
215 "ac3",
216 "raw ac3",
217 "audio/x-ac3",
218 "ac3",
c9a65ca8 219 0,
de6d9b64
FB
220 CODEC_ID_AC3,
221 0,
222 raw_write_header,
223 raw_write_packet,
224 raw_write_trailer,
225};
226
c9a65ca8 227AVOutputFormat h263_oformat = {
de6d9b64
FB
228 "h263",
229 "raw h263",
230 "video/x-h263",
231 "h263",
232 0,
c9a65ca8 233 0,
de6d9b64
FB
234 CODEC_ID_H263,
235 raw_write_header,
236 raw_write_packet,
237 raw_write_trailer,
c9a65ca8
FB
238};
239
7866eeff
MN
240AVInputFormat m4v_iformat = {
241 "m4v",
242 "raw MPEG4 video format",
243 0,
2f0f5b20 244 NULL /*mpegvideo_probe*/,
7866eeff
MN
245 video_read_header,
246 raw_read_packet,
247 raw_read_close,
bb76a117
MR
248 .extensions = "m4v", //FIXME remove after writing mpeg4_probe
249 .value = CODEC_ID_MPEG4,
7866eeff
MN
250};
251
89b3d7c9
MK
252AVOutputFormat m4v_oformat = {
253 "m4v",
254 "raw MPEG4 video format",
255 NULL,
256 "m4v",
257 0,
258 CODEC_ID_NONE,
259 CODEC_ID_MPEG4,
260 raw_write_header,
261 raw_write_packet,
262 raw_write_trailer,
263};
264
c9a65ca8
FB
265AVInputFormat mpegvideo_iformat = {
266 "mpegvideo",
267 "MPEG video",
268 0,
269 mpegvideo_probe,
de6d9b64
FB
270 video_read_header,
271 raw_read_packet,
272 raw_read_close,
bb76a117 273 .value = CODEC_ID_MPEG1VIDEO,
de6d9b64
FB
274};
275
c9a65ca8
FB
276AVOutputFormat mpeg1video_oformat = {
277 "mpeg1video",
de6d9b64
FB
278 "MPEG video",
279 "video/x-mpeg",
280 "mpg,mpeg",
281 0,
c9a65ca8 282 0,
de6d9b64
FB
283 CODEC_ID_MPEG1VIDEO,
284 raw_write_header,
285 raw_write_packet,
286 raw_write_trailer,
de6d9b64
FB
287};
288
c9a65ca8 289AVInputFormat mjpeg_iformat = {
27e084bd
FB
290 "mjpeg",
291 "MJPEG video",
27e084bd 292 0,
c9a65ca8 293 NULL,
27e084bd
FB
294 video_read_header,
295 raw_read_packet,
296 raw_read_close,
bb76a117
MR
297 .extensions = "mjpg,mjpeg",
298 .value = CODEC_ID_MJPEG,
27e084bd
FB
299};
300
c9a65ca8
FB
301AVOutputFormat mjpeg_oformat = {
302 "mjpeg",
303 "MJPEG video",
304 "video/x-mjpeg",
305 "mjpg,mjpeg",
5ed8fafc 306 0,
5ed8fafc 307 0,
c9a65ca8 308 CODEC_ID_MJPEG,
5ed8fafc
FB
309 raw_write_header,
310 raw_write_packet,
311 raw_write_trailer,
5ed8fafc
FB
312};
313
c9a65ca8 314/* pcm formats */
5ed8fafc 315
c9a65ca8
FB
316#define PCMDEF(name, long_name, ext, codec) \
317AVInputFormat pcm_ ## name ## _iformat = {\
318 #name,\
319 long_name,\
320 0,\
321 NULL,\
322 raw_read_header,\
323 raw_read_packet,\
324 raw_read_close,\
bb76a117
MR
325 .extensions = ext,\
326 .value = codec,\
c9a65ca8
FB
327};\
328\
329AVOutputFormat pcm_ ## name ## _oformat = {\
330 #name,\
331 long_name,\
332 NULL,\
333 ext,\
334 0,\
335 codec,\
336 0,\
337 raw_write_header,\
338 raw_write_packet,\
339 raw_write_trailer,\
5ed8fafc
FB
340};
341
5ed8fafc 342#ifdef WORDS_BIGENDIAN
c9a65ca8
FB
343#define BE_DEF(s) s
344#define LE_DEF(s) NULL
5ed8fafc 345#else
c9a65ca8
FB
346#define BE_DEF(s) NULL
347#define LE_DEF(s) s
5ed8fafc 348#endif
5ed8fafc 349
5ed8fafc 350
c9a65ca8
FB
351PCMDEF(s16le, "pcm signed 16 bit little endian format",
352 LE_DEF("sw"), CODEC_ID_PCM_S16LE)
5ed8fafc 353
c9a65ca8
FB
354PCMDEF(s16be, "pcm signed 16 bit big endian format",
355 BE_DEF("sw"), CODEC_ID_PCM_S16BE)
5ed8fafc 356
c9a65ca8
FB
357PCMDEF(u16le, "pcm unsigned 16 bit little endian format",
358 LE_DEF("uw"), CODEC_ID_PCM_U16LE)
5ed8fafc 359
c9a65ca8
FB
360PCMDEF(u16be, "pcm unsigned 16 bit big endian format",
361 BE_DEF("uw"), CODEC_ID_PCM_U16BE)
5ed8fafc 362
c9a65ca8
FB
363PCMDEF(s8, "pcm signed 8 bit format",
364 "sb", CODEC_ID_PCM_S8)
5ed8fafc 365
c9a65ca8
FB
366PCMDEF(u8, "pcm unsigned 8 bit format",
367 "ub", CODEC_ID_PCM_U8)
5ed8fafc 368
c9a65ca8
FB
369PCMDEF(mulaw, "pcm mu law format",
370 "ul", CODEC_ID_PCM_MULAW)
de6d9b64 371
c9a65ca8
FB
372PCMDEF(alaw, "pcm A law format",
373 "al", CODEC_ID_PCM_ALAW)
de6d9b64
FB
374
375int rawvideo_read_packet(AVFormatContext *s,
376 AVPacket *pkt)
377{
378 int packet_size, ret, width, height;
379 AVStream *st = s->streams[0];
380
381 width = st->codec.width;
382 height = st->codec.height;
383
384 switch(st->codec.pix_fmt) {
385 case PIX_FMT_YUV420P:
386 packet_size = (width * height * 3) / 2;
387 break;
388 case PIX_FMT_YUV422:
389 packet_size = (width * height * 2);
390 break;
391 case PIX_FMT_BGR24:
392 case PIX_FMT_RGB24:
393 packet_size = (width * height * 3);
394 break;
395 default:
42343f7e 396 av_abort();
de6d9b64
FB
397 break;
398 }
399
400 if (av_new_packet(pkt, packet_size) < 0)
401 return -EIO;
402
403 pkt->stream_index = 0;
c2c2cd2d 404#if 0
de6d9b64
FB
405 /* bypass buffered I/O */
406 ret = url_read(url_fileno(&s->pb), pkt->data, pkt->size);
c2c2cd2d
MN
407#else
408 ret = get_buffer(&s->pb, pkt->data, pkt->size);
409#endif
de6d9b64
FB
410 if (ret != pkt->size) {
411 av_free_packet(pkt);
412 return -EIO;
413 } else {
414 return 0;
415 }
416}
417
c9a65ca8
FB
418AVInputFormat rawvideo_iformat = {
419 "rawvideo",
420 "raw video format",
421 0,
422 NULL,
423 raw_read_header,
424 rawvideo_read_packet,
425 raw_read_close,
bb76a117
MR
426 .extensions = "yuv",
427 .value = CODEC_ID_RAWVIDEO,
c9a65ca8
FB
428};
429
430AVOutputFormat rawvideo_oformat = {
de6d9b64
FB
431 "rawvideo",
432 "raw video format",
433 NULL,
434 "yuv",
c9a65ca8 435 0,
de6d9b64
FB
436 CODEC_ID_NONE,
437 CODEC_ID_RAWVIDEO,
438 raw_write_header,
439 raw_write_packet,
440 raw_write_trailer,
de6d9b64 441};
c9a65ca8 442
87bdd3e5
FB
443static int null_write_packet(struct AVFormatContext *s,
444 int stream_index,
445 unsigned char *buf, int size, int force_pts)
446{
447 return 0;
448}
449
450AVOutputFormat null_oformat = {
451 "null",
452 "null video format",
453 NULL,
454 NULL,
455 0,
456#ifdef WORDS_BIGENDIAN
457 CODEC_ID_PCM_S16BE,
458#else
459 CODEC_ID_PCM_S16LE,
460#endif
461 CODEC_ID_RAWVIDEO,
462 raw_write_header,
463 null_write_packet,
464 raw_write_trailer,
b1d89f82 465 .flags = AVFMT_NOFILE | AVFMT_RAWPICTURE,
87bdd3e5
FB
466};
467
c9a65ca8
FB
468int raw_init(void)
469{
470 av_register_input_format(&mp3_iformat);
471 av_register_output_format(&mp2_oformat);
472
473 av_register_input_format(&ac3_iformat);
474 av_register_output_format(&ac3_oformat);
475
476 av_register_output_format(&h263_oformat);
89b3d7c9 477
7866eeff 478 av_register_input_format(&m4v_iformat);
89b3d7c9 479 av_register_output_format(&m4v_oformat);
c9a65ca8
FB
480
481 av_register_input_format(&mpegvideo_iformat);
482 av_register_output_format(&mpeg1video_oformat);
483
484 av_register_input_format(&mjpeg_iformat);
485 av_register_output_format(&mjpeg_oformat);
486
487 av_register_input_format(&pcm_s16le_iformat);
488 av_register_output_format(&pcm_s16le_oformat);
489 av_register_input_format(&pcm_s16be_iformat);
490 av_register_output_format(&pcm_s16be_oformat);
491 av_register_input_format(&pcm_u16le_iformat);
492 av_register_output_format(&pcm_u16le_oformat);
493 av_register_input_format(&pcm_u16be_iformat);
494 av_register_output_format(&pcm_u16be_oformat);
495 av_register_input_format(&pcm_s8_iformat);
496 av_register_output_format(&pcm_s8_oformat);
497 av_register_input_format(&pcm_u8_iformat);
498 av_register_output_format(&pcm_u8_oformat);
499 av_register_input_format(&pcm_mulaw_iformat);
500 av_register_output_format(&pcm_mulaw_oformat);
501 av_register_input_format(&pcm_alaw_iformat);
502 av_register_output_format(&pcm_alaw_oformat);
503
504 av_register_input_format(&rawvideo_iformat);
505 av_register_output_format(&rawvideo_oformat);
87bdd3e5
FB
506
507 av_register_output_format(&null_oformat);
c9a65ca8
FB
508 return 0;
509}