2012-01-14 16:47:07 +00:00
|
|
|
/*
|
|
|
|
* SMJPEG demuxer
|
|
|
|
* Copyright (c) 2011 Paul B Mahol
|
|
|
|
*
|
2012-06-29 01:55:56 +00:00
|
|
|
* This file is part of FFmpeg.
|
2012-01-14 16:47:07 +00:00
|
|
|
*
|
2012-06-29 01:55:56 +00:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2012-01-14 16:47:07 +00:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
2012-06-29 01:55:56 +00:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2012-01-14 16:47:07 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2012-06-29 01:55:56 +00:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2012-01-14 16:47:07 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* This is a demuxer for Loki SDL Motion JPEG files
|
|
|
|
*/
|
|
|
|
|
2014-03-10 14:35:59 +00:00
|
|
|
#include <inttypes.h>
|
|
|
|
|
2012-01-14 16:47:07 +00:00
|
|
|
#include "avformat.h"
|
|
|
|
#include "internal.h"
|
|
|
|
#include "riff.h"
|
|
|
|
#include "smjpeg.h"
|
|
|
|
|
|
|
|
typedef struct SMJPEGContext {
|
|
|
|
int audio_stream_index;
|
|
|
|
int video_stream_index;
|
|
|
|
} SMJPEGContext;
|
|
|
|
|
|
|
|
static int smjpeg_probe(AVProbeData *p)
|
|
|
|
{
|
|
|
|
if (!memcmp(p->buf, SMJPEG_MAGIC, 8))
|
|
|
|
return AVPROBE_SCORE_MAX;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-12 12:20:36 +00:00
|
|
|
static int smjpeg_read_header(AVFormatContext *s)
|
2012-01-14 16:47:07 +00:00
|
|
|
{
|
|
|
|
SMJPEGContext *sc = s->priv_data;
|
|
|
|
AVStream *ast = NULL, *vst = NULL;
|
|
|
|
AVIOContext *pb = s->pb;
|
|
|
|
uint32_t version, htype, hlength, duration;
|
|
|
|
char *comment;
|
|
|
|
|
|
|
|
avio_skip(pb, 8); // magic
|
|
|
|
version = avio_rb32(pb);
|
|
|
|
if (version)
|
2014-03-10 14:35:59 +00:00
|
|
|
avpriv_request_sample(s, "Unknown version %"PRIu32, version);
|
2012-01-14 16:47:07 +00:00
|
|
|
|
|
|
|
duration = avio_rb32(pb); // in msec
|
|
|
|
|
2014-08-07 20:12:41 +00:00
|
|
|
while (!avio_feof(pb)) {
|
2012-01-14 16:47:07 +00:00
|
|
|
htype = avio_rl32(pb);
|
|
|
|
switch (htype) {
|
|
|
|
case SMJPEG_TXT:
|
|
|
|
hlength = avio_rb32(pb);
|
|
|
|
if (!hlength || hlength > 512)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
comment = av_malloc(hlength + 1);
|
|
|
|
if (!comment)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
if (avio_read(pb, comment, hlength) != hlength) {
|
|
|
|
av_freep(&comment);
|
|
|
|
av_log(s, AV_LOG_ERROR, "error when reading comment\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
comment[hlength] = 0;
|
|
|
|
av_dict_set(&s->metadata, "comment", comment,
|
|
|
|
AV_DICT_DONT_STRDUP_VAL);
|
|
|
|
break;
|
|
|
|
case SMJPEG_SND:
|
|
|
|
if (ast) {
|
2013-02-25 22:54:28 +00:00
|
|
|
avpriv_request_sample(s, "Multiple audio streams");
|
2012-12-23 17:10:05 +00:00
|
|
|
return AVERROR_PATCHWELCOME;
|
2012-01-14 16:47:07 +00:00
|
|
|
}
|
|
|
|
hlength = avio_rb32(pb);
|
|
|
|
if (hlength < 8)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
ast = avformat_new_stream(s, 0);
|
|
|
|
if (!ast)
|
|
|
|
return AVERROR(ENOMEM);
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 18:42:52 +00:00
|
|
|
ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
|
|
|
|
ast->codecpar->sample_rate = avio_rb16(pb);
|
|
|
|
ast->codecpar->bits_per_coded_sample = avio_r8(pb);
|
|
|
|
ast->codecpar->channels = avio_r8(pb);
|
|
|
|
ast->codecpar->codec_tag = avio_rl32(pb);
|
|
|
|
ast->codecpar->codec_id = ff_codec_get_id(ff_codec_smjpeg_audio_tags,
|
|
|
|
ast->codecpar->codec_tag);
|
2012-01-14 16:47:07 +00:00
|
|
|
ast->duration = duration;
|
|
|
|
sc->audio_stream_index = ast->index;
|
|
|
|
avpriv_set_pts_info(ast, 32, 1, 1000);
|
|
|
|
avio_skip(pb, hlength - 8);
|
|
|
|
break;
|
|
|
|
case SMJPEG_VID:
|
|
|
|
if (vst) {
|
2013-02-25 22:54:28 +00:00
|
|
|
avpriv_request_sample(s, "Multiple video streams");
|
2012-01-14 16:47:07 +00:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
hlength = avio_rb32(pb);
|
|
|
|
if (hlength < 12)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
vst = avformat_new_stream(s, 0);
|
|
|
|
if (!vst)
|
|
|
|
return AVERROR(ENOMEM);
|
2016-04-10 19:58:15 +00:00
|
|
|
vst->nb_frames = avio_rb32(pb);
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 18:42:52 +00:00
|
|
|
vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
|
|
|
|
vst->codecpar->width = avio_rb16(pb);
|
|
|
|
vst->codecpar->height = avio_rb16(pb);
|
|
|
|
vst->codecpar->codec_tag = avio_rl32(pb);
|
|
|
|
vst->codecpar->codec_id = ff_codec_get_id(ff_codec_smjpeg_video_tags,
|
|
|
|
vst->codecpar->codec_tag);
|
2012-01-14 16:47:07 +00:00
|
|
|
vst->duration = duration;
|
|
|
|
sc->video_stream_index = vst->index;
|
|
|
|
avpriv_set_pts_info(vst, 32, 1, 1000);
|
|
|
|
avio_skip(pb, hlength - 12);
|
|
|
|
break;
|
|
|
|
case SMJPEG_HEND:
|
|
|
|
return 0;
|
|
|
|
default:
|
2014-03-10 14:35:59 +00:00
|
|
|
av_log(s, AV_LOG_ERROR, "unknown header %"PRIx32"\n", htype);
|
2012-01-14 16:47:07 +00:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return AVERROR_EOF;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int smjpeg_read_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
SMJPEGContext *sc = s->priv_data;
|
2012-10-11 20:53:22 +00:00
|
|
|
uint32_t dtype, size, timestamp;
|
2012-02-14 03:48:42 +00:00
|
|
|
int64_t pos;
|
2012-10-11 20:53:22 +00:00
|
|
|
int ret;
|
2012-01-14 16:47:07 +00:00
|
|
|
|
2014-08-07 20:12:41 +00:00
|
|
|
if (avio_feof(s->pb))
|
2012-01-14 16:47:07 +00:00
|
|
|
return AVERROR_EOF;
|
2012-02-14 03:48:42 +00:00
|
|
|
pos = avio_tell(s->pb);
|
2012-01-14 16:47:07 +00:00
|
|
|
dtype = avio_rl32(s->pb);
|
|
|
|
switch (dtype) {
|
|
|
|
case SMJPEG_SNDD:
|
|
|
|
timestamp = avio_rb32(s->pb);
|
|
|
|
size = avio_rb32(s->pb);
|
|
|
|
ret = av_get_packet(s->pb, pkt, size);
|
|
|
|
pkt->stream_index = sc->audio_stream_index;
|
|
|
|
pkt->pts = timestamp;
|
2012-02-14 03:48:42 +00:00
|
|
|
pkt->pos = pos;
|
2012-01-14 16:47:07 +00:00
|
|
|
break;
|
|
|
|
case SMJPEG_VIDD:
|
|
|
|
timestamp = avio_rb32(s->pb);
|
|
|
|
size = avio_rb32(s->pb);
|
|
|
|
ret = av_get_packet(s->pb, pkt, size);
|
|
|
|
pkt->stream_index = sc->video_stream_index;
|
|
|
|
pkt->pts = timestamp;
|
2012-02-14 03:48:42 +00:00
|
|
|
pkt->pos = pos;
|
2012-01-14 16:47:07 +00:00
|
|
|
break;
|
|
|
|
case SMJPEG_DONE:
|
|
|
|
ret = AVERROR_EOF;
|
|
|
|
break;
|
|
|
|
default:
|
2014-03-10 14:35:59 +00:00
|
|
|
av_log(s, AV_LOG_ERROR, "unknown chunk %"PRIx32"\n", dtype);
|
2012-01-14 16:47:07 +00:00
|
|
|
ret = AVERROR_INVALIDDATA;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
AVInputFormat ff_smjpeg_demuxer = {
|
|
|
|
.name = "smjpeg",
|
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("Loki SDL MJPEG"),
|
|
|
|
.priv_data_size = sizeof(SMJPEGContext),
|
|
|
|
.read_probe = smjpeg_probe,
|
|
|
|
.read_header = smjpeg_read_header,
|
|
|
|
.read_packet = smjpeg_read_packet,
|
|
|
|
.extensions = "mjpg",
|
2012-02-14 03:48:42 +00:00
|
|
|
.flags = AVFMT_GENERIC_INDEX,
|
2012-01-14 16:47:07 +00:00
|
|
|
};
|