Libav
img2dec.c
Go to the documentation of this file.
1 /*
2  * Image format
3  * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4  * Copyright (c) 2004 Michael Niedermayer
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 "libavutil/avstring.h"
24 #include "libavutil/log.h"
25 #include "libavutil/opt.h"
26 #include "libavutil/pixdesc.h"
27 #include "libavutil/parseutils.h"
28 #include "avformat.h"
29 #include "internal.h"
30 
31 typedef struct VideoDemuxData {
32  const AVClass *class;
33  int img_first;
34  int img_last;
36  int img_count;
37  int is_pipe;
38  char path[1024];
39  char *pixel_format;
40  char *video_size;
41  char *framerate;
42  int loop;
45 
46 static const int sizes[][2] = {
47  { 640, 480 },
48  { 720, 480 },
49  { 720, 576 },
50  { 352, 288 },
51  { 352, 240 },
52  { 160, 128 },
53  { 512, 384 },
54  { 640, 352 },
55  { 640, 240 },
56 };
57 
58 static int infer_size(int *width_ptr, int *height_ptr, int size)
59 {
60  int i;
61 
62  for (i = 0; i < FF_ARRAY_ELEMS(sizes); i++) {
63  if ((sizes[i][0] * sizes[i][1]) == size) {
64  *width_ptr = sizes[i][0];
65  *height_ptr = sizes[i][1];
66  return 0;
67  }
68  }
69 
70  return -1;
71 }
72 
73 /* return -1 if no image found */
74 static int find_image_range(int *pfirst_index, int *plast_index,
75  const char *path, int max_start)
76 {
77  char buf[1024];
78  int range, last_index, range1, first_index;
79 
80  /* find the first image */
81  for (first_index = 0; first_index < max_start; first_index++) {
82  if (av_get_frame_filename(buf, sizeof(buf), path, first_index) < 0) {
83  *pfirst_index =
84  *plast_index = 1;
85  if (avio_check(buf, AVIO_FLAG_READ) > 0)
86  return 0;
87  return -1;
88  }
89  if (avio_check(buf, AVIO_FLAG_READ) > 0)
90  break;
91  }
92  if (first_index == 5)
93  goto fail;
94 
95  /* find the last image */
96  last_index = first_index;
97  for (;;) {
98  range = 0;
99  for (;;) {
100  if (!range)
101  range1 = 1;
102  else
103  range1 = 2 * range;
104  if (av_get_frame_filename(buf, sizeof(buf), path,
105  last_index + range1) < 0)
106  goto fail;
107  if (avio_check(buf, AVIO_FLAG_READ) <= 0)
108  break;
109  range = range1;
110  /* just in case... */
111  if (range >= (1 << 30))
112  goto fail;
113  }
114  /* we are sure than image last_index + range exists */
115  if (!range)
116  break;
117  last_index += range;
118  }
119  *pfirst_index = first_index;
120  *plast_index = last_index;
121  return 0;
122 
123 fail:
124  return -1;
125 }
126 
128 {
129  if (p->filename && ff_guess_image2_codec(p->filename)) {
131  return AVPROBE_SCORE_MAX;
132  else
134  }
135  return 0;
136 }
137 
139 {
140  VideoDemuxData *s = s1->priv_data;
141  int first_index, last_index, ret = 0;
142  int width = 0, height = 0;
143  AVStream *st;
146 
148 
149  st = avformat_new_stream(s1, NULL);
150  if (!st) {
151  return AVERROR(ENOMEM);
152  }
153 
154  if (s->pixel_format &&
155  (pix_fmt = av_get_pix_fmt(s->pixel_format)) == AV_PIX_FMT_NONE) {
156  av_log(s1, AV_LOG_ERROR, "No such pixel format: %s.\n",
157  s->pixel_format);
158  return AVERROR(EINVAL);
159  }
160  if (s->video_size &&
161  (ret = av_parse_video_size(&width, &height, s->video_size)) < 0) {
162  av_log(s, AV_LOG_ERROR,
163  "Could not parse video size: %s.\n", s->video_size);
164  return ret;
165  }
166  if ((ret = av_parse_video_rate(&framerate, s->framerate)) < 0) {
167  av_log(s, AV_LOG_ERROR,
168  "Could not parse framerate: %s.\n", s->framerate);
169  return ret;
170  }
171 
172  av_strlcpy(s->path, s1->filename, sizeof(s->path));
173  s->img_number = 0;
174  s->img_count = 0;
175 
176  /* find format */
177  if (s1->iformat->flags & AVFMT_NOFILE)
178  s->is_pipe = 0;
179  else {
180  s->is_pipe = 1;
182  }
183 
184  avpriv_set_pts_info(st, 60, framerate.den, framerate.num);
185 
186  if (width && height) {
187  st->codecpar->width = width;
188  st->codecpar->height = height;
189  }
190 
191  if (!s->is_pipe) {
192  if (find_image_range(&first_index, &last_index, s->path,
193  FFMAX(s->start_number, 5)) < 0)
194  return AVERROR(ENOENT);
195  s->img_first = first_index;
196  s->img_last = last_index;
197  s->img_number = s->start_number != 1 ? s->start_number : first_index;
198  /* compute duration */
199  st->start_time = 0;
200  st->duration = last_index - first_index + 1;
201  }
202 
203  if (s1->video_codec_id) {
205  st->codecpar->codec_id = s1->video_codec_id;
206  } else if (s1->audio_codec_id) {
208  st->codecpar->codec_id = s1->audio_codec_id;
209  } else {
212  }
213  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
214  pix_fmt != AV_PIX_FMT_NONE)
215  st->codecpar->format = pix_fmt;
216 
217  return 0;
218 }
219 
221 {
222  VideoDemuxData *s = s1->priv_data;
223  char filename[1024];
224  int i, res;
225  int size[3] = { 0 }, ret[3] = { 0 };
226  AVIOContext *f[3] = { NULL };
227  AVCodecParameters *par = s1->streams[0]->codecpar;
228 
229  if (!s->is_pipe) {
230  /* loop over input */
231  if (s->loop && s->img_number > s->img_last) {
232  s->img_number = s->img_first;
233  }
234  if (s->img_number > s->img_last)
235  return AVERROR_EOF;
236  if (av_get_frame_filename(filename, sizeof(filename),
237  s->path,
238  s->img_number) < 0 && s->img_number > 1)
239  return AVERROR(EIO);
240  for (i = 0; i < 3; i++) {
241  if (s1->io_open(s1, &f[i], filename, AVIO_FLAG_READ, NULL) < 0) {
242  if (i >= 1)
243  break;
244  av_log(s1, AV_LOG_ERROR, "Could not open file : %s\n",
245  filename);
246  return AVERROR(EIO);
247  }
248  size[i] = avio_size(f[i]);
249 
250  if (par->codec_id != AV_CODEC_ID_RAWVIDEO)
251  break;
252  filename[strlen(filename) - 1] = 'U' + i;
253  }
254 
255  if (par->codec_id == AV_CODEC_ID_RAWVIDEO && !par->width)
256  infer_size(&par->width, &par->height, size[0]);
257  } else {
258  f[0] = s1->pb;
259  if (f[0]->eof_reached)
260  return AVERROR(EIO);
261  size[0] = 4096;
262  }
263 
264  res = av_new_packet(pkt, size[0] + size[1] + size[2]);
265  if (res < 0)
266  return res;
267  pkt->stream_index = 0;
268  pkt->flags |= AV_PKT_FLAG_KEY;
269 
270  pkt->size = 0;
271  for (i = 0; i < 3; i++) {
272  if (f[i]) {
273  ret[i] = avio_read(f[i], pkt->data + pkt->size, size[i]);
274  if (!s->is_pipe)
275  ff_format_io_close(s1, &f[i]);
276  if (ret[i] > 0)
277  pkt->size += ret[i];
278  }
279  }
280 
281  if (ret[0] <= 0 || ret[1] < 0 || ret[2] < 0) {
282  av_packet_unref(pkt);
283  return AVERROR(EIO); /* signal EOF */
284  } else {
285  s->img_count++;
286  s->img_number++;
287  return 0;
288  }
289 }
290 
291 #define OFFSET(x) offsetof(VideoDemuxData, x)
292 #define DEC AV_OPT_FLAG_DECODING_PARAM
293 static const AVOption options[] = {
294  { "pixel_format", "", OFFSET(pixel_format), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, DEC },
295  { "video_size", "", OFFSET(video_size), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, DEC },
296  { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, { .str = "25" }, 0, 0, DEC },
297  { "loop", "", OFFSET(loop), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, DEC },
298  { "start_number", "first number in the sequence", OFFSET(start_number), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, INT_MAX, DEC },
299  { NULL },
300 };
301 
302 #if CONFIG_IMAGE2_DEMUXER
303 static const AVClass img2_class = {
304  .class_name = "image2 demuxer",
305  .item_name = av_default_item_name,
306  .option = options,
307  .version = LIBAVUTIL_VERSION_INT,
308 };
309 AVInputFormat ff_image2_demuxer = {
310  .name = "image2",
311  .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
312  .priv_data_size = sizeof(VideoDemuxData),
316  .flags = AVFMT_NOFILE,
317  .priv_class = &img2_class,
318 };
319 #endif
320 #if CONFIG_IMAGE2PIPE_DEMUXER
321 static const AVClass img2pipe_class = {
322  .class_name = "image2pipe demuxer",
323  .item_name = av_default_item_name,
324  .option = options,
325  .version = LIBAVUTIL_VERSION_INT,
326 };
327 AVInputFormat ff_image2pipe_demuxer = {
328  .name = "image2pipe",
329  .long_name = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
330  .priv_data_size = sizeof(VideoDemuxData),
333  .priv_class = &img2pipe_class,
334 };
335 #endif
int(* io_open)(struct AVFormatContext *s, AVIOContext **pb, const char *url, int flags, AVDictionary **options)
A callback for opening new IO streams.
Definition: avformat.h:1270
int img_number
Definition: img2dec.c:35
Bytestream IO Context.
Definition: avio.h:104
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:297
int size
int av_parse_video_rate(AVRational *rate, const char *arg)
Parse str and store the detected values in *rate.
Definition: parseutils.c:127
AVOption.
Definition: opt.h:234
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:100
#define OFFSET(x)
Definition: img2dec.c:291
const char * filename
Definition: avformat.h:399
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:2986
char path[1024]
Definition: img2dec.c:38
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3483
int num
numerator
Definition: rational.h:44
int size
Definition: avcodec.h:1347
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
#define AVIO_FLAG_READ
read-only
Definition: avio.h:368
#define FF_ARRAY_ELEMS(a)
int ctx_flags
Flags signalling stream properties.
Definition: avformat.h:989
This struct describes the properties of an encoded stream.
Definition: avcodec.h:3475
int img_count
Definition: img2dec.c:36
char * pixel_format
Set by a private option.
Definition: img2dec.c:39
Format I/O context.
Definition: avformat.h:940
int avio_check(const char *url, int flags)
Return AVIO_FLAG_* access flags corresponding to the access permissions of the resource in url...
Definition: avio.c:301
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:39
int width
Video only.
Definition: avcodec.h:3525
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition: avformat.h:919
AVOptions.
int flags
Can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER, AVFMT_SHOW_IDS, AVFMT_GENERIC_INDEX, AVFMT_TS_DISCONT, AVFMT_NOBINSEARCH, AVFMT_NOGENSEARCH, AVFMT_NO_BYTE_SEEK.
Definition: avformat.h:543
enum AVStreamParseType need_parsing
Definition: avformat.h:879
void ff_format_io_close(AVFormatContext *s, AVIOContext **pb)
A wrapper around AVFormatContext.io_close that should be used instead of calling the pointer directly...
Definition: utils.c:3317
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2648
static int img_read_header(AVFormatContext *s1)
Definition: img2dec.c:138
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1008
uint8_t * data
Definition: avcodec.h:1346
static int img_read_probe(AVProbeData *p)
Definition: img2dec.c:127
static int flags
Definition: log.c:50
#define AVERROR_EOF
End of file.
Definition: error.h:51
static const AVOption options[]
Definition: img2dec.c:293
enum AVCodecID video_codec_id
Forced video codec_id.
Definition: avformat.h:1094
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:545
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1378
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:84
char * framerate
Set by a private option.
Definition: img2dec.c:41
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
static const int sizes[][2]
Definition: img2dec.c:46
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:148
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3479
#define FFMAX(a, b)
Definition: common.h:64
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition: avstring.c:81
#define fail()
Definition: checkasm.h:80
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1352
char filename[1024]
input or output filename
Definition: avformat.h:1016
enum AVCodecID audio_codec_id
Forced audio codec_id.
Definition: avformat.h:1100
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
char * video_size
Set by a private option.
Definition: img2dec.c:40
enum AVPixelFormat pix_fmt
Definition: movenc.c:853
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
int start_number
Definition: img2dec.c:43
int av_get_frame_filename(char *buf, int buf_size, const char *path, int number)
Return in &#39;buf&#39; the path with &#39;d&#39; replaced by a number.
Definition: utils.c:2817
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:546
Stream structure.
Definition: avformat.h:705
#define DEC
Definition: img2dec.c:292
NULL
Definition: eval.c:55
static int width
Definition: utils.c:156
AVIOContext * pb
I/O context.
Definition: avformat.h:982
av_default_item_name
Definition: dnxhdenc.c:55
static int find_image_range(int *pfirst_index, int *plast_index, const char *path, int max_start)
Definition: img2dec.c:74
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:347
int av_filename_number_test(const char *filename)
Check whether filename actually is a numbered sequence generator.
Definition: utils.c:134
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
Describe the class of an AVClass context structure.
Definition: log.h:34
rational number numerator/denominator
Definition: rational.h:43
static int infer_size(int *width_ptr, int *height_ptr, int size)
Definition: img2dec.c:58
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:405
This structure contains the data a format has to probe a file.
Definition: avformat.h:398
misc parsing utilities
int img_last
Definition: img2dec.c:34
int height
Definition: gxfenc.c:72
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:757
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:407
full parsing and repack
Definition: avformat.h:657
Main libavformat public API header.
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:412
int64_t start_time
Decoding: pts of the first frame of the stream, in stream time base.
Definition: avformat.h:750
static int img_read_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: img2dec.c:220
int den
denominator
Definition: rational.h:45
struct AVInputFormat * iformat
The input container format.
Definition: avformat.h:952
enum AVCodecID ff_guess_image2_codec(const char *filename)
Definition: img2.c:101
void * priv_data
Format private data.
Definition: avformat.h:968
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:529
AVCodecParameters * codecpar
Definition: avformat.h:831
enum AVPixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
Definition: pixdesc.c:1716
int stream_index
Definition: avcodec.h:1348
int img_first
Definition: img2dec.c:33
AVPixelFormat
Pixel format.
Definition: pixfmt.h:57
This structure stores compressed data.
Definition: avcodec.h:1323