2010-11-26 12:11:16 +02:00
|
|
|
/*
|
|
|
|
* MxPEG clip file demuxer
|
|
|
|
* Copyright (c) 2010 Anatoly Nenashev
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg 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.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* 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
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2012-04-08 00:22:05 +03:00
|
|
|
#include "libavutil/channel_layout.h"
|
2013-03-27 20:36:51 +03:00
|
|
|
#include "libavutil/internal.h"
|
2010-11-26 12:11:16 +02:00
|
|
|
#include "libavutil/intreadwrite.h"
|
|
|
|
#include "libavcodec/mjpeg.h"
|
|
|
|
#include "avformat.h"
|
2011-11-29 21:28:15 +03:00
|
|
|
#include "internal.h"
|
2010-11-26 12:11:16 +02:00
|
|
|
#include "avio.h"
|
|
|
|
|
|
|
|
#define DEFAULT_PACKET_SIZE 1024
|
|
|
|
#define OVERREAD_SIZE 3
|
|
|
|
|
|
|
|
typedef struct MXGContext {
|
|
|
|
uint8_t *buffer;
|
|
|
|
uint8_t *buffer_ptr;
|
|
|
|
uint8_t *soi_ptr;
|
|
|
|
unsigned int buffer_size;
|
|
|
|
int64_t dts;
|
|
|
|
unsigned int cache_size;
|
|
|
|
} MXGContext;
|
|
|
|
|
2012-01-12 15:20:36 +03:00
|
|
|
static int mxg_read_header(AVFormatContext *s)
|
2010-11-26 12:11:16 +02:00
|
|
|
{
|
|
|
|
AVStream *video_st, *audio_st;
|
|
|
|
MXGContext *mxg = s->priv_data;
|
|
|
|
|
|
|
|
/* video parameters will be extracted from the compressed bitstream */
|
2011-06-18 12:43:24 +03:00
|
|
|
video_st = avformat_new_stream(s, NULL);
|
2010-11-26 12:11:16 +02:00
|
|
|
if (!video_st)
|
|
|
|
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 21:42:52 +03:00
|
|
|
video_st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
|
|
|
|
video_st->codecpar->codec_id = AV_CODEC_ID_MXPEG;
|
2011-11-29 21:28:15 +03:00
|
|
|
avpriv_set_pts_info(video_st, 64, 1, 1000000);
|
2010-11-26 12:11:16 +02:00
|
|
|
|
2011-06-18 12:43:24 +03:00
|
|
|
audio_st = avformat_new_stream(s, NULL);
|
2010-11-26 12:11:16 +02:00
|
|
|
if (!audio_st)
|
|
|
|
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 21:42:52 +03:00
|
|
|
audio_st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
|
|
|
|
audio_st->codecpar->codec_id = AV_CODEC_ID_PCM_ALAW;
|
|
|
|
audio_st->codecpar->channels = 1;
|
|
|
|
audio_st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
|
|
|
|
audio_st->codecpar->sample_rate = 8000;
|
|
|
|
audio_st->codecpar->bits_per_coded_sample = 8;
|
|
|
|
audio_st->codecpar->block_align = 1;
|
2011-11-29 21:28:15 +03:00
|
|
|
avpriv_set_pts_info(audio_st, 64, 1, 1000000);
|
2010-11-26 12:11:16 +02:00
|
|
|
|
|
|
|
mxg->soi_ptr = mxg->buffer_ptr = mxg->buffer = 0;
|
|
|
|
mxg->buffer_size = 0;
|
|
|
|
mxg->dts = AV_NOPTS_VALUE;
|
|
|
|
mxg->cache_size = 0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t* mxg_find_startmarker(uint8_t *p, uint8_t *end)
|
|
|
|
{
|
|
|
|
for (; p < end - 3; p += 4) {
|
2013-06-26 10:33:44 +03:00
|
|
|
uint32_t x = AV_RN32(p);
|
2010-11-26 12:11:16 +02:00
|
|
|
|
|
|
|
if (x & (~(x+0x01010101)) & 0x80808080) {
|
|
|
|
if (p[0] == 0xff) {
|
|
|
|
return p;
|
|
|
|
} else if (p[1] == 0xff) {
|
|
|
|
return p+1;
|
|
|
|
} else if (p[2] == 0xff) {
|
|
|
|
return p+2;
|
|
|
|
} else if (p[3] == 0xff) {
|
|
|
|
return p+3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; p < end; ++p) {
|
|
|
|
if (*p == 0xff) return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
return end;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mxg_update_cache(AVFormatContext *s, unsigned int cache_size)
|
|
|
|
{
|
|
|
|
MXGContext *mxg = s->priv_data;
|
|
|
|
unsigned int current_pos = mxg->buffer_ptr - mxg->buffer;
|
|
|
|
unsigned int soi_pos;
|
2013-02-22 16:37:35 +03:00
|
|
|
uint8_t *buffer;
|
2010-11-26 12:11:16 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* reallocate internal buffer */
|
|
|
|
if (current_pos > current_pos + cache_size)
|
|
|
|
return AVERROR(ENOMEM);
|
2011-12-31 18:51:35 +03:00
|
|
|
soi_pos = mxg->soi_ptr - mxg->buffer;
|
2013-02-22 16:37:35 +03:00
|
|
|
buffer = av_fast_realloc(mxg->buffer, &mxg->buffer_size,
|
|
|
|
current_pos + cache_size +
|
2015-07-27 22:53:16 +02:00
|
|
|
AV_INPUT_BUFFER_PADDING_SIZE);
|
2013-02-22 16:37:35 +03:00
|
|
|
if (!buffer)
|
2010-11-26 12:11:16 +02:00
|
|
|
return AVERROR(ENOMEM);
|
2013-02-22 16:37:35 +03:00
|
|
|
mxg->buffer = buffer;
|
2010-11-26 12:11:16 +02:00
|
|
|
mxg->buffer_ptr = mxg->buffer + current_pos;
|
|
|
|
if (mxg->soi_ptr) mxg->soi_ptr = mxg->buffer + soi_pos;
|
|
|
|
|
|
|
|
/* get data */
|
2011-02-21 17:43:01 +02:00
|
|
|
ret = avio_read(s->pb, mxg->buffer_ptr + mxg->cache_size,
|
2010-11-26 12:11:16 +02:00
|
|
|
cache_size - mxg->cache_size);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
mxg->cache_size += ret;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mxg_read_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
unsigned int size;
|
|
|
|
uint8_t *startmarker_ptr, *end, *search_end, marker;
|
|
|
|
MXGContext *mxg = s->priv_data;
|
|
|
|
|
2014-08-07 23:12:41 +03:00
|
|
|
while (!avio_feof(s->pb) && !s->pb->error){
|
2010-11-26 12:11:16 +02:00
|
|
|
if (mxg->cache_size <= OVERREAD_SIZE) {
|
|
|
|
/* update internal buffer */
|
|
|
|
ret = mxg_update_cache(s, DEFAULT_PACKET_SIZE + OVERREAD_SIZE);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
end = mxg->buffer_ptr + mxg->cache_size;
|
|
|
|
|
|
|
|
/* find start marker - 0xff */
|
|
|
|
if (mxg->cache_size > OVERREAD_SIZE) {
|
|
|
|
search_end = end - OVERREAD_SIZE;
|
|
|
|
startmarker_ptr = mxg_find_startmarker(mxg->buffer_ptr, search_end);
|
|
|
|
} else {
|
|
|
|
search_end = end;
|
|
|
|
startmarker_ptr = mxg_find_startmarker(mxg->buffer_ptr, search_end);
|
|
|
|
if (startmarker_ptr >= search_end - 1 ||
|
|
|
|
*(startmarker_ptr + 1) != EOI) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (startmarker_ptr != search_end) { /* start marker found */
|
|
|
|
marker = *(startmarker_ptr + 1);
|
|
|
|
mxg->buffer_ptr = startmarker_ptr + 2;
|
|
|
|
mxg->cache_size = end - mxg->buffer_ptr;
|
|
|
|
|
|
|
|
if (marker == SOI) {
|
|
|
|
mxg->soi_ptr = startmarker_ptr;
|
|
|
|
} else if (marker == EOI) {
|
|
|
|
if (!mxg->soi_ptr) {
|
|
|
|
av_log(s, AV_LOG_WARNING, "Found EOI before SOI, skipping\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-02-27 05:09:47 +02:00
|
|
|
size = mxg->buffer_ptr - mxg->soi_ptr;
|
|
|
|
ret = av_new_packet(pkt, size);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
memcpy(pkt->data, mxg->soi_ptr, size);
|
|
|
|
|
2010-11-26 12:11:16 +02:00
|
|
|
pkt->pts = pkt->dts = mxg->dts;
|
2011-06-18 12:43:24 +03:00
|
|
|
pkt->stream_index = 0;
|
2010-11-26 12:11:16 +02:00
|
|
|
|
|
|
|
if (mxg->soi_ptr - mxg->buffer > mxg->cache_size) {
|
|
|
|
if (mxg->cache_size > 0) {
|
2015-09-01 22:45:07 +02:00
|
|
|
memmove(mxg->buffer, mxg->buffer_ptr, mxg->cache_size);
|
2010-11-26 12:11:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
mxg->buffer_ptr = mxg->buffer;
|
|
|
|
}
|
|
|
|
mxg->soi_ptr = 0;
|
|
|
|
|
|
|
|
return pkt->size;
|
|
|
|
} else if ( (SOF0 <= marker && marker <= SOF15) ||
|
|
|
|
(SOS <= marker && marker <= COM) ) {
|
|
|
|
/* all other markers that start marker segment also contain
|
|
|
|
length value (see specification for JPEG Annex B.1) */
|
|
|
|
size = AV_RB16(mxg->buffer_ptr);
|
|
|
|
if (size < 2)
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
|
|
|
|
if (mxg->cache_size < size) {
|
|
|
|
ret = mxg_update_cache(s, size);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
startmarker_ptr = mxg->buffer_ptr - 2;
|
|
|
|
mxg->cache_size = 0;
|
|
|
|
} else {
|
|
|
|
mxg->cache_size -= size;
|
|
|
|
}
|
|
|
|
|
|
|
|
mxg->buffer_ptr += size;
|
|
|
|
|
|
|
|
if (marker == APP13 && size >= 16) { /* audio data */
|
2018-02-27 05:09:47 +02:00
|
|
|
ret = av_new_packet(pkt, size - 14);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
memcpy(pkt->data, startmarker_ptr + 16, size - 14);
|
|
|
|
|
2010-11-26 12:11:16 +02:00
|
|
|
/* time (GMT) of first sample in usec since 1970, little-endian */
|
|
|
|
pkt->pts = pkt->dts = AV_RL64(startmarker_ptr + 8);
|
2011-06-18 12:43:24 +03:00
|
|
|
pkt->stream_index = 1;
|
2010-11-26 12:11:16 +02:00
|
|
|
|
|
|
|
if (startmarker_ptr - mxg->buffer > mxg->cache_size) {
|
|
|
|
if (mxg->cache_size > 0) {
|
|
|
|
memcpy(mxg->buffer, mxg->buffer_ptr, mxg->cache_size);
|
|
|
|
}
|
|
|
|
mxg->buffer_ptr = mxg->buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pkt->size;
|
|
|
|
} else if (marker == COM && size >= 18 &&
|
|
|
|
!strncmp(startmarker_ptr + 4, "MXF", 3)) {
|
|
|
|
/* time (GMT) of video frame in usec since 1970, little-endian */
|
|
|
|
mxg->dts = AV_RL64(startmarker_ptr + 12);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* start marker not found */
|
|
|
|
mxg->buffer_ptr = search_end;
|
|
|
|
mxg->cache_size = OVERREAD_SIZE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return AVERROR_EOF;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mxg_close(struct AVFormatContext *s)
|
|
|
|
{
|
|
|
|
MXGContext *mxg = s->priv_data;
|
|
|
|
av_freep(&mxg->buffer);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-04-19 19:45:24 +02:00
|
|
|
const AVInputFormat ff_mxg_demuxer = {
|
2012-04-06 17:50:48 +03:00
|
|
|
.name = "mxg",
|
2012-07-25 00:51:41 +03:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("MxPEG clip"),
|
2010-11-26 12:11:16 +02:00
|
|
|
.priv_data_size = sizeof(MXGContext),
|
2012-04-06 17:50:48 +03:00
|
|
|
.read_header = mxg_read_header,
|
|
|
|
.read_packet = mxg_read_packet,
|
|
|
|
.read_close = mxg_close,
|
|
|
|
.extensions = "mxg",
|
2010-11-26 12:11:16 +02:00
|
|
|
};
|