]> git.sesse.net Git - ffmpeg/blob - libavformat/raw.c
fixed memory leak
[ffmpeg] / libavformat / raw.c
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 #ifdef CONFIG_ENCODERS
22 /* simple formats */
23 static int raw_write_header(struct AVFormatContext *s)
24 {
25     return 0;
26 }
27
28 static int raw_write_packet(struct AVFormatContext *s, int stream_index,
29                             const uint8_t *buf, int size, int64_t pts)
30 {
31     put_buffer(&s->pb, buf, size);
32     put_flush_packet(&s->pb);
33     return 0;
34 }
35
36 static int raw_write_trailer(struct AVFormatContext *s)
37 {
38     return 0;
39 }
40 #endif //CONFIG_ENCODERS
41
42 /* raw input */
43 static int raw_read_header(AVFormatContext *s, AVFormatParameters *ap)
44 {
45     AVStream *st;
46     int id;
47
48     st = av_new_stream(s, 0);
49     if (!st)
50         return AVERROR_NOMEM;
51     if (ap) {
52         id = s->iformat->value;
53         if (id == CODEC_ID_RAWVIDEO) {
54             st->codec.codec_type = CODEC_TYPE_VIDEO;
55         } else {
56             st->codec.codec_type = CODEC_TYPE_AUDIO;
57         }
58         st->codec.codec_id = id;
59
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;
64             break;
65         case CODEC_TYPE_VIDEO:
66             st->codec.frame_rate      = ap->frame_rate;
67             st->codec.frame_rate_base = ap->frame_rate_base;
68             st->codec.width = ap->width;
69             st->codec.height = ap->height;
70             st->codec.pix_fmt = ap->pix_fmt;
71             break;
72         default:
73             return -1;
74         }
75     } else {
76         return -1;
77     }
78     return 0;
79 }
80
81 #define RAW_PACKET_SIZE 1024
82
83 static int raw_read_packet(AVFormatContext *s, AVPacket *pkt)
84 {
85     int ret, size;
86     //    AVStream *st = s->streams[0];
87     
88     size= RAW_PACKET_SIZE;
89
90     if (av_new_packet(pkt, size) < 0)
91         return -EIO;
92
93     pkt->stream_index = 0;
94     ret = get_buffer(&s->pb, pkt->data, size);
95     if (ret <= 0) {
96         av_free_packet(pkt);
97         return -EIO;
98     }
99     /* note: we need to modify the packet size here to handle the last
100        packet */
101     pkt->size = ret;
102     return ret;
103 }
104
105 static int raw_read_close(AVFormatContext *s)
106 {
107     return 0;
108 }
109
110 int pcm_read_seek(AVFormatContext *s, 
111                   int stream_index, int64_t timestamp)
112 {
113     AVStream *st;
114     int block_align, byte_rate;
115     int64_t pos;
116
117     st = s->streams[0];
118     switch(st->codec.codec_id) {
119     case CODEC_ID_PCM_S16LE:
120     case CODEC_ID_PCM_S16BE:
121     case CODEC_ID_PCM_U16LE:
122     case CODEC_ID_PCM_U16BE:
123         block_align = 2 * st->codec.channels;
124         byte_rate = block_align * st->codec.sample_rate;
125         break;
126     case CODEC_ID_PCM_S8:
127     case CODEC_ID_PCM_U8:
128     case CODEC_ID_PCM_MULAW:
129     case CODEC_ID_PCM_ALAW:
130         block_align = st->codec.channels;
131         byte_rate = block_align * st->codec.sample_rate;
132         break;
133     default:
134         block_align = st->codec.block_align;
135         byte_rate = st->codec.bit_rate / 8;
136         break;
137     }
138     
139     if (block_align <= 0 || byte_rate <= 0)
140         return -1;
141
142     /* compute the position by aligning it to block_align */
143     pos = (timestamp * byte_rate) / AV_TIME_BASE;
144     pos = (pos / block_align) * block_align;
145
146     /* recompute exact position */
147     st->cur_dts = (pos * AV_TIME_BASE) / byte_rate;
148     url_fseek(&s->pb, pos + s->data_offset, SEEK_SET);
149     return 0;
150 }
151
152 /* ac3 read */
153 static int ac3_read_header(AVFormatContext *s,
154                            AVFormatParameters *ap)
155 {
156     AVStream *st;
157
158     st = av_new_stream(s, 0);
159     if (!st)
160         return AVERROR_NOMEM;
161
162     st->codec.codec_type = CODEC_TYPE_AUDIO;
163     st->codec.codec_id = CODEC_ID_AC3;
164     st->need_parsing = 1;
165     /* the parameters will be extracted from the compressed bitstream */
166     return 0;
167 }
168
169 /* mpeg1/h263 input */
170 static int video_read_header(AVFormatContext *s,
171                              AVFormatParameters *ap)
172 {
173     AVStream *st;
174
175     st = av_new_stream(s, 0);
176     if (!st)
177         return AVERROR_NOMEM;
178
179     st->codec.codec_type = CODEC_TYPE_VIDEO;
180     st->codec.codec_id = s->iformat->value;
181     st->need_parsing = 1;
182
183     /* for mjpeg, specify frame rate */
184     /* for mpeg4 specify it too (most mpeg4 streams dont have the fixed_vop_rate set ...)*/
185     if (st->codec.codec_id == CODEC_ID_MJPEG || 
186         st->codec.codec_id == CODEC_ID_MPEG4) {
187         if (ap && ap->frame_rate) {
188             st->codec.frame_rate      = ap->frame_rate;
189             st->codec.frame_rate_base = ap->frame_rate_base;
190         } else {
191             st->codec.frame_rate      = 25;
192             st->codec.frame_rate_base = 1;
193         }
194     }
195     return 0;
196 }
197
198 #define SEQ_START_CODE          0x000001b3
199 #define GOP_START_CODE          0x000001b8
200 #define PICTURE_START_CODE      0x00000100
201
202 /* XXX: improve that by looking at several start codes */
203 static int mpegvideo_probe(AVProbeData *p)
204 {
205     int code;
206     const uint8_t *d;
207
208     /* we search the first start code. If it is a sequence, gop or
209        picture start code then we decide it is an mpeg video
210        stream. We do not send highest value to give a chance to mpegts */
211     /* NOTE: the search range was restricted to avoid too many false
212        detections */
213
214     if (p->buf_size < 6)
215         return 0;
216     d = p->buf;
217     code = (d[0] << 24) | (d[1] << 16) | (d[2] << 8) | (d[3]);
218     if ((code & 0xffffff00) == 0x100) {
219         if (code == SEQ_START_CODE ||
220             code == GOP_START_CODE ||
221             code == PICTURE_START_CODE)
222             return 50 - 1;
223         else
224             return 0;
225     }
226     return 0;
227 }
228
229 static int h263_probe(AVProbeData *p)
230 {
231     int code;
232     const uint8_t *d;
233
234     if (p->buf_size < 6)
235         return 0;
236     d = p->buf;
237     code = (d[0] << 14) | (d[1] << 6) | (d[2] >> 2);
238     if (code == 0x20) {
239         return 50;
240     }
241     return 0;
242 }
243
244 AVInputFormat ac3_iformat = {
245     "ac3",
246     "raw ac3",
247     0,
248     NULL,
249     ac3_read_header,
250     raw_read_packet,
251     raw_read_close,
252     .extensions = "ac3",
253 };
254
255 #ifdef CONFIG_ENCODERS
256 AVOutputFormat ac3_oformat = {
257     "ac3",
258     "raw ac3",
259     "audio/x-ac3", 
260     "ac3",
261     0,
262     CODEC_ID_AC3,
263     0,
264     raw_write_header,
265     raw_write_packet,
266     raw_write_trailer,
267 };
268 #endif //CONFIG_ENCODERS
269
270 AVInputFormat h263_iformat = {
271     "h263",
272     "raw h263",
273     0,
274     h263_probe,
275     video_read_header,
276     raw_read_packet,
277     raw_read_close,
278 //    .extensions = "h263", //FIXME remove after writing mpeg4_probe
279     .value = CODEC_ID_H263,
280 };
281
282 #ifdef CONFIG_ENCODERS
283 AVOutputFormat h263_oformat = {
284     "h263",
285     "raw h263",
286     "video/x-h263",
287     "h263",
288     0,
289     0,
290     CODEC_ID_H263,
291     raw_write_header,
292     raw_write_packet,
293     raw_write_trailer,
294 };
295 #endif //CONFIG_ENCODERS
296
297 AVInputFormat m4v_iformat = {
298     "m4v",
299     "raw MPEG4 video format",
300     0,
301     NULL /*mpegvideo_probe*/,
302     video_read_header,
303     raw_read_packet,
304     raw_read_close,
305     .extensions = "m4v", //FIXME remove after writing mpeg4_probe
306     .value = CODEC_ID_MPEG4,
307 };
308
309 #ifdef CONFIG_ENCODERS
310 AVOutputFormat m4v_oformat = {
311     "m4v",
312     "raw MPEG4 video format",
313     NULL,
314     "m4v",
315     0,
316     CODEC_ID_NONE,
317     CODEC_ID_MPEG4,
318     raw_write_header,
319     raw_write_packet,
320     raw_write_trailer,
321 };
322 #endif //CONFIG_ENCODERS
323
324 AVInputFormat h264_iformat = {
325     "h264",
326     "raw H264 video format",
327     0,
328     NULL /*mpegvideo_probe*/,
329     video_read_header,
330     raw_read_packet,
331     raw_read_close,
332     .extensions = "h26l,h264", //FIXME remove after writing mpeg4_probe
333     .value = CODEC_ID_H264,
334 };
335
336 #ifdef CONFIG_ENCODERS
337 AVOutputFormat h264_oformat = {
338     "h264",
339     "raw H264 video format",
340     NULL,
341     "h264",
342     0,
343     CODEC_ID_NONE,
344     CODEC_ID_H264,
345     raw_write_header,
346     raw_write_packet,
347     raw_write_trailer,
348 };
349 #endif //CONFIG_ENCODERS
350
351 AVInputFormat mpegvideo_iformat = {
352     "mpegvideo",
353     "MPEG video",
354     0,
355     mpegvideo_probe,
356     video_read_header,
357     raw_read_packet,
358     raw_read_close,
359     .value = CODEC_ID_MPEG1VIDEO,
360 };
361
362 #ifdef CONFIG_ENCODERS
363 AVOutputFormat mpeg1video_oformat = {
364     "mpeg1video",
365     "MPEG video",
366     "video/x-mpeg",
367     "mpg,mpeg",
368     0,
369     0,
370     CODEC_ID_MPEG1VIDEO,
371     raw_write_header,
372     raw_write_packet,
373     raw_write_trailer,
374 };
375 #endif //CONFIG_ENCODERS
376
377 AVInputFormat mjpeg_iformat = {
378     "mjpeg",
379     "MJPEG video",
380     0,
381     NULL,
382     video_read_header,
383     raw_read_packet,
384     raw_read_close,
385     .extensions = "mjpg,mjpeg",
386     .value = CODEC_ID_MJPEG,
387 };
388
389 #ifdef CONFIG_ENCODERS
390 AVOutputFormat mjpeg_oformat = {
391     "mjpeg",
392     "MJPEG video",
393     "video/x-mjpeg",
394     "mjpg,mjpeg",
395     0,
396     0,
397     CODEC_ID_MJPEG,
398     raw_write_header,
399     raw_write_packet,
400     raw_write_trailer,
401 };
402 #endif //CONFIG_ENCODERS
403
404 /* pcm formats */
405
406 #define PCMINPUTDEF(name, long_name, ext, codec) \
407 AVInputFormat pcm_ ## name ## _iformat = {\
408     #name,\
409     long_name,\
410     0,\
411     NULL,\
412     raw_read_header,\
413     raw_read_packet,\
414     raw_read_close,\
415     pcm_read_seek,\
416     .extensions = ext,\
417     .value = codec,\
418 };
419
420 #if !defined(CONFIG_ENCODERS) && defined(CONFIG_DECODERS)
421
422 #define PCMDEF(name, long_name, ext, codec) \
423     PCMINPUTDEF(name, long_name, ext, codec)
424
425 #else
426
427 #define PCMDEF(name, long_name, ext, codec) \
428     PCMINPUTDEF(name, long_name, ext, codec)\
429 \
430 AVOutputFormat pcm_ ## name ## _oformat = {\
431     #name,\
432     long_name,\
433     NULL,\
434     ext,\
435     0,\
436     codec,\
437     0,\
438     raw_write_header,\
439     raw_write_packet,\
440     raw_write_trailer,\
441 };
442 #endif //CONFIG_ENCODERS
443
444 #ifdef WORDS_BIGENDIAN
445 #define BE_DEF(s) s
446 #define LE_DEF(s) NULL
447 #else
448 #define BE_DEF(s) NULL
449 #define LE_DEF(s) s
450 #endif
451
452
453 PCMDEF(s16le, "pcm signed 16 bit little endian format", 
454        LE_DEF("sw"), CODEC_ID_PCM_S16LE)
455
456 PCMDEF(s16be, "pcm signed 16 bit big endian format", 
457        BE_DEF("sw"), CODEC_ID_PCM_S16BE)
458
459 PCMDEF(u16le, "pcm unsigned 16 bit little endian format", 
460        LE_DEF("uw"), CODEC_ID_PCM_U16LE)
461
462 PCMDEF(u16be, "pcm unsigned 16 bit big endian format", 
463        BE_DEF("uw"), CODEC_ID_PCM_U16BE)
464
465 PCMDEF(s8, "pcm signed 8 bit format", 
466        "sb", CODEC_ID_PCM_S8)
467
468 PCMDEF(u8, "pcm unsigned 8 bit format", 
469        "ub", CODEC_ID_PCM_U8)
470
471 PCMDEF(mulaw, "pcm mu law format", 
472        "ul", CODEC_ID_PCM_MULAW)
473
474 PCMDEF(alaw, "pcm A law format", 
475        "al", CODEC_ID_PCM_ALAW)
476
477 static int rawvideo_read_packet(AVFormatContext *s, AVPacket *pkt)
478 {
479     int packet_size, ret, width, height;
480     AVStream *st = s->streams[0];
481
482     width = st->codec.width;
483     height = st->codec.height;
484
485     packet_size = avpicture_get_size(st->codec.pix_fmt, width, height);
486     if (packet_size < 0)
487         av_abort();
488
489     if (av_new_packet(pkt, packet_size) < 0)
490         return -EIO;
491
492     pkt->stream_index = 0;
493 #if 0
494     /* bypass buffered I/O */
495     ret = url_read(url_fileno(&s->pb), pkt->data, pkt->size);
496 #else
497     ret = get_buffer(&s->pb, pkt->data, pkt->size);
498 #endif
499     if (ret != pkt->size) {
500         av_free_packet(pkt);
501         return -EIO;
502     } else {
503         return 0;
504     }
505 }
506
507 AVInputFormat rawvideo_iformat = {
508     "rawvideo",
509     "raw video format",
510     0,
511     NULL,
512     raw_read_header,
513     rawvideo_read_packet,
514     raw_read_close,
515     .extensions = "yuv",
516     .value = CODEC_ID_RAWVIDEO,
517 };
518
519 #ifdef CONFIG_ENCODERS
520 AVOutputFormat rawvideo_oformat = {
521     "rawvideo",
522     "raw video format",
523     NULL,
524     "yuv",
525     0,
526     CODEC_ID_NONE,
527     CODEC_ID_RAWVIDEO,
528     raw_write_header,
529     raw_write_packet,
530     raw_write_trailer,
531 };
532 #endif //CONFIG_ENCODERS
533
534 #ifdef CONFIG_ENCODERS
535 static int null_write_packet(struct AVFormatContext *s, 
536                              int stream_index,
537                              const uint8_t *buf, int size, int64_t pts)
538 {
539     return 0;
540 }
541
542 AVOutputFormat null_oformat = {
543     "null",
544     "null video format",
545     NULL,
546     NULL,
547     0,
548 #ifdef WORDS_BIGENDIAN
549     CODEC_ID_PCM_S16BE,
550 #else
551     CODEC_ID_PCM_S16LE,
552 #endif
553     CODEC_ID_RAWVIDEO,
554     raw_write_header,
555     null_write_packet,
556     raw_write_trailer,
557     .flags = AVFMT_NOFILE | AVFMT_RAWPICTURE,
558 };
559 #endif //CONFIG_ENCODERS
560
561 #ifndef CONFIG_ENCODERS
562 #define av_register_output_format(format)
563 #endif
564 #ifndef CONFIG_DECODERS
565 #define av_register_input_format(format)
566 #endif
567
568 int raw_init(void)
569 {
570     av_register_input_format(&ac3_iformat);
571     av_register_output_format(&ac3_oformat);
572
573     av_register_input_format(&h263_iformat);
574     av_register_output_format(&h263_oformat);
575     
576     av_register_input_format(&m4v_iformat);
577     av_register_output_format(&m4v_oformat);
578     
579     av_register_input_format(&h264_iformat);
580     av_register_output_format(&h264_oformat);
581
582     av_register_input_format(&mpegvideo_iformat);
583     av_register_output_format(&mpeg1video_oformat);
584
585     av_register_input_format(&mjpeg_iformat);
586     av_register_output_format(&mjpeg_oformat);
587
588     av_register_input_format(&pcm_s16le_iformat);
589     av_register_output_format(&pcm_s16le_oformat);
590     av_register_input_format(&pcm_s16be_iformat);
591     av_register_output_format(&pcm_s16be_oformat);
592     av_register_input_format(&pcm_u16le_iformat);
593     av_register_output_format(&pcm_u16le_oformat);
594     av_register_input_format(&pcm_u16be_iformat);
595     av_register_output_format(&pcm_u16be_oformat);
596     av_register_input_format(&pcm_s8_iformat);
597     av_register_output_format(&pcm_s8_oformat);
598     av_register_input_format(&pcm_u8_iformat);
599     av_register_output_format(&pcm_u8_oformat);
600     av_register_input_format(&pcm_mulaw_iformat);
601     av_register_output_format(&pcm_mulaw_oformat);
602     av_register_input_format(&pcm_alaw_iformat);
603     av_register_output_format(&pcm_alaw_oformat);
604
605     av_register_input_format(&rawvideo_iformat);
606     av_register_output_format(&rawvideo_oformat);
607
608     av_register_output_format(&null_oformat);
609     return 0;
610 }