3 * Copyright (c) 2001 Fabrice Bellard
4 * Copyright (c) 2005 Alex Beregszaszi
6 * This file is part of Libav.
8 * Libav 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.
13 * Libav 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.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "avio_internal.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/parseutils.h"
29 #include "libavutil/pixdesc.h"
30 #include "libavutil/intreadwrite.h"
32 #define RAW_PACKET_SIZE 1024
34 int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
38 size = RAW_PACKET_SIZE;
40 if (av_new_packet(pkt, size) < 0)
41 return AVERROR(ENOMEM);
43 pkt->pos= avio_tell(s->pb);
44 pkt->stream_index = 0;
45 ret = avio_read_partial(s->pb, pkt->data, size);
49 } else if (ret < size) {
50 /* initialize end of packet for partial reads to avoid reading
51 * uninitialized data on allowed overreads */
52 memset(pkt->data + ret, 0, AV_INPUT_BUFFER_PADDING_SIZE);
58 int ff_raw_audio_read_header(AVFormatContext *s)
60 AVStream *st = avformat_new_stream(s, NULL);
62 return AVERROR(ENOMEM);
63 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
64 st->codecpar->codec_id = s->iformat->raw_codec_id;
65 st->need_parsing = AVSTREAM_PARSE_FULL;
67 /* the parameters will be extracted from the compressed bitstream */
72 /* MPEG-1/H.263 input */
73 int ff_raw_video_read_header(AVFormatContext *s)
76 FFRawVideoDemuxerContext *s1 = s->priv_data;
81 st = avformat_new_stream(s, NULL);
83 ret = AVERROR(ENOMEM);
87 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
88 st->codecpar->codec_id = s->iformat->raw_codec_id;
89 st->need_parsing = AVSTREAM_PARSE_FULL;
91 if ((ret = av_parse_video_rate(&framerate, s1->framerate)) < 0) {
92 av_log(s, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s1->framerate);
96 st->avg_frame_rate = framerate;
97 avpriv_set_pts_info(st, 64, framerate.den, framerate.num);
103 /* Note: Do not forget to add new entries to the Makefile as well. */
105 #define OFFSET(x) offsetof(FFRawVideoDemuxerContext, x)
106 #define DEC AV_OPT_FLAG_DECODING_PARAM
107 const AVOption ff_rawvideo_options[] = {
108 { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC},
112 #if CONFIG_LATM_DEMUXER
114 #define LOAS_SYNC_WORD 0x2b7
116 static int latm_read_probe(AVProbeData *p)
118 int max_frames = 0, first_frames = 0;
120 uint8_t *buf0 = p->buf;
123 uint8_t *end = buf0 + p->buf_size - 3;
127 for (; buf < end; buf = buf2 + 1) {
130 for (frames = 0; buf2 < end; frames++) {
131 uint32_t header = AV_RB24(buf2);
132 if ((header >> 13) != LOAS_SYNC_WORD) {
134 // Found something that isn't a LOAS header, starting
135 // from a position other than the start of the buffer.
136 // Discard the count we've accumulated so far since it
137 // probably was a false positive.
142 fsize = (header & 0x1FFF) + 3;
147 max_frames = FFMAX(max_frames, frames);
149 first_frames = frames;
152 if (first_frames >= 3)
153 return AVPROBE_SCORE_EXTENSION + 1;
154 else if (max_frames > 100)
155 return AVPROBE_SCORE_EXTENSION;
156 else if (max_frames >= 3)
157 return AVPROBE_SCORE_EXTENSION / 2;
158 else if (max_frames > 1)
164 AVInputFormat ff_latm_demuxer = {
166 .long_name = NULL_IF_CONFIG_SMALL("raw LOAS/LATM"),
167 .read_probe = latm_read_probe,
168 .read_header = ff_raw_audio_read_header,
169 .read_packet = ff_raw_read_partial_packet,
170 .flags = AVFMT_GENERIC_INDEX,
171 .extensions = "latm",
172 .raw_codec_id = AV_CODEC_ID_AAC_LATM,
176 #if CONFIG_MJPEG_DEMUXER
177 FF_DEF_RAWVIDEO_DEMUXER(mjpeg, "raw MJPEG video", NULL, "mjpg,mjpeg", AV_CODEC_ID_MJPEG)
180 #if CONFIG_MLP_DEMUXER
181 AVInputFormat ff_mlp_demuxer = {
183 .long_name = NULL_IF_CONFIG_SMALL("raw MLP"),
184 .read_header = ff_raw_audio_read_header,
185 .read_packet = ff_raw_read_partial_packet,
186 .flags = AVFMT_GENERIC_INDEX,
188 .raw_codec_id = AV_CODEC_ID_MLP,
192 #if CONFIG_TRUEHD_DEMUXER
193 AVInputFormat ff_truehd_demuxer = {
195 .long_name = NULL_IF_CONFIG_SMALL("raw TrueHD"),
196 .read_header = ff_raw_audio_read_header,
197 .read_packet = ff_raw_read_partial_packet,
198 .flags = AVFMT_GENERIC_INDEX,
200 .raw_codec_id = AV_CODEC_ID_TRUEHD,
204 #if CONFIG_SHORTEN_DEMUXER
205 AVInputFormat ff_shorten_demuxer = {
207 .long_name = NULL_IF_CONFIG_SMALL("raw Shorten"),
208 .read_header = ff_raw_audio_read_header,
209 .read_packet = ff_raw_read_partial_packet,
210 .flags = AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH | AVFMT_NO_BYTE_SEEK,
212 .raw_codec_id = AV_CODEC_ID_SHORTEN,
216 #if CONFIG_VC1_DEMUXER
217 FF_DEF_RAWVIDEO_DEMUXER(vc1, "raw VC-1", NULL, "vc1", AV_CODEC_ID_VC1)