rawdec.c
Go to the documentation of this file.
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 
31 /* raw input */
33 {
34  AVStream *st;
35  enum CodecID id;
36 
37  st = avformat_new_stream(s, NULL);
38  if (!st)
39  return AVERROR(ENOMEM);
40 
41  id = s->iformat->value;
42  if (id == CODEC_ID_RAWVIDEO) {
44  } else {
46  }
47  st->codec->codec_id = id;
48 
49  switch(st->codec->codec_type) {
50  case AVMEDIA_TYPE_AUDIO: {
52 
53  st->codec->channels = 1;
54 
55  if (id == CODEC_ID_ADPCM_G722)
56  st->codec->sample_rate = 16000;
57 
58  if (s1 && s1->sample_rate)
59  st->codec->sample_rate = s1->sample_rate;
60  if (s1 && s1->channels)
61  st->codec->channels = s1->channels;
62 
64  assert(st->codec->bits_per_coded_sample > 0);
66  avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
67  break;
68  }
69  case AVMEDIA_TYPE_VIDEO: {
71  int width = 0, height = 0, ret = 0;
72  enum PixelFormat pix_fmt;
73  AVRational framerate;
74 
75  if (s1->video_size && (ret = av_parse_video_size(&width, &height, s1->video_size)) < 0) {
76  av_log(s, AV_LOG_ERROR, "Couldn't parse video size.\n");
77  goto fail;
78  }
79  if ((pix_fmt = av_get_pix_fmt(s1->pixel_format)) == PIX_FMT_NONE) {
80  av_log(s, AV_LOG_ERROR, "No such pixel format: %s.\n", s1->pixel_format);
81  ret = AVERROR(EINVAL);
82  goto fail;
83  }
84  if ((ret = av_parse_video_rate(&framerate, s1->framerate)) < 0) {
85  av_log(s, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s1->framerate);
86  goto fail;
87  }
88  avpriv_set_pts_info(st, 64, framerate.den, framerate.num);
89  st->codec->width = width;
90  st->codec->height = height;
91  st->codec->pix_fmt = pix_fmt;
92 fail:
93  return ret;
94  }
95  default:
96  return -1;
97  }
98  return 0;
99 }
100 
101 #define RAW_PACKET_SIZE 1024
102 
104 {
105  int ret, size;
106 
107  size = RAW_PACKET_SIZE;
108 
109  if (av_new_packet(pkt, size) < 0)
110  return AVERROR(ENOMEM);
111 
112  pkt->pos= avio_tell(s->pb);
113  pkt->stream_index = 0;
114  ret = ffio_read_partial(s->pb, pkt->data, size);
115  if (ret < 0) {
116  av_free_packet(pkt);
117  return ret;
118  }
119  pkt->size = ret;
120  return ret;
121 }
122 
124  AVFormatParameters *ap)
125 {
127  if (!st)
128  return AVERROR(ENOMEM);
130  st->codec->codec_id = s->iformat->value;
132  st->start_time = 0;
133  /* the parameters will be extracted from the compressed bitstream */
134 
135  return 0;
136 }
137 
138 /* MPEG-1/H.263 input */
140  AVFormatParameters *ap)
141 {
142  AVStream *st;
144  AVRational framerate;
145  int ret = 0;
146 
147 
148  st = avformat_new_stream(s, NULL);
149  if (!st) {
150  ret = AVERROR(ENOMEM);
151  goto fail;
152  }
153 
155  st->codec->codec_id = s->iformat->value;
157 
158  if ((ret = av_parse_video_rate(&framerate, s1->framerate)) < 0) {
159  av_log(s, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s1->framerate);
160  goto fail;
161  }
162 
163  st->r_frame_rate = st->avg_frame_rate = framerate;
164  avpriv_set_pts_info(st, 64, 1, 1200000);
165 
166 fail:
167  return ret;
168 }
169 
170 /* Note: Do not forget to add new entries to the Makefile as well. */
171 
172 #define OFFSET(x) offsetof(FFRawVideoDemuxerContext, x)
173 #define DEC AV_OPT_FLAG_DECODING_PARAM
175  { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC},
176  { NULL },
177 };
178 
179 #if CONFIG_G722_DEMUXER
180 AVInputFormat ff_g722_demuxer = {
181  .name = "g722",
182  .long_name = NULL_IF_CONFIG_SMALL("raw G.722"),
183  .read_header = ff_raw_read_header,
184  .read_packet = ff_raw_read_partial_packet,
185  .flags= AVFMT_GENERIC_INDEX,
186  .extensions = "g722,722",
187  .value = CODEC_ID_ADPCM_G722,
188 };
189 #endif
190 
191 #if CONFIG_LATM_DEMUXER
192 AVInputFormat ff_latm_demuxer = {
193  .name = "latm",
194  .long_name = NULL_IF_CONFIG_SMALL("raw LOAS/LATM"),
195  .read_header = ff_raw_audio_read_header,
196  .read_packet = ff_raw_read_partial_packet,
197  .flags= AVFMT_GENERIC_INDEX,
198  .extensions = "latm",
199  .value = CODEC_ID_AAC_LATM,
200 };
201 #endif
202 
203 #if CONFIG_MJPEG_DEMUXER
204 FF_DEF_RAWVIDEO_DEMUXER(mjpeg, "raw MJPEG video", NULL, "mjpg,mjpeg", CODEC_ID_MJPEG)
205 #endif
206 
207 #if CONFIG_MLP_DEMUXER
208 AVInputFormat ff_mlp_demuxer = {
209  .name = "mlp",
210  .long_name = NULL_IF_CONFIG_SMALL("raw MLP"),
211  .read_header = ff_raw_audio_read_header,
212  .read_packet = ff_raw_read_partial_packet,
213  .flags= AVFMT_GENERIC_INDEX,
214  .extensions = "mlp",
215  .value = CODEC_ID_MLP,
216 };
217 #endif
218 
219 #if CONFIG_TRUEHD_DEMUXER
220 AVInputFormat ff_truehd_demuxer = {
221  .name = "truehd",
222  .long_name = NULL_IF_CONFIG_SMALL("raw TrueHD"),
223  .read_header = ff_raw_audio_read_header,
224  .read_packet = ff_raw_read_partial_packet,
225  .flags= AVFMT_GENERIC_INDEX,
226  .extensions = "thd",
227  .value = CODEC_ID_TRUEHD,
228 };
229 #endif
230 
231 #if CONFIG_SHORTEN_DEMUXER
232 AVInputFormat ff_shorten_demuxer = {
233  .name = "shn",
234  .long_name = NULL_IF_CONFIG_SMALL("raw Shorten"),
235  .read_header = ff_raw_audio_read_header,
236  .read_packet = ff_raw_read_partial_packet,
238  .extensions = "shn",
239  .value = CODEC_ID_SHORTEN,
240 };
241 #endif
242 
243 #if CONFIG_VC1_DEMUXER
244 FF_DEF_RAWVIDEO_DEMUXER(vc1, "raw VC-1", NULL, "vc1", CODEC_ID_VC1)
245 #endif
const AVOption ff_rawvideo_options[]
Definition: rawdec.c:174
enum PixelFormat pix_fmt
Pixel format, see PIX_FMT_xxx.
Definition: avcodec.h:1426
char * framerate
String describing framerate, set by a private option.
Definition: rawdec.h:39
#define AVFMT_NOBINSEARCH
Format does not allow to fallback to binary search via read_timestamp.
Definition: avformat.h:381
int size
int av_parse_video_rate(AVRational *rate, const char *arg)
Parse str and store the detected values in *rate.
Definition: parseutils.c:123
AVOption.
Definition: opt.h:244
int ff_raw_video_read_header(AVFormatContext *s, AVFormatParameters *ap)
Definition: rawdec.c:139
int av_parse_video_size(int *width_ptr, int *height_ptr, const char *str)
Parse str and put in width_ptr and height_ptr the detected values.
Definition: parseutils.c:95
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:933
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the pts for a given stream.
Definition: utils.c:3828
#define FF_DEF_RAWVIDEO_DEMUXER(shortname, longname, probe, ext, id)
Definition: rawdec.h:60
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:60
enum PixelFormat pix_fmt
Definition: v4l.c:65
int num
numerator
Definition: rational.h:44
int size
Definition: avcodec.h:909
AVOptions.
int width
Definition: rotozoom.c:164
char * video_size
String describing video size, set by a private option.
Definition: rawdec.h:37
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:1751
Format I/O context.
Definition: avformat.h:863
enum AVStreamParseType need_parsing
Definition: avformat.h:815
uint8_t * data
Definition: avcodec.h:908
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:151
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:492
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1974
int av_get_bits_per_sample(enum CodecID codec_id)
Return codec bits per sample.
Definition: utils.c:1634
#define AVERROR(e)
Definition: error.h:43
int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rawdec.c:103
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:191
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
AVStream * avformat_new_stream(AVFormatContext *s, AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2776
int ffio_read_partial(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:699
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:755
char * pixel_format
Set by a private option.
Definition: rawdec.h:38
AVCodecContext * codec
codec context
Definition: avformat.h:623
int ff_raw_read_header(AVFormatContext *s, AVFormatParameters *ap)
Definition: rawdec.c:32
int width
picture width / height.
Definition: avcodec.h:1408
#define RAW_PACKET_SIZE
Definition: rawdec.c:101
#define OFFSET(x)
Definition: rawdec.c:172
Stream structure.
Definition: avformat.h:620
NULL
Definition: eval.c:50
enum AVMediaType codec_type
Definition: avcodec.h:1574
int value
General purpose read-only value that the format can use.
Definition: avformat.h:542
int sample_rate
samples per second
Definition: avcodec.h:1456
AVIOContext * pb
Definition: avformat.h:896
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
#define AVFMT_NOGENSEARCH
Format does not allow to fallback to generic search.
Definition: avformat.h:382
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:376
rational number numerator/denominator
Definition: rational.h:43
enum CodecID id
Definition: mxfenc.c:85
#define s1
Definition: regdef.h:38
int ff_raw_audio_read_header(AVFormatContext *s, AVFormatParameters *ap)
Definition: rawdec.c:123
misc parsing utilities
int height
Definition: gxfenc.c:73
full parsing and repack
Definition: avformat.h:581
PixelFormat
Pixel format.
Definition: pixfmt.h:62
Main libavformat public API header.
int64_t start_time
Decoding: pts of the first frame of the stream, in stream time base.
Definition: avformat.h:677
int den
denominator
Definition: rational.h:45
struct AVInputFormat * iformat
Can only be iformat or oformat, not both at the same time.
Definition: avformat.h:876
CodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:83
#define AVFMT_NO_BYTE_SEEK
Format does not allow seeking by bytes.
Definition: avformat.h:383
int channels
number of audio channels
Definition: avcodec.h:1457
void * priv_data
Format private data.
Definition: avformat.h:883
#define DEC
Definition: rawdec.c:173
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:460
int stream_index
Definition: avcodec.h:910
AVRational r_frame_rate
Real base framerate of the stream.
Definition: avformat.h:632
enum PixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
Definition: pixdesc.c:1116
enum CodecID codec_id
Definition: avcodec.h:1575