3b1bea642950fc4875601a25b3d92402cd552d11
[libav.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 Libav.
7 *
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.
12 *
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.
17 *
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
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/intreadwrite.h"
31
32 #define RAW_PACKET_SIZE 1024
33
34 int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
35 {
36 int ret, size;
37
38 size = RAW_PACKET_SIZE;
39
40 if (av_new_packet(pkt, size) < 0)
41 return AVERROR(ENOMEM);
42
43 pkt->pos= avio_tell(s->pb);
44 pkt->stream_index = 0;
45 ret = ffio_read_partial(s->pb, pkt->data, size);
46 if (ret < 0) {
47 av_packet_unref(pkt);
48 return ret;
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);
53 }
54 pkt->size = ret;
55 return ret;
56 }
57
58 int ff_raw_audio_read_header(AVFormatContext *s)
59 {
60 AVStream *st = avformat_new_stream(s, NULL);
61 if (!st)
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;
66 st->start_time = 0;
67 /* the parameters will be extracted from the compressed bitstream */
68
69 return 0;
70 }
71
72 /* MPEG-1/H.263 input */
73 int ff_raw_video_read_header(AVFormatContext *s)
74 {
75 AVStream *st;
76 FFRawVideoDemuxerContext *s1 = s->priv_data;
77 AVRational framerate;
78 int ret = 0;
79
80
81 st = avformat_new_stream(s, NULL);
82 if (!st) {
83 ret = AVERROR(ENOMEM);
84 goto fail;
85 }
86
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;
90
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);
93 goto fail;
94 }
95
96 st->avg_frame_rate = framerate;
97 avpriv_set_pts_info(st, 64, framerate.den, framerate.num);
98
99 fail:
100 return ret;
101 }
102
103 /* Note: Do not forget to add new entries to the Makefile as well. */
104
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},
109 { NULL },
110 };
111
112 #if CONFIG_LATM_DEMUXER
113
114 #define LOAS_SYNC_WORD 0x2b7
115
116 static int latm_read_probe(AVProbeData *p)
117 {
118 int max_frames = 0, first_frames = 0;
119 int fsize, frames;
120 uint8_t *buf0 = p->buf;
121 uint8_t *buf2;
122 uint8_t *buf;
123 uint8_t *end = buf0 + p->buf_size - 3;
124
125 buf = buf0;
126
127 for (; buf < end; buf = buf2 + 1) {
128 buf2 = buf;
129
130 for (frames = 0; buf2 < end; frames++) {
131 uint32_t header = AV_RB24(buf2);
132 if ((header >> 13) != LOAS_SYNC_WORD) {
133 if (buf != buf0) {
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.
138 frames = 0;
139 }
140 break;
141 }
142 fsize = (header & 0x1FFF) + 3;
143 if (fsize < 7)
144 break;
145 buf2 += fsize;
146 }
147 max_frames = FFMAX(max_frames, frames);
148 if (buf == buf0)
149 first_frames = frames;
150 }
151
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)
159 return 1;
160 else
161 return 0;
162 }
163
164 AVInputFormat ff_latm_demuxer = {
165 .name = "latm",
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,
173 };
174 #endif
175
176 #if CONFIG_MJPEG_DEMUXER
177 FF_DEF_RAWVIDEO_DEMUXER(mjpeg, "raw MJPEG video", NULL, "mjpg,mjpeg", AV_CODEC_ID_MJPEG)
178 #endif
179
180 #if CONFIG_MLP_DEMUXER
181 AVInputFormat ff_mlp_demuxer = {
182 .name = "mlp",
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,
187 .extensions = "mlp",
188 .raw_codec_id = AV_CODEC_ID_MLP,
189 };
190 #endif
191
192 #if CONFIG_TRUEHD_DEMUXER
193 AVInputFormat ff_truehd_demuxer = {
194 .name = "truehd",
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,
199 .extensions = "thd",
200 .raw_codec_id = AV_CODEC_ID_TRUEHD,
201 };
202 #endif
203
204 #if CONFIG_SHORTEN_DEMUXER
205 AVInputFormat ff_shorten_demuxer = {
206 .name = "shn",
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,
211 .extensions = "shn",
212 .raw_codec_id = AV_CODEC_ID_SHORTEN,
213 };
214 #endif
215
216 #if CONFIG_VC1_DEMUXER
217 FF_DEF_RAWVIDEO_DEMUXER(vc1, "raw VC-1", NULL, "vc1", AV_CODEC_ID_VC1)
218 #endif