]> git.sesse.net Git - ffmpeg/blob - libavformat/rawdec.c
Merge remote branch 'qatar/master'
[ffmpeg] / libavformat / rawdec.c
1 /*
2  * RAW demuxers
3  * Copyright (c) 2001 Fabrice Bellard
4  * Copyright (c) 2005 Alex Beregszaszi
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 #include "avformat.h"
24 #include "avio_internal.h"
25 #include "rawdec.h"
26
27 /* raw input */
28 int ff_raw_read_header(AVFormatContext *s, AVFormatParameters *ap)
29 {
30     AVStream *st;
31     enum CodecID id;
32
33     st = av_new_stream(s, 0);
34     if (!st)
35         return AVERROR(ENOMEM);
36
37         id = s->iformat->value;
38         if (id == CODEC_ID_RAWVIDEO) {
39             st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
40         } else {
41             st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
42         }
43         st->codec->codec_id = id;
44
45         switch(st->codec->codec_type) {
46         case AVMEDIA_TYPE_AUDIO:
47             st->codec->sample_rate = ap->sample_rate;
48             if(ap->channels) st->codec->channels = ap->channels;
49             else             st->codec->channels = 1;
50             st->codec->bits_per_coded_sample = av_get_bits_per_sample(st->codec->codec_id);
51             assert(st->codec->bits_per_coded_sample > 0);
52             st->codec->block_align = st->codec->bits_per_coded_sample*st->codec->channels/8;
53             av_set_pts_info(st, 64, 1, st->codec->sample_rate);
54             break;
55         case AVMEDIA_TYPE_VIDEO:
56             if(ap->time_base.num)
57                 av_set_pts_info(st, 64, ap->time_base.num, ap->time_base.den);
58             else
59                 av_set_pts_info(st, 64, 1, 25);
60             st->codec->width = ap->width;
61             st->codec->height = ap->height;
62             st->codec->pix_fmt = ap->pix_fmt;
63             if(st->codec->pix_fmt == PIX_FMT_NONE)
64                 st->codec->pix_fmt= PIX_FMT_YUV420P;
65             break;
66         default:
67             return -1;
68         }
69     return 0;
70 }
71
72 #define RAW_PACKET_SIZE 1024
73
74 int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
75 {
76     int ret, size;
77
78     size = RAW_PACKET_SIZE;
79
80     if (av_new_packet(pkt, size) < 0)
81         return AVERROR(ENOMEM);
82
83     pkt->pos= avio_tell(s->pb);
84     pkt->stream_index = 0;
85     ret = ffio_read_partial(s->pb, pkt->data, size);
86     if (ret < 0) {
87         av_free_packet(pkt);
88         return ret;
89     }
90     pkt->size = ret;
91     return ret;
92 }
93
94 int ff_raw_audio_read_header(AVFormatContext *s,
95                              AVFormatParameters *ap)
96 {
97     AVStream *st = av_new_stream(s, 0);
98     if (!st)
99         return AVERROR(ENOMEM);
100     st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
101     st->codec->codec_id = s->iformat->value;
102     st->need_parsing = AVSTREAM_PARSE_FULL;
103     st->start_time = 0;
104     /* the parameters will be extracted from the compressed bitstream */
105
106     return 0;
107 }
108
109 /* MPEG-1/H.263 input */
110 int ff_raw_video_read_header(AVFormatContext *s,
111                              AVFormatParameters *ap)
112 {
113     AVStream *st;
114
115     st = av_new_stream(s, 0);
116     if (!st)
117         return AVERROR(ENOMEM);
118
119     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
120     st->codec->codec_id = s->iformat->value;
121     st->need_parsing = AVSTREAM_PARSE_FULL;
122
123     /* for MJPEG, specify frame rate */
124     /* for MPEG-4 specify it, too (most MPEG-4 streams do not have the fixed_vop_rate set ...)*/
125     if (ap->time_base.num) {
126         st->codec->time_base= ap->time_base;
127     } else if ( st->codec->codec_id == CODEC_ID_MJPEG ||
128                 st->codec->codec_id == CODEC_ID_MPEG4 ||
129                 st->codec->codec_id == CODEC_ID_DIRAC ||
130                 st->codec->codec_id == CODEC_ID_DNXHD ||
131                 st->codec->codec_id == CODEC_ID_VC1   ||
132                 st->codec->codec_id == CODEC_ID_H264) {
133         st->codec->time_base= (AVRational){1,25};
134     }
135     av_set_pts_info(st, 64, 1, 1200000);
136
137     return 0;
138 }
139
140 /* Note: Do not forget to add new entries to the Makefile as well. */
141
142 #if CONFIG_G722_DEMUXER
143 AVInputFormat ff_g722_demuxer = {
144     "g722",
145     NULL_IF_CONFIG_SMALL("raw G.722"),
146     0,
147     NULL,
148     ff_raw_read_header,
149     ff_raw_read_partial_packet,
150     .flags= AVFMT_GENERIC_INDEX,
151     .extensions = "g722,722",
152     .value = CODEC_ID_ADPCM_G722,
153 };
154 #endif
155
156 #if CONFIG_GSM_DEMUXER
157 AVInputFormat ff_gsm_demuxer = {
158     "gsm",
159     NULL_IF_CONFIG_SMALL("raw GSM"),
160     0,
161     NULL,
162     ff_raw_audio_read_header,
163     ff_raw_read_partial_packet,
164     .flags= AVFMT_GENERIC_INDEX,
165     .extensions = "gsm",
166     .value = CODEC_ID_GSM,
167 };
168 #endif
169
170 #if CONFIG_MJPEG_DEMUXER
171 AVInputFormat ff_mjpeg_demuxer = {
172     "mjpeg",
173     NULL_IF_CONFIG_SMALL("raw MJPEG video"),
174     0,
175     NULL,
176     ff_raw_video_read_header,
177     ff_raw_read_partial_packet,
178     .flags= AVFMT_GENERIC_INDEX,
179     .extensions = "mjpg,mjpeg",
180     .value = CODEC_ID_MJPEG,
181 };
182 #endif
183
184 #if CONFIG_MLP_DEMUXER
185 AVInputFormat ff_mlp_demuxer = {
186     "mlp",
187     NULL_IF_CONFIG_SMALL("raw MLP"),
188     0,
189     NULL,
190     ff_raw_audio_read_header,
191     ff_raw_read_partial_packet,
192     .flags= AVFMT_GENERIC_INDEX,
193     .extensions = "mlp",
194     .value = CODEC_ID_MLP,
195 };
196 #endif
197
198 #if CONFIG_TRUEHD_DEMUXER
199 AVInputFormat ff_truehd_demuxer = {
200     "truehd",
201     NULL_IF_CONFIG_SMALL("raw TrueHD"),
202     0,
203     NULL,
204     ff_raw_audio_read_header,
205     ff_raw_read_partial_packet,
206     .flags= AVFMT_GENERIC_INDEX,
207     .extensions = "thd",
208     .value = CODEC_ID_TRUEHD,
209 };
210 #endif
211
212 #if CONFIG_SHORTEN_DEMUXER
213 AVInputFormat ff_shorten_demuxer = {
214     "shn",
215     NULL_IF_CONFIG_SMALL("raw Shorten"),
216     0,
217     NULL,
218     ff_raw_audio_read_header,
219     ff_raw_read_partial_packet,
220     .flags= AVFMT_GENERIC_INDEX,
221     .extensions = "shn",
222     .value = CODEC_ID_SHORTEN,
223 };
224 #endif
225
226 #if CONFIG_VC1_DEMUXER
227 AVInputFormat ff_vc1_demuxer = {
228     "vc1",
229     NULL_IF_CONFIG_SMALL("raw VC-1"),
230     0,
231     NULL /* vc1_probe */,
232     ff_raw_video_read_header,
233     ff_raw_read_partial_packet,
234     .extensions = "vc1",
235     .value = CODEC_ID_VC1,
236 };
237 #endif