Imported Upstream version 6.1
[platform/upstream/ffmpeg.git] / 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 "config_components.h"
24
25 #include "avformat.h"
26 #include "internal.h"
27 #include "rawdec.h"
28 #include "libavutil/opt.h"
29
30 #define RAW_PACKET_SIZE 1024
31
32 int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
33 {
34     FFRawDemuxerContext *raw = s->priv_data;
35     int ret, size;
36
37     size = raw->raw_packet_size;
38
39     if ((ret = av_new_packet(pkt, size)) < 0)
40         return ret;
41
42     pkt->pos= avio_tell(s->pb);
43     pkt->stream_index = 0;
44     ret = avio_read_partial(s->pb, pkt->data, size);
45     if (ret < 0) {
46         av_packet_unref(pkt);
47         return ret;
48     }
49     av_shrink_packet(pkt, ret);
50     return ret;
51 }
52
53 int ff_raw_audio_read_header(AVFormatContext *s)
54 {
55     AVStream *st = avformat_new_stream(s, NULL);
56     if (!st)
57         return AVERROR(ENOMEM);
58     st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
59     st->codecpar->codec_id = s->iformat->raw_codec_id;
60     ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
61     st->start_time = 0;
62     /* the parameters will be extracted from the compressed bitstream */
63
64     return 0;
65 }
66
67 /* MPEG-1/H.263 input */
68 int ff_raw_video_read_header(AVFormatContext *s)
69 {
70     AVStream *st;
71     FFStream *sti;
72     FFRawVideoDemuxerContext *s1 = s->priv_data;
73     int ret = 0;
74
75
76     st = avformat_new_stream(s, NULL);
77     if (!st) {
78         ret = AVERROR(ENOMEM);
79         goto fail;
80     }
81     sti = ffstream(st);
82
83     st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
84     st->codecpar->codec_id = s->iformat->raw_codec_id;
85     sti->need_parsing = AVSTREAM_PARSE_FULL_RAW;
86
87     st->avg_frame_rate = s1->framerate;
88     avpriv_set_pts_info(st, 64, 1, 1200000);
89
90 fail:
91     return ret;
92 }
93
94 int ff_raw_subtitle_read_header(AVFormatContext *s)
95 {
96     AVStream *st = avformat_new_stream(s, NULL);
97     if (!st)
98         return AVERROR(ENOMEM);
99     st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
100     st->codecpar->codec_id = s->iformat->raw_codec_id;
101     st->start_time = 0;
102     return 0;
103 }
104
105 static int raw_data_read_header(AVFormatContext *s)
106 {
107     AVStream *st = avformat_new_stream(s, NULL);
108     if (!st)
109         return AVERROR(ENOMEM);
110     st->codecpar->codec_type = AVMEDIA_TYPE_DATA;
111     st->codecpar->codec_id = s->iformat->raw_codec_id;
112     st->start_time = 0;
113     return 0;
114 }
115
116 /* Note: Do not forget to add new entries to the Makefile as well. */
117
118 #define OFFSET(x) offsetof(FFRawVideoDemuxerContext, x)
119 #define DEC AV_OPT_FLAG_DECODING_PARAM
120 static const AVOption rawvideo_options[] = {
121     { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC},
122     { "raw_packet_size", "", OFFSET(raw_packet_size), AV_OPT_TYPE_INT, {.i64 = RAW_PACKET_SIZE }, 1, INT_MAX, DEC},
123     { NULL },
124 };
125 #undef OFFSET
126
127 const AVClass ff_rawvideo_demuxer_class = {
128     .class_name = "generic raw video demuxer",
129     .item_name  = av_default_item_name,
130     .option     = rawvideo_options,
131     .version    = LIBAVUTIL_VERSION_INT,
132 };
133
134 #define OFFSET(x) offsetof(FFRawDemuxerContext, x)
135 static const AVOption raw_options[] = {
136     { "raw_packet_size", "", OFFSET(raw_packet_size), AV_OPT_TYPE_INT, {.i64 = RAW_PACKET_SIZE }, 1, INT_MAX, DEC},
137     { NULL },
138 };
139
140 const AVClass ff_raw_demuxer_class = {
141     .class_name = "generic raw demuxer",
142     .item_name  = av_default_item_name,
143     .option     = raw_options,
144     .version    = LIBAVUTIL_VERSION_INT,
145 };
146
147 #if CONFIG_DATA_DEMUXER
148 const AVInputFormat ff_data_demuxer = {
149     .name           = "data",
150     .long_name      = NULL_IF_CONFIG_SMALL("raw data"),
151     .read_header    = raw_data_read_header,
152     .read_packet    = ff_raw_read_partial_packet,
153     .raw_codec_id   = AV_CODEC_ID_NONE,
154     .flags          = AVFMT_NOTIMESTAMPS,
155     .priv_data_size = sizeof(FFRawDemuxerContext),\
156     .priv_class     = &ff_raw_demuxer_class,
157 };
158 #endif
159
160 #if CONFIG_MJPEG_DEMUXER
161 static int mjpeg_probe(const AVProbeData *p)
162 {
163     int i;
164     int state = -1;
165     int nb_invalid = 0;
166     int nb_frames = 0;
167
168     for (i = 0; i < p->buf_size - 1; i++) {
169         int c;
170         if (p->buf[i] != 0xFF)
171             continue;
172         c = p->buf[i+1];
173         switch (c) {
174         case 0xD8:
175             state = 0xD8;
176             break;
177         case 0xC0:
178         case 0xC1:
179         case 0xC2:
180         case 0xC3:
181         case 0xC5:
182         case 0xC6:
183         case 0xC7:
184         case 0xF7:
185             if (state == 0xD8) {
186                 state = 0xC0;
187             } else
188                 nb_invalid++;
189             break;
190         case 0xDA:
191             if (state == 0xC0) {
192                 state = 0xDA;
193             } else
194                 nb_invalid++;
195             break;
196         case 0xD9:
197             if (state == 0xDA) {
198                 state = 0xD9;
199                 nb_frames++;
200             } else
201                 nb_invalid++;
202             break;
203         default:
204             if (  (c >= 0x02 && c <= 0xBF)
205                 || c == 0xC8) {
206                 nb_invalid++;
207             }
208         }
209     }
210
211     if (nb_invalid*4 + 1 < nb_frames) {
212         static const char ct_jpeg[] = "\r\nContent-Type: image/jpeg\r\n";
213         int i;
214
215         for (i=0; i<FFMIN(p->buf_size - (int)sizeof(ct_jpeg), 100); i++)
216             if (!memcmp(p->buf + i, ct_jpeg, sizeof(ct_jpeg) - 1))
217                 return AVPROBE_SCORE_EXTENSION;
218
219         if (nb_invalid == 0 && nb_frames > 2)
220             return AVPROBE_SCORE_EXTENSION / 2;
221         return AVPROBE_SCORE_EXTENSION / 4;
222     }
223     if (!nb_invalid && nb_frames)
224         return AVPROBE_SCORE_EXTENSION / 4;
225
226     return 0;
227 }
228
229 FF_DEF_RAWVIDEO_DEMUXER2(mjpeg, "raw MJPEG video", mjpeg_probe, "mjpg,mjpeg,mpo", AV_CODEC_ID_MJPEG, AVFMT_GENERIC_INDEX|AVFMT_NOTIMESTAMPS)
230 #endif