variable UDP packet size patch by Max Krasnyansky
[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{
fa777321
FB
156 int code;
157 const uint8_t *d;
c9a65ca8
FB
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 */
fa777321
FB
162 /* NOTE: the search range was restricted to avoid too many false
163 detections */
164
165 if (p->buf_size < 6)
166 return 0;
167 d = p->buf;
168 code = (d[0] << 24) | (d[1] << 16) | (d[2] << 8) | (d[3]);
169 if ((code & 0xffffff00) == 0x100) {
170 if (code == SEQ_START_CODE ||
171 code == GOP_START_CODE ||
172 code == PICTURE_START_CODE)
173 return 50 - 1;
174 else
175 return 0;
c9a65ca8
FB
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 445
87bdd3e5
FB
446static int null_write_packet(struct AVFormatContext *s,
447 int stream_index,
448 unsigned char *buf, int size, int force_pts)
449{
450 return 0;
451}
452
453AVOutputFormat null_oformat = {
454 "null",
455 "null video format",
456 NULL,
457 NULL,
458 0,
459#ifdef WORDS_BIGENDIAN
460 CODEC_ID_PCM_S16BE,
461#else
462 CODEC_ID_PCM_S16LE,
463#endif
464 CODEC_ID_RAWVIDEO,
465 raw_write_header,
466 null_write_packet,
467 raw_write_trailer,
b1d89f82 468 .flags = AVFMT_NOFILE | AVFMT_RAWPICTURE,
87bdd3e5
FB
469};
470
c9a65ca8
FB
471int raw_init(void)
472{
473 av_register_input_format(&mp3_iformat);
474 av_register_output_format(&mp2_oformat);
475
476 av_register_input_format(&ac3_iformat);
477 av_register_output_format(&ac3_oformat);
478
479 av_register_output_format(&h263_oformat);
89b3d7c9 480
7866eeff 481 av_register_input_format(&m4v_iformat);
89b3d7c9 482 av_register_output_format(&m4v_oformat);
c9a65ca8
FB
483
484 av_register_input_format(&mpegvideo_iformat);
485 av_register_output_format(&mpeg1video_oformat);
486
487 av_register_input_format(&mjpeg_iformat);
488 av_register_output_format(&mjpeg_oformat);
489
490 av_register_input_format(&pcm_s16le_iformat);
491 av_register_output_format(&pcm_s16le_oformat);
492 av_register_input_format(&pcm_s16be_iformat);
493 av_register_output_format(&pcm_s16be_oformat);
494 av_register_input_format(&pcm_u16le_iformat);
495 av_register_output_format(&pcm_u16le_oformat);
496 av_register_input_format(&pcm_u16be_iformat);
497 av_register_output_format(&pcm_u16be_oformat);
498 av_register_input_format(&pcm_s8_iformat);
499 av_register_output_format(&pcm_s8_oformat);
500 av_register_input_format(&pcm_u8_iformat);
501 av_register_output_format(&pcm_u8_oformat);
502 av_register_input_format(&pcm_mulaw_iformat);
503 av_register_output_format(&pcm_mulaw_oformat);
504 av_register_input_format(&pcm_alaw_iformat);
505 av_register_output_format(&pcm_alaw_oformat);
506
507 av_register_input_format(&rawvideo_iformat);
508 av_register_output_format(&rawvideo_oformat);
87bdd3e5
FB
509
510 av_register_output_format(&null_oformat);
c9a65ca8
FB
511 return 0;
512}