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