2001-07-22 14:18:56 +00:00
|
|
|
/*
|
|
|
|
* Image format
|
2002-05-25 22:34:32 +00:00
|
|
|
* Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
|
2001-07-22 14:18:56 +00:00
|
|
|
*
|
2002-05-25 22:34:32 +00: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 14:18:56 +00:00
|
|
|
*
|
2002-05-25 22:34:32 +00:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
2001-07-22 14:18:56 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2002-05-25 22:34:32 +00:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2001-07-22 14:18:56 +00:00
|
|
|
*
|
2002-05-25 22:34:32 +00: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 14:18:56 +00:00
|
|
|
*/
|
2002-12-11 03:18:47 +00:00
|
|
|
#include <unistd.h>
|
2001-07-22 14:18:56 +00:00
|
|
|
#include "avformat.h"
|
|
|
|
|
2002-12-20 19:25:10 +00:00
|
|
|
#ifdef __MINGW32__
|
|
|
|
# include <windows.h>
|
2002-12-20 21:04:08 +00:00
|
|
|
# define usleep(t) Sleep((t) / 1000)
|
|
|
|
#endif
|
|
|
|
#ifdef __BEOS__
|
|
|
|
# ifndef usleep
|
|
|
|
# include <OS.h>
|
|
|
|
# define usleep(t) snooze((bigtime_t)(t))
|
|
|
|
# endif
|
2002-12-20 19:25:10 +00:00
|
|
|
#endif
|
|
|
|
|
2001-07-22 14:18:56 +00:00
|
|
|
typedef struct {
|
|
|
|
int width;
|
|
|
|
int height;
|
|
|
|
int img_number;
|
|
|
|
int img_size;
|
2003-01-11 05:02:14 +00:00
|
|
|
AVImageFormat *img_fmt;
|
|
|
|
int pix_fmt;
|
2001-07-22 14:18:56 +00:00
|
|
|
int is_pipe;
|
|
|
|
char path[1024];
|
2003-01-11 05:02:14 +00:00
|
|
|
/* temporary usage */
|
|
|
|
void *ptr;
|
2001-07-22 14:18:56 +00:00
|
|
|
} VideoData;
|
|
|
|
|
2002-12-11 03:18:47 +00:00
|
|
|
int emulate_frame_rate;
|
|
|
|
|
2003-01-11 05:02:14 +00:00
|
|
|
static int image_probe(AVProbeData *p)
|
2001-07-22 14:18:56 +00:00
|
|
|
{
|
2003-01-11 05:33:35 +00:00
|
|
|
if (filename_number_test(p->filename) >= 0 && guess_image_format(p->filename))
|
2003-01-11 05:02:14 +00:00
|
|
|
return AVPROBE_SCORE_MAX;
|
|
|
|
else
|
2001-07-22 14:18:56 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-01-11 05:02:14 +00:00
|
|
|
static int read_header_alloc_cb(void *opaque, AVImageInfo *info)
|
2001-07-22 14:18:56 +00:00
|
|
|
{
|
2003-01-11 05:02:14 +00:00
|
|
|
VideoData *s = opaque;
|
2001-07-22 14:18:56 +00:00
|
|
|
|
2003-01-11 05:02:14 +00: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 14:18:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int img_read_header(AVFormatContext *s1, AVFormatParameters *ap)
|
|
|
|
{
|
2002-05-20 16:31:13 +00:00
|
|
|
VideoData *s = s1->priv_data;
|
2003-01-11 05:02:14 +00:00
|
|
|
int i, ret;
|
2001-07-22 14:18:56 +00:00
|
|
|
char buf[1024];
|
|
|
|
ByteIOContext pb1, *f = &pb1;
|
|
|
|
AVStream *st;
|
|
|
|
|
2002-05-20 16:31:13 +00:00
|
|
|
st = av_new_stream(s1, 0);
|
2001-07-22 14:18:56 +00:00
|
|
|
if (!st) {
|
2002-05-18 23:11:09 +00:00
|
|
|
av_free(s);
|
2001-07-22 14:18:56 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2002-05-20 16:31:13 +00:00
|
|
|
|
2003-01-11 05:02:14 +00:00
|
|
|
if (ap && ap->image_format)
|
|
|
|
s->img_fmt = ap->image_format;
|
|
|
|
|
2001-07-22 14:18:56 +00:00
|
|
|
strcpy(s->path, s1->filename);
|
|
|
|
s->img_number = 0;
|
|
|
|
|
|
|
|
/* find format */
|
2002-05-20 16:31:13 +00:00
|
|
|
if (s1->iformat->flags & AVFMT_NOFILE)
|
2001-07-22 14:18:56 +00:00
|
|
|
s->is_pipe = 0;
|
|
|
|
else
|
|
|
|
s->is_pipe = 1;
|
|
|
|
|
|
|
|
if (!s->is_pipe) {
|
|
|
|
/* try to find the first image */
|
|
|
|
for(i=0;i<5;i++) {
|
2001-09-16 21:50:48 +00:00
|
|
|
if (get_frame_filename(buf, sizeof(buf), s->path, s->img_number) < 0)
|
|
|
|
goto fail;
|
2001-07-22 14:18:56 +00:00
|
|
|
if (url_fopen(f, buf, URL_RDONLY) >= 0)
|
|
|
|
break;
|
|
|
|
s->img_number++;
|
|
|
|
}
|
|
|
|
if (i == 5)
|
|
|
|
goto fail;
|
|
|
|
} else {
|
|
|
|
f = &s1->pb;
|
|
|
|
}
|
|
|
|
|
2003-01-11 05:02:14 +00:00
|
|
|
ret = av_read_image(f, s1->filename, s->img_fmt, read_header_alloc_cb, s);
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail1;
|
2001-07-22 14:18:56 +00: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 05:02:14 +00:00
|
|
|
st->codec.pix_fmt = s->pix_fmt;
|
|
|
|
s->img_size = avpicture_get_size(s->pix_fmt, s->width, s->height);
|
|
|
|
|
2001-07-22 14:18:56 +00:00
|
|
|
if (!ap || !ap->frame_rate)
|
|
|
|
st->codec.frame_rate = 25 * FRAME_RATE_BASE;
|
|
|
|
else
|
|
|
|
st->codec.frame_rate = ap->frame_rate;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
fail1:
|
|
|
|
if (!s->is_pipe)
|
|
|
|
url_fclose(f);
|
|
|
|
fail:
|
2002-05-18 23:11:09 +00:00
|
|
|
av_free(s);
|
2001-07-22 14:18:56 +00:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2003-01-11 05:02:14 +00:00
|
|
|
static int read_packet_alloc_cb(void *opaque, AVImageInfo *info)
|
2001-07-22 14:18:56 +00:00
|
|
|
{
|
2003-01-11 05:02:14 +00: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 14:18:56 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-01-11 05:02:14 +00:00
|
|
|
static int img_read_packet(AVFormatContext *s1, AVPacket *pkt)
|
2001-07-22 14:18:56 +00:00
|
|
|
{
|
2003-01-11 05:02:14 +00:00
|
|
|
VideoData *s = s1->priv_data;
|
|
|
|
char filename[1024];
|
|
|
|
int ret;
|
|
|
|
ByteIOContext f1, *f;
|
|
|
|
static INT64 first_frame;
|
2001-07-22 14:18:56 +00:00
|
|
|
|
2003-01-11 05:02:14 +00:00
|
|
|
if (emulate_frame_rate) {
|
|
|
|
if (!first_frame) {
|
|
|
|
first_frame = av_gettime();
|
|
|
|
} else {
|
|
|
|
INT64 pts;
|
|
|
|
INT64 nowus;
|
|
|
|
|
|
|
|
nowus = av_gettime() - first_frame;
|
|
|
|
|
|
|
|
pts = ((INT64)s->img_number * FRAME_RATE_BASE * 1000000) / (s1->streams[0]->codec.frame_rate);
|
|
|
|
|
|
|
|
if (pts > nowus)
|
|
|
|
usleep(pts - nowus);
|
2001-07-22 14:18:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-11 05:02:14 +00:00
|
|
|
if (!s->is_pipe) {
|
|
|
|
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 18:59:34 +00:00
|
|
|
|
2003-01-11 05:02:14 +00: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 18:59:34 +00:00
|
|
|
}
|
|
|
|
|
2003-01-11 05:02:14 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
av_free_packet(pkt);
|
|
|
|
return -EIO; /* signal EOF */
|
|
|
|
} else {
|
|
|
|
pkt->pts = ((INT64)s->img_number * s1->pts_den * FRAME_RATE_BASE) / (s1->streams[0]->codec.frame_rate * s1->pts_num);
|
|
|
|
s->img_number++;
|
|
|
|
return 0;
|
|
|
|
}
|
2001-08-11 18:59:34 +00:00
|
|
|
}
|
|
|
|
|
2003-01-11 05:02:14 +00:00
|
|
|
static int img_read_close(AVFormatContext *s1)
|
2001-07-22 14:18:56 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-01-11 05:02:14 +00:00
|
|
|
/******************************************************/
|
|
|
|
/* image output */
|
|
|
|
|
|
|
|
static int img_set_parameters(AVFormatContext *s, AVFormatParameters *ap)
|
2002-09-06 13:01:19 +00:00
|
|
|
{
|
2003-01-11 05:02:14 +00: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 13:01:19 +00:00
|
|
|
} else {
|
2003-01-11 05:02:14 +00:00
|
|
|
img_fmt = guess_image_format(s->filename);
|
2002-09-06 13:01:19 +00:00
|
|
|
}
|
2003-01-11 05:02:14 +00:00
|
|
|
if (!img_fmt)
|
|
|
|
return -1;
|
2002-09-06 13:01:19 +00:00
|
|
|
|
2003-01-11 05:02:14 +00: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 13:01:19 +00:00
|
|
|
}
|
2003-01-11 05:02:14 +00: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 13:01:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-07-22 14:18:56 +00:00
|
|
|
static int img_write_header(AVFormatContext *s)
|
|
|
|
{
|
2002-05-20 16:31:13 +00:00
|
|
|
VideoData *img = s->priv_data;
|
2001-07-22 14:18:56 +00:00
|
|
|
|
|
|
|
img->img_number = 1;
|
|
|
|
strcpy(img->path, s->filename);
|
|
|
|
|
|
|
|
/* find format */
|
2002-05-20 16:31:13 +00:00
|
|
|
if (s->oformat->flags & AVFMT_NOFILE)
|
2001-07-22 14:18:56 +00:00
|
|
|
img->is_pipe = 0;
|
|
|
|
else
|
|
|
|
img->is_pipe = 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int img_write_packet(AVFormatContext *s, int stream_index,
|
2002-04-07 21:44:29 +00:00
|
|
|
UINT8 *buf, int size, int force_pts)
|
2001-07-22 14:18:56 +00:00
|
|
|
{
|
|
|
|
VideoData *img = s->priv_data;
|
|
|
|
AVStream *st = s->streams[stream_index];
|
|
|
|
ByteIOContext pb1, *pb;
|
2003-01-11 05:02:14 +00:00
|
|
|
AVPicture *picture;
|
|
|
|
int width, height, ret;
|
2001-07-22 14:18:56 +00:00
|
|
|
char filename[1024];
|
2003-01-11 05:02:14 +00:00
|
|
|
AVImageInfo info;
|
2001-07-22 14:18:56 +00:00
|
|
|
|
|
|
|
width = st->codec.width;
|
|
|
|
height = st->codec.height;
|
2002-09-06 13:01:19 +00:00
|
|
|
|
2003-01-11 05:02:14 +00:00
|
|
|
picture = (AVPicture *)buf;
|
2001-07-22 14:18:56 +00:00
|
|
|
|
|
|
|
if (!img->is_pipe) {
|
2003-01-11 05:02:14 +00:00
|
|
|
if (get_frame_filename(filename, sizeof(filename),
|
|
|
|
img->path, img->img_number) < 0)
|
|
|
|
return -EIO;
|
2001-07-22 14:18:56 +00:00
|
|
|
pb = &pb1;
|
|
|
|
if (url_fopen(pb, filename, URL_WRONLY) < 0)
|
|
|
|
return -EIO;
|
|
|
|
} else {
|
|
|
|
pb = &s->pb;
|
|
|
|
}
|
2003-01-11 05:02:14 +00:00
|
|
|
info.width = width;
|
|
|
|
info.height = height;
|
|
|
|
info.pix_fmt = st->codec.pix_fmt;
|
|
|
|
info.pict = *picture;
|
|
|
|
ret = av_write_image(pb, img->img_fmt, &info);
|
2001-07-22 14:18:56 +00: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 05:02:14 +00:00
|
|
|
/* input */
|
2001-07-22 14:18:56 +00:00
|
|
|
|
2003-01-11 05:02:14 +00:00
|
|
|
static AVInputFormat image_iformat = {
|
|
|
|
"image",
|
|
|
|
"image sequence",
|
2002-05-20 16:31:13 +00:00
|
|
|
sizeof(VideoData),
|
2003-01-11 05:02:14 +00:00
|
|
|
image_probe,
|
2001-07-22 14:18:56 +00:00
|
|
|
img_read_header,
|
|
|
|
img_read_packet,
|
|
|
|
img_read_close,
|
|
|
|
NULL,
|
2001-09-16 21:50:48 +00:00
|
|
|
AVFMT_NOFILE | AVFMT_NEEDNUMBER,
|
2001-07-22 14:18:56 +00:00
|
|
|
};
|
|
|
|
|
2003-01-11 05:02:14 +00:00
|
|
|
static AVInputFormat imagepipe_iformat = {
|
|
|
|
"imagepipe",
|
|
|
|
"piped image sequence",
|
2002-05-20 16:31:13 +00:00
|
|
|
sizeof(VideoData),
|
|
|
|
NULL, /* no probe */
|
2001-07-22 14:18:56 +00:00
|
|
|
img_read_header,
|
|
|
|
img_read_packet,
|
|
|
|
img_read_close,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2003-01-11 05:02:14 +00:00
|
|
|
/* output */
|
2001-08-11 18:59:34 +00:00
|
|
|
|
2003-01-11 05:02:14 +00:00
|
|
|
static AVOutputFormat image_oformat = {
|
|
|
|
"image",
|
|
|
|
"image sequence",
|
2001-08-11 18:59:34 +00:00
|
|
|
"",
|
|
|
|
"",
|
2002-05-20 16:31:13 +00:00
|
|
|
sizeof(VideoData),
|
2001-08-11 18:59:34 +00:00
|
|
|
CODEC_ID_NONE,
|
|
|
|
CODEC_ID_RAWVIDEO,
|
|
|
|
img_write_header,
|
|
|
|
img_write_packet,
|
|
|
|
img_write_trailer,
|
2003-01-11 05:02:14 +00:00
|
|
|
AVFMT_NOFILE | AVFMT_NEEDNUMBER | AVFMT_RAWPICTURE,
|
|
|
|
img_set_parameters,
|
2001-08-11 18:59:34 +00:00
|
|
|
};
|
2002-05-20 16:31:13 +00:00
|
|
|
|
2003-01-11 05:02:14 +00:00
|
|
|
static AVOutputFormat imagepipe_oformat = {
|
|
|
|
"imagepipe",
|
|
|
|
"piped image sequence",
|
|
|
|
"",
|
2002-09-06 13:01:19 +00:00
|
|
|
"",
|
|
|
|
sizeof(VideoData),
|
|
|
|
CODEC_ID_NONE,
|
|
|
|
CODEC_ID_RAWVIDEO,
|
|
|
|
img_write_header,
|
|
|
|
img_write_packet,
|
|
|
|
img_write_trailer,
|
2003-01-11 05:33:35 +00:00
|
|
|
AVFMT_RAWPICTURE,
|
2003-01-11 05:02:14 +00:00
|
|
|
img_set_parameters,
|
2002-09-06 13:01:19 +00:00
|
|
|
};
|
|
|
|
|
2002-05-20 16:31:13 +00:00
|
|
|
int img_init(void)
|
|
|
|
{
|
2003-01-11 05:02:14 +00:00
|
|
|
av_register_input_format(&image_iformat);
|
|
|
|
av_register_output_format(&image_oformat);
|
2002-05-20 16:31:13 +00:00
|
|
|
|
2003-01-11 05:02:14 +00:00
|
|
|
av_register_input_format(&imagepipe_iformat);
|
|
|
|
av_register_output_format(&imagepipe_oformat);
|
2002-09-06 13:01:19 +00:00
|
|
|
|
2002-05-20 16:31:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|