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