2001-07-22 16:18:56 +02:00
|
|
|
/*
|
|
|
|
* Image format
|
2002-05-26 00:34:32 +02:00
|
|
|
* Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
|
2001-07-22 16:18:56 +02:00
|
|
|
*
|
2002-05-26 00:34:32 +02:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
2001-07-22 16:18:56 +02:00
|
|
|
*
|
2002-05-26 00:34:32 +02:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
2001-07-22 16:18:56 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2002-05-26 00:34:32 +02:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2001-07-22 16:18:56 +02:00
|
|
|
*
|
2002-05-26 00:34:32 +02:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
2001-07-22 16:18:56 +02:00
|
|
|
*/
|
|
|
|
#include "avformat.h"
|
2002-12-20 20:25:10 +01:00
|
|
|
|
2003-08-24 23:20:44 +02:00
|
|
|
/* XXX: this is a hack */
|
|
|
|
int loop_input = 0;
|
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
typedef struct {
|
|
|
|
int width;
|
|
|
|
int height;
|
2003-08-24 23:20:44 +02:00
|
|
|
int img_first;
|
|
|
|
int img_last;
|
2001-07-22 16:18:56 +02:00
|
|
|
int img_number;
|
2003-08-24 23:20:44 +02:00
|
|
|
int img_count;
|
2001-07-22 16:18:56 +02:00
|
|
|
int img_size;
|
2003-01-11 06:02:14 +01:00
|
|
|
AVImageFormat *img_fmt;
|
|
|
|
int pix_fmt;
|
2001-07-22 16:18:56 +02:00
|
|
|
int is_pipe;
|
|
|
|
char path[1024];
|
2003-01-11 06:02:14 +01:00
|
|
|
/* temporary usage */
|
|
|
|
void *ptr;
|
2001-07-22 16:18:56 +02:00
|
|
|
} VideoData;
|
|
|
|
|
2003-08-08 19:53:30 +02:00
|
|
|
|
|
|
|
/* return -1 if no image found */
|
|
|
|
static int find_image_range(int *pfirst_index, int *plast_index,
|
|
|
|
const char *path)
|
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
int range, last_index, range1, first_index;
|
|
|
|
|
|
|
|
/* find the first image */
|
|
|
|
for(first_index = 0; first_index < 5; first_index++) {
|
|
|
|
if (get_frame_filename(buf, sizeof(buf), path, first_index) < 0)
|
|
|
|
goto fail;
|
|
|
|
if (url_exist(buf))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (first_index == 5)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* find the last image */
|
|
|
|
last_index = first_index;
|
|
|
|
for(;;) {
|
|
|
|
range = 0;
|
|
|
|
for(;;) {
|
|
|
|
if (!range)
|
|
|
|
range1 = 1;
|
|
|
|
else
|
|
|
|
range1 = 2 * range;
|
|
|
|
if (get_frame_filename(buf, sizeof(buf), path,
|
|
|
|
last_index + range1) < 0)
|
|
|
|
goto fail;
|
|
|
|
if (!url_exist(buf))
|
|
|
|
break;
|
|
|
|
range = range1;
|
|
|
|
/* just in case... */
|
|
|
|
if (range >= (1 << 30))
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
/* we are sure than image last_index + range exists */
|
|
|
|
if (!range)
|
|
|
|
break;
|
|
|
|
last_index += range;
|
|
|
|
}
|
|
|
|
*pfirst_index = first_index;
|
|
|
|
*plast_index = last_index;
|
|
|
|
return 0;
|
|
|
|
fail:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-01-11 06:02:14 +01:00
|
|
|
static int image_probe(AVProbeData *p)
|
2001-07-22 16:18:56 +02:00
|
|
|
{
|
2003-01-11 06:33:35 +01:00
|
|
|
if (filename_number_test(p->filename) >= 0 && guess_image_format(p->filename))
|
2003-01-11 06:02:14 +01:00
|
|
|
return AVPROBE_SCORE_MAX;
|
|
|
|
else
|
2001-07-22 16:18:56 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-01-11 06:02:14 +01:00
|
|
|
static int read_header_alloc_cb(void *opaque, AVImageInfo *info)
|
2001-07-22 16:18:56 +02:00
|
|
|
{
|
2003-01-11 06:02:14 +01:00
|
|
|
VideoData *s = opaque;
|
2001-07-22 16:18:56 +02:00
|
|
|
|
2003-01-11 06:02:14 +01:00
|
|
|
s->width = info->width;
|
|
|
|
s->height = info->height;
|
|
|
|
s->pix_fmt = info->pix_fmt;
|
|
|
|
/* stop image reading but no error */
|
|
|
|
return 1;
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int img_read_header(AVFormatContext *s1, AVFormatParameters *ap)
|
|
|
|
{
|
2002-05-20 18:31:13 +02:00
|
|
|
VideoData *s = s1->priv_data;
|
2003-08-08 19:53:30 +02:00
|
|
|
int ret, first_index, last_index;
|
2001-07-22 16:18:56 +02:00
|
|
|
char buf[1024];
|
|
|
|
ByteIOContext pb1, *f = &pb1;
|
|
|
|
AVStream *st;
|
|
|
|
|
2002-05-20 18:31:13 +02:00
|
|
|
st = av_new_stream(s1, 0);
|
2001-07-22 16:18:56 +02:00
|
|
|
if (!st) {
|
2002-05-19 01:11:09 +02:00
|
|
|
av_free(s);
|
2001-07-22 16:18:56 +02:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2002-05-20 18:31:13 +02:00
|
|
|
|
2003-01-11 06:02:14 +01:00
|
|
|
if (ap && ap->image_format)
|
|
|
|
s->img_fmt = ap->image_format;
|
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
strcpy(s->path, s1->filename);
|
|
|
|
s->img_number = 0;
|
2003-08-24 23:20:44 +02:00
|
|
|
s->img_count = 0;
|
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
/* find format */
|
2002-05-20 18:31:13 +02:00
|
|
|
if (s1->iformat->flags & AVFMT_NOFILE)
|
2001-07-22 16:18:56 +02:00
|
|
|
s->is_pipe = 0;
|
|
|
|
else
|
|
|
|
s->is_pipe = 1;
|
|
|
|
|
2003-08-08 19:53:30 +02:00
|
|
|
if (!ap || !ap->frame_rate) {
|
|
|
|
st->codec.frame_rate = 25;
|
|
|
|
st->codec.frame_rate_base = 1;
|
|
|
|
} else {
|
|
|
|
st->codec.frame_rate = ap->frame_rate;
|
|
|
|
st->codec.frame_rate_base = ap->frame_rate_base;
|
|
|
|
}
|
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
if (!s->is_pipe) {
|
2003-08-08 19:53:30 +02:00
|
|
|
if (find_image_range(&first_index, &last_index, s->path) < 0)
|
|
|
|
goto fail;
|
2003-08-24 23:20:44 +02:00
|
|
|
s->img_first = first_index;
|
|
|
|
s->img_last = last_index;
|
2003-08-08 19:53:30 +02:00
|
|
|
s->img_number = first_index;
|
|
|
|
/* compute duration */
|
|
|
|
st->start_time = 0;
|
|
|
|
st->duration = ((int64_t)AV_TIME_BASE *
|
|
|
|
(last_index - first_index + 1) *
|
|
|
|
st->codec.frame_rate_base) / st->codec.frame_rate;
|
|
|
|
if (get_frame_filename(buf, sizeof(buf), s->path, s->img_number) < 0)
|
|
|
|
goto fail;
|
|
|
|
if (url_fopen(f, buf, URL_RDONLY) < 0)
|
2001-07-22 16:18:56 +02:00
|
|
|
goto fail;
|
|
|
|
} else {
|
|
|
|
f = &s1->pb;
|
|
|
|
}
|
|
|
|
|
2003-01-11 06:02:14 +01:00
|
|
|
ret = av_read_image(f, s1->filename, s->img_fmt, read_header_alloc_cb, s);
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail1;
|
2001-07-22 16:18:56 +02:00
|
|
|
|
|
|
|
if (!s->is_pipe) {
|
|
|
|
url_fclose(f);
|
|
|
|
} else {
|
|
|
|
url_fseek(f, 0, SEEK_SET);
|
|
|
|
}
|
|
|
|
|
|
|
|
st->codec.codec_type = CODEC_TYPE_VIDEO;
|
|
|
|
st->codec.codec_id = CODEC_ID_RAWVIDEO;
|
|
|
|
st->codec.width = s->width;
|
|
|
|
st->codec.height = s->height;
|
2003-01-11 06:02:14 +01:00
|
|
|
st->codec.pix_fmt = s->pix_fmt;
|
|
|
|
s->img_size = avpicture_get_size(s->pix_fmt, s->width, s->height);
|
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
return 0;
|
|
|
|
fail1:
|
|
|
|
if (!s->is_pipe)
|
|
|
|
url_fclose(f);
|
|
|
|
fail:
|
2002-05-19 01:11:09 +02:00
|
|
|
av_free(s);
|
2001-07-22 16:18:56 +02:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2003-01-11 06:02:14 +01:00
|
|
|
static int read_packet_alloc_cb(void *opaque, AVImageInfo *info)
|
2001-07-22 16:18:56 +02:00
|
|
|
{
|
2003-01-11 06:02:14 +01:00
|
|
|
VideoData *s = opaque;
|
|
|
|
|
|
|
|
if (info->width != s->width ||
|
|
|
|
info->height != s->height)
|
|
|
|
return -1;
|
|
|
|
avpicture_fill(&info->pict, s->ptr, info->pix_fmt, info->width, info->height);
|
2001-07-22 16:18:56 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-01-11 06:02:14 +01:00
|
|
|
static int img_read_packet(AVFormatContext *s1, AVPacket *pkt)
|
2001-07-22 16:18:56 +02:00
|
|
|
{
|
2003-01-11 06:02:14 +01:00
|
|
|
VideoData *s = s1->priv_data;
|
|
|
|
char filename[1024];
|
|
|
|
int ret;
|
|
|
|
ByteIOContext f1, *f;
|
2001-07-22 16:18:56 +02:00
|
|
|
|
2003-01-11 06:02:14 +01:00
|
|
|
if (!s->is_pipe) {
|
2003-08-24 23:20:44 +02:00
|
|
|
/* loop over input */
|
|
|
|
if (loop_input && s->img_number > s->img_last) {
|
|
|
|
s->img_number = s->img_first;
|
|
|
|
}
|
2003-01-11 06:02:14 +01:00
|
|
|
if (get_frame_filename(filename, sizeof(filename),
|
|
|
|
s->path, s->img_number) < 0)
|
|
|
|
return -EIO;
|
|
|
|
f = &f1;
|
|
|
|
if (url_fopen(f, filename, URL_RDONLY) < 0)
|
|
|
|
return -EIO;
|
|
|
|
} else {
|
|
|
|
f = &s1->pb;
|
|
|
|
if (url_feof(f))
|
|
|
|
return -EIO;
|
|
|
|
}
|
2001-08-11 20:59:34 +02:00
|
|
|
|
2003-01-11 06:02:14 +01:00
|
|
|
av_new_packet(pkt, s->img_size);
|
|
|
|
pkt->stream_index = 0;
|
|
|
|
|
|
|
|
s->ptr = pkt->data;
|
|
|
|
ret = av_read_image(f, filename, s->img_fmt, read_packet_alloc_cb, s);
|
|
|
|
if (!s->is_pipe) {
|
|
|
|
url_fclose(f);
|
2001-08-11 20:59:34 +02:00
|
|
|
}
|
|
|
|
|
2003-01-11 06:02:14 +01:00
|
|
|
if (ret < 0) {
|
|
|
|
av_free_packet(pkt);
|
|
|
|
return -EIO; /* signal EOF */
|
|
|
|
} else {
|
2003-08-24 23:20:44 +02:00
|
|
|
/* XXX: computing this pts is not necessary as it is done in
|
|
|
|
the generic code too */
|
|
|
|
pkt->pts = av_rescale((int64_t)s->img_count * s1->streams[0]->codec.frame_rate_base, s1->pts_den, s1->streams[0]->codec.frame_rate) / s1->pts_num;
|
|
|
|
s->img_count++;
|
2003-01-11 06:02:14 +01:00
|
|
|
s->img_number++;
|
|
|
|
return 0;
|
|
|
|
}
|
2001-08-11 20:59:34 +02:00
|
|
|
}
|
|
|
|
|
2003-01-11 06:02:14 +01:00
|
|
|
static int img_read_close(AVFormatContext *s1)
|
2001-07-22 16:18:56 +02:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-01-11 06:02:14 +01:00
|
|
|
/******************************************************/
|
|
|
|
/* image output */
|
|
|
|
|
|
|
|
static int img_set_parameters(AVFormatContext *s, AVFormatParameters *ap)
|
2002-09-06 15:01:19 +02:00
|
|
|
{
|
2003-01-11 06:02:14 +01:00
|
|
|
VideoData *img = s->priv_data;
|
|
|
|
AVStream *st;
|
|
|
|
AVImageFormat *img_fmt;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* find output image format */
|
|
|
|
if (ap && ap->image_format) {
|
|
|
|
img_fmt = ap->image_format;
|
2002-09-06 15:01:19 +02:00
|
|
|
} else {
|
2003-01-11 06:02:14 +01:00
|
|
|
img_fmt = guess_image_format(s->filename);
|
2002-09-06 15:01:19 +02:00
|
|
|
}
|
2003-01-11 06:02:14 +01:00
|
|
|
if (!img_fmt)
|
|
|
|
return -1;
|
2002-09-06 15:01:19 +02:00
|
|
|
|
2003-01-11 06:02:14 +01:00
|
|
|
if (s->nb_streams != 1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
st = s->streams[0];
|
|
|
|
/* we select the first matching format */
|
|
|
|
for(i=0;i<PIX_FMT_NB;i++) {
|
|
|
|
if (img_fmt->supported_pixel_formats & (1 << i))
|
|
|
|
break;
|
2002-09-06 15:01:19 +02:00
|
|
|
}
|
2003-01-11 06:02:14 +01:00
|
|
|
if (i >= PIX_FMT_NB)
|
|
|
|
return -1;
|
|
|
|
img->img_fmt = img_fmt;
|
|
|
|
img->pix_fmt = i;
|
|
|
|
st->codec.pix_fmt = img->pix_fmt;
|
2002-09-06 15:01:19 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
static int img_write_header(AVFormatContext *s)
|
|
|
|
{
|
2002-05-20 18:31:13 +02:00
|
|
|
VideoData *img = s->priv_data;
|
2001-07-22 16:18:56 +02:00
|
|
|
|
|
|
|
img->img_number = 1;
|
|
|
|
strcpy(img->path, s->filename);
|
|
|
|
|
|
|
|
/* find format */
|
2002-05-20 18:31:13 +02:00
|
|
|
if (s->oformat->flags & AVFMT_NOFILE)
|
2001-07-22 16:18:56 +02:00
|
|
|
img->is_pipe = 0;
|
|
|
|
else
|
|
|
|
img->is_pipe = 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int img_write_packet(AVFormatContext *s, int stream_index,
|
2003-09-11 00:37:33 +02:00
|
|
|
const uint8_t *buf, int size, int64_t pts)
|
2001-07-22 16:18:56 +02:00
|
|
|
{
|
|
|
|
VideoData *img = s->priv_data;
|
|
|
|
AVStream *st = s->streams[stream_index];
|
|
|
|
ByteIOContext pb1, *pb;
|
2003-01-11 06:02:14 +01:00
|
|
|
AVPicture *picture;
|
|
|
|
int width, height, ret;
|
2001-07-22 16:18:56 +02:00
|
|
|
char filename[1024];
|
2003-01-11 06:02:14 +01:00
|
|
|
AVImageInfo info;
|
2001-07-22 16:18:56 +02:00
|
|
|
|
|
|
|
width = st->codec.width;
|
|
|
|
height = st->codec.height;
|
2002-09-06 15:01:19 +02:00
|
|
|
|
2003-01-11 06:02:14 +01:00
|
|
|
picture = (AVPicture *)buf;
|
2001-07-22 16:18:56 +02:00
|
|
|
|
|
|
|
if (!img->is_pipe) {
|
2003-01-11 06:02:14 +01:00
|
|
|
if (get_frame_filename(filename, sizeof(filename),
|
|
|
|
img->path, img->img_number) < 0)
|
|
|
|
return -EIO;
|
2001-07-22 16:18:56 +02:00
|
|
|
pb = &pb1;
|
|
|
|
if (url_fopen(pb, filename, URL_WRONLY) < 0)
|
|
|
|
return -EIO;
|
|
|
|
} else {
|
|
|
|
pb = &s->pb;
|
|
|
|
}
|
2003-01-11 06:02:14 +01:00
|
|
|
info.width = width;
|
|
|
|
info.height = height;
|
|
|
|
info.pix_fmt = st->codec.pix_fmt;
|
2003-10-14 16:40:36 +02:00
|
|
|
info.interleaved = 0; /* FIXME: there should be a way to set it right */
|
2003-01-11 06:02:14 +01:00
|
|
|
info.pict = *picture;
|
|
|
|
ret = av_write_image(pb, img->img_fmt, &info);
|
2001-07-22 16:18:56 +02:00
|
|
|
if (!img->is_pipe) {
|
|
|
|
url_fclose(pb);
|
|
|
|
}
|
|
|
|
|
|
|
|
img->img_number++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int img_write_trailer(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-01-11 06:02:14 +01:00
|
|
|
/* input */
|
2001-07-22 16:18:56 +02:00
|
|
|
|
2003-01-11 06:02:14 +01:00
|
|
|
static AVInputFormat image_iformat = {
|
|
|
|
"image",
|
|
|
|
"image sequence",
|
2002-05-20 18:31:13 +02:00
|
|
|
sizeof(VideoData),
|
2003-01-11 06:02:14 +01:00
|
|
|
image_probe,
|
2001-07-22 16:18:56 +02:00
|
|
|
img_read_header,
|
|
|
|
img_read_packet,
|
|
|
|
img_read_close,
|
|
|
|
NULL,
|
2001-09-16 23:50:48 +02:00
|
|
|
AVFMT_NOFILE | AVFMT_NEEDNUMBER,
|
2001-07-22 16:18:56 +02:00
|
|
|
};
|
|
|
|
|
2003-01-11 06:02:14 +01:00
|
|
|
static AVInputFormat imagepipe_iformat = {
|
|
|
|
"imagepipe",
|
|
|
|
"piped image sequence",
|
2002-05-20 18:31:13 +02:00
|
|
|
sizeof(VideoData),
|
|
|
|
NULL, /* no probe */
|
2001-07-22 16:18:56 +02:00
|
|
|
img_read_header,
|
|
|
|
img_read_packet,
|
|
|
|
img_read_close,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2003-01-11 06:02:14 +01:00
|
|
|
/* output */
|
2001-08-11 20:59:34 +02:00
|
|
|
|
2003-01-11 06:02:14 +01:00
|
|
|
static AVOutputFormat image_oformat = {
|
|
|
|
"image",
|
|
|
|
"image sequence",
|
2001-08-11 20:59:34 +02:00
|
|
|
"",
|
|
|
|
"",
|
2002-05-20 18:31:13 +02:00
|
|
|
sizeof(VideoData),
|
2001-08-11 20:59:34 +02:00
|
|
|
CODEC_ID_NONE,
|
|
|
|
CODEC_ID_RAWVIDEO,
|
|
|
|
img_write_header,
|
|
|
|
img_write_packet,
|
|
|
|
img_write_trailer,
|
2003-01-11 06:02:14 +01:00
|
|
|
AVFMT_NOFILE | AVFMT_NEEDNUMBER | AVFMT_RAWPICTURE,
|
|
|
|
img_set_parameters,
|
2001-08-11 20:59:34 +02:00
|
|
|
};
|
2002-05-20 18:31:13 +02:00
|
|
|
|
2003-01-11 06:02:14 +01:00
|
|
|
static AVOutputFormat imagepipe_oformat = {
|
|
|
|
"imagepipe",
|
|
|
|
"piped image sequence",
|
|
|
|
"",
|
2002-09-06 15:01:19 +02:00
|
|
|
"",
|
|
|
|
sizeof(VideoData),
|
|
|
|
CODEC_ID_NONE,
|
|
|
|
CODEC_ID_RAWVIDEO,
|
|
|
|
img_write_header,
|
|
|
|
img_write_packet,
|
|
|
|
img_write_trailer,
|
2003-01-11 06:33:35 +01:00
|
|
|
AVFMT_RAWPICTURE,
|
2003-01-11 06:02:14 +01:00
|
|
|
img_set_parameters,
|
2002-09-06 15:01:19 +02:00
|
|
|
};
|
|
|
|
|
2002-05-20 18:31:13 +02:00
|
|
|
int img_init(void)
|
|
|
|
{
|
2003-01-11 06:02:14 +01:00
|
|
|
av_register_input_format(&image_iformat);
|
|
|
|
av_register_output_format(&image_oformat);
|
2002-05-20 18:31:13 +02:00
|
|
|
|
2003-01-11 06:02:14 +01:00
|
|
|
av_register_input_format(&imagepipe_iformat);
|
|
|
|
av_register_output_format(&imagepipe_oformat);
|
2002-09-06 15:01:19 +02:00
|
|
|
|
2002-05-20 18:31:13 +02:00
|
|
|
return 0;
|
|
|
|
}
|