PageRenderTime 22ms CodeModel.GetById 10ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/libavformat/rawdec.c

http://github.com/FFmpeg/FFmpeg
C | 214 lines | 163 code | 27 blank | 24 comment | 26 complexity | 940d3c7c69bc86661c0b045e692f6566 MD5 | raw file
  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
 35int 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
 56int 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 */
 71int 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
 91fail:
 92    return ret;
 93}
 94
 95int 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
106int 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
121const 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};
126const AVOption ff_raw_options[] = {
127    { "raw_packet_size", "", OFFSET(raw_packet_size), AV_OPT_TYPE_INT, {.i64 = RAW_PACKET_SIZE }, 1, INT_MAX, DEC},
128    { NULL },
129};
130
131#if CONFIG_DATA_DEMUXER
132FF_RAW_DEMUXER_CLASS(raw_data)
133
134AVInputFormat ff_data_demuxer = {
135    .name           = "data",
136    .long_name      = NULL_IF_CONFIG_SMALL("raw data"),
137    .read_header    = ff_raw_data_read_header,
138    .read_packet    = ff_raw_read_partial_packet,
139    .raw_codec_id   = AV_CODEC_ID_NONE,
140    .flags          = AVFMT_NOTIMESTAMPS,
141    .priv_data_size = sizeof(FFRawDemuxerContext),\
142    .priv_class     = &raw_data_demuxer_class,\
143};
144#endif
145
146#if CONFIG_MJPEG_DEMUXER
147static int mjpeg_probe(const AVProbeData *p)
148{
149    int i;
150    int state = -1;
151    int nb_invalid = 0;
152    int nb_frames = 0;
153
154    for (i = 0; i < p->buf_size - 1; i++) {
155        int c;
156        if (p->buf[i] != 0xFF)
157            continue;
158        c = p->buf[i+1];
159        switch (c) {
160        case 0xD8:
161            state = 0xD8;
162            break;
163        case 0xC0:
164        case 0xC1:
165        case 0xC2:
166        case 0xC3:
167        case 0xC5:
168        case 0xC6:
169        case 0xC7:
170        case 0xF7:
171            if (state == 0xD8) {
172                state = 0xC0;
173            } else
174                nb_invalid++;
175            break;
176        case 0xDA:
177            if (state == 0xC0) {
178                state = 0xDA;
179            } else
180                nb_invalid++;
181            break;
182        case 0xD9:
183            if (state == 0xDA) {
184                state = 0xD9;
185                nb_frames++;
186            } else
187                nb_invalid++;
188            break;
189        default:
190            if (  (c >= 0x02 && c <= 0xBF)
191                || c == 0xC8) {
192                nb_invalid++;
193            }
194        }
195    }
196
197    if (nb_invalid*4 + 1 < nb_frames) {
198        static const char ct_jpeg[] = "\r\nContent-Type: image/jpeg\r\n";
199        int i;
200
201        for (i=0; i<FFMIN(p->buf_size - (int)sizeof(ct_jpeg), 100); i++)
202            if (!memcmp(p->buf + i, ct_jpeg, sizeof(ct_jpeg) - 1))
203                return AVPROBE_SCORE_EXTENSION;
204
205        if (nb_invalid == 0 && nb_frames > 2)
206            return AVPROBE_SCORE_EXTENSION / 2;
207        return AVPROBE_SCORE_EXTENSION / 4;
208    }
209
210    return 0;
211}
212
213FF_DEF_RAWVIDEO_DEMUXER2(mjpeg, "raw MJPEG video", mjpeg_probe, "mjpg,mjpeg,mpo", AV_CODEC_ID_MJPEG, AVFMT_GENERIC_INDEX|AVFMT_NOTIMESTAMPS)
214#endif