1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2025-08-10 06:10:52 +02:00

avidec: K&R formatting cosmetics

Signed-off-by: Diego Biurrun <diego@biurrun.de>
Signed-off-by: Luca Barbato <lu_zero@gentoo.org>
This commit is contained in:
Luca Barbato
2013-07-27 12:47:09 +02:00
parent 37063714c0
commit 10aa44aa67

View File

@@ -19,15 +19,15 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
#include "libavutil/intreadwrite.h" #include "libavutil/avstring.h"
#include "libavutil/mathematics.h"
#include "libavutil/bswap.h" #include "libavutil/bswap.h"
#include "libavutil/dict.h" #include "libavutil/dict.h"
#include "libavutil/avstring.h" #include "libavutil/intreadwrite.h"
#include "libavutil/mathematics.h"
#include "avformat.h" #include "avformat.h"
#include "internal.h"
#include "avi.h" #include "avi.h"
#include "dv.h" #include "dv.h"
#include "internal.h"
#include "riff.h" #include "riff.h"
#undef NDEBUG #undef NDEBUG
@@ -35,21 +35,22 @@
typedef struct AVIStream { typedef struct AVIStream {
int64_t frame_offset; /* current frame (video) or byte (audio) counter int64_t frame_offset; /* current frame (video) or byte (audio) counter
(used to compute the pts) */ * (used to compute the pts) */
int remaining; int remaining;
int packet_size; int packet_size;
uint32_t scale; uint32_t scale;
uint32_t rate; uint32_t rate;
int sample_size; /* size of one sample (or packet) (in the rate/scale sense) in bytes */ int sample_size; /* size of one sample (or packet)
* (in the rate/scale sense) in bytes */
int64_t cum_len; /* temporary storage (used during seek) */ int64_t cum_len; /* temporary storage (used during seek) */
int prefix; /* normally 'd'<<8 + 'c' or 'w'<<8 + 'b' */
int prefix; ///< normally 'd'<<8 + 'c' or 'w'<<8 + 'b'
int prefix_count; int prefix_count;
uint32_t pal[256]; uint32_t pal[256];
int has_pal; int has_pal;
int dshow_block_align; ///< block align variable used to emulate bugs in the MS dshow demuxer int dshow_block_align; /* block align variable used to emulate bugs in
* the MS dshow demuxer */
AVFormatContext *sub_ctx; AVFormatContext *sub_ctx;
AVPacket sub_pkt; AVPacket sub_pkt;
@@ -96,12 +97,13 @@ static int guess_ni_flag(AVFormatContext *s);
(tag >> 24) & 0xff, \ (tag >> 24) & 0xff, \
size) size)
static inline int get_duration(AVIStream *ast, int len){ static inline int get_duration(AVIStream *ast, int len)
if(ast->sample_size){ {
if (ast->sample_size)
return len; return len;
}else if (ast->dshow_block_align){ else if (ast->dshow_block_align)
return (len + ast->dshow_block_align - 1) / ast->dshow_block_align; return (len + ast->dshow_block_align - 1) / ast->dshow_block_align;
}else else
return 1; return 1;
} }
@@ -124,12 +126,14 @@ static int get_riff(AVFormatContext *s, AVIOContext *pb)
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
if (header[7] == 0x19) if (header[7] == 0x19)
av_log(s, AV_LOG_INFO, "This file has been generated by a totally broken muxer.\n"); av_log(s, AV_LOG_INFO,
"This file has been generated by a totally broken muxer.\n");
return 0; return 0;
} }
static int read_braindead_odml_indx(AVFormatContext *s, int frame_num){ static int read_braindead_odml_indx(AVFormatContext *s, int frame_num)
{
AVIContext *avi = s->priv_data; AVIContext *avi = s->priv_data;
AVIOContext *pb = s->pb; AVIOContext *pb = s->pb;
int longs_pre_entry = avio_rl16(pb); int longs_pre_entry = avio_rl16(pb);
@@ -138,15 +142,22 @@ static int read_braindead_odml_indx(AVFormatContext *s, int frame_num){
int entries_in_use = avio_rl32(pb); int entries_in_use = avio_rl32(pb);
int chunk_id = avio_rl32(pb); int chunk_id = avio_rl32(pb);
int64_t base = avio_rl64(pb); int64_t base = avio_rl64(pb);
int stream_id= 10*((chunk_id&0xFF) - '0') + (((chunk_id>>8)&0xFF) - '0'); int stream_id = ((chunk_id & 0xFF) - '0') * 10 +
((chunk_id >> 8 & 0xFF) - '0');
AVStream *st; AVStream *st;
AVIStream *ast; AVIStream *ast;
int i; int i;
int64_t last_pos = -1; int64_t last_pos = -1;
int64_t filesize = avio_size(s->pb); int64_t filesize = avio_size(s->pb);
av_dlog(s, "longs_pre_entry:%d index_type:%d entries_in_use:%d chunk_id:%X base:%16"PRIX64"\n", av_dlog(s,
longs_pre_entry,index_type, entries_in_use, chunk_id, base); "longs_pre_entry:%d index_type:%d entries_in_use:%d "
"chunk_id:%X base:%16"PRIX64"\n",
longs_pre_entry,
index_type,
entries_in_use,
chunk_id,
base);
if (stream_id >= s->nb_streams || stream_id < 0) if (stream_id >= s->nb_streams || stream_id < 0)
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
@@ -165,7 +176,9 @@ static int read_braindead_odml_indx(AVFormatContext *s, int frame_num){
if (filesize > 0 && base >= filesize) { if (filesize > 0 && base >= filesize) {
av_log(s, AV_LOG_ERROR, "ODML index invalid\n"); av_log(s, AV_LOG_ERROR, "ODML index invalid\n");
if(base>>32 == (base & 0xFFFFFFFF) && (base & 0xFFFFFFFF) < filesize && filesize <= 0xFFFFFFFF) if (base >> 32 == (base & 0xFFFFFFFF) &&
(base & 0xFFFFFFFF) < filesize &&
filesize <= 0xFFFFFFFF)
base &= 0xFFFFFFFF; base &= 0xFFFFFFFF;
else else
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
@@ -186,7 +199,8 @@ static int read_braindead_odml_indx(AVFormatContext *s, int frame_num){
if (last_pos == pos || pos == base - 8) if (last_pos == pos || pos == base - 8)
avi->non_interleaved = 1; avi->non_interleaved = 1;
if (last_pos != pos && (len || !ast->sample_size)) if (last_pos != pos && (len || !ast->sample_size))
av_add_index_entry(st, pos, ast->cum_len, len, 0, key ? AVINDEX_KEYFRAME : 0); av_add_index_entry(st, pos, ast->cum_len, len, 0,
key ? AVINDEX_KEYFRAME : 0);
ast->cum_len += get_duration(ast, len); ast->cum_len += get_duration(ast, len);
last_pos = pos; last_pos = pos;
@@ -220,7 +234,8 @@ static int read_braindead_odml_indx(AVFormatContext *s, int frame_num){
return 0; return 0;
} }
static void clean_index(AVFormatContext *s){ static void clean_index(AVFormatContext *s)
{
int i; int i;
int64_t j; int64_t j;
@@ -234,22 +249,25 @@ static void clean_index(AVFormatContext *s){
if (n != 1 || ast->sample_size == 0) if (n != 1 || ast->sample_size == 0)
continue; continue;
while(max < 1024) max+=max; while (max < 1024)
max += max;
pos = st->index_entries[0].pos; pos = st->index_entries[0].pos;
size = st->index_entries[0].size; size = st->index_entries[0].size;
ts = st->index_entries[0].timestamp; ts = st->index_entries[0].timestamp;
for(j=0; j<size; j+=max){ for (j = 0; j < size; j += max)
av_add_index_entry(st, pos+j, ts+j, FFMIN(max, size-j), 0, AVINDEX_KEYFRAME); av_add_index_entry(st, pos + j, ts + j, FFMIN(max, size - j), 0,
} AVINDEX_KEYFRAME);
} }
} }
static int avi_read_tag(AVFormatContext *s, AVStream *st, uint32_t tag, uint32_t size) static int avi_read_tag(AVFormatContext *s, AVStream *st, uint32_t tag,
uint32_t size)
{ {
AVIOContext *pb = s->pb; AVIOContext *pb = s->pb;
char key[5] = {0}, *value; char key[5] = { 0 };
char *value;
size += (size & 1); size += (size & 1);
@@ -295,7 +313,8 @@ static void avi_read_nikon(AVFormatContext *s, uint64_t end)
uint32_t tag = avio_rl32(s->pb); uint32_t tag = avio_rl32(s->pb);
uint32_t size = avio_rl32(s->pb); uint32_t size = avio_rl32(s->pb);
switch (tag) { switch (tag) {
case MKTAG('n', 'c', 't', 'g'): { /* Nikon Tags */ case MKTAG('n', 'c', 't', 'g'): /* Nikon Tags */
{
uint64_t tag_end = avio_tell(s->pb) + size; uint64_t tag_end = avio_tell(s->pb) + size;
while (avio_tell(s->pb) < tag_end) { while (avio_tell(s->pb) < tag_end) {
uint16_t tag = avio_rl16(s->pb); uint16_t tag = avio_rl16(s->pb);
@@ -305,9 +324,14 @@ static void avi_read_nikon(AVFormatContext *s, uint64_t end)
size -= avio_read(s->pb, buffer, size -= avio_read(s->pb, buffer,
FFMIN(size, sizeof(buffer) - 1)); FFMIN(size, sizeof(buffer) - 1));
switch (tag) { switch (tag) {
case 0x03: name = "maker"; break; case 0x03:
case 0x04: name = "model"; break; name = "maker";
case 0x13: name = "creation_time"; break;
case 0x04:
name = "model";
break;
case 0x13:
name = "creation_time";
if (buffer[4] == ':' && buffer[7] == ':') if (buffer[4] == ':' && buffer[7] == ':')
buffer[4] = buffer[7] = '-'; buffer[4] = buffer[7] = '-';
break; break;
@@ -372,18 +396,20 @@ static int avi_read_header(AVFormatContext *s)
if (tag1 == MKTAG('m', 'o', 'v', 'i')) { if (tag1 == MKTAG('m', 'o', 'v', 'i')) {
avi->movi_list = avio_tell(pb) - 4; avi->movi_list = avio_tell(pb) - 4;
if(size) avi->movi_end = avi->movi_list + size + (size & 1); if (size)
else avi->movi_end = avio_size(pb); avi->movi_end = avi->movi_list + size + (size & 1);
else
avi->movi_end = avio_size(pb);
av_dlog(NULL, "movi end=%"PRIx64"\n", avi->movi_end); av_dlog(NULL, "movi end=%"PRIx64"\n", avi->movi_end);
goto end_of_header; goto end_of_header;
} } else if (tag1 == MKTAG('I', 'N', 'F', 'O'))
else if (tag1 == MKTAG('I', 'N', 'F', 'O'))
ff_read_riff_info(s, size - 4); ff_read_riff_info(s, size - 4);
else if (tag1 == MKTAG('n', 'c', 'd', 't')) else if (tag1 == MKTAG('n', 'c', 'd', 't'))
avi_read_nikon(s, list_end); avi_read_nikon(s, list_end);
break; break;
case MKTAG('I', 'D', 'I', 'T'): { case MKTAG('I', 'D', 'I', 'T'):
{
unsigned char date[64] = { 0 }; unsigned char date[64] = { 0 };
size += (size & 1); size += (size & 1);
size -= avio_read(pb, date, FFMIN(size, sizeof(date) - 1)); size -= avio_read(pb, date, FFMIN(size, sizeof(date) - 1));
@@ -435,17 +461,17 @@ static int avi_read_header(AVFormatContext *s)
st->priv_data = ast; st->priv_data = ast;
} }
if (amv_file_format) if (amv_file_format)
tag1 = stream_index ? MKTAG('a','u','d','s') : MKTAG('v','i','d','s'); tag1 = stream_index ? MKTAG('a', 'u', 'd', 's')
: MKTAG('v', 'i', 'd', 's');
print_tag("strh", tag1, -1); print_tag("strh", tag1, -1);
if(tag1 == MKTAG('i', 'a', 'v', 's') || tag1 == MKTAG('i', 'v', 'a', 's')){ if (tag1 == MKTAG('i', 'a', 'v', 's') ||
tag1 == MKTAG('i', 'v', 'a', 's')) {
int64_t dv_dur; int64_t dv_dur;
/* /* After some consideration -- I don't think we
* After some consideration -- I don't think we * have to support anything but DV in type1 AVIs. */
* have to support anything but DV in type1 AVIs.
*/
if (s->nb_streams != 1) if (s->nb_streams != 1)
goto fail; goto fail;
@@ -475,10 +501,8 @@ static int avi_read_header(AVFormatContext *s)
dv_dur *= AV_TIME_BASE; dv_dur *= AV_TIME_BASE;
s->duration = av_rescale(dv_dur, ast->scale, ast->rate); s->duration = av_rescale(dv_dur, ast->scale, ast->rate);
} }
/* /* else, leave duration alone; timing estimation in utils.c
* else, leave duration alone; timing estimation in utils.c * will make a guess based on bitrate. */
* will make a guess based on bitrate.
*/
stream_index = s->nb_streams - 1; stream_index = s->nb_streams - 1;
avio_skip(pb, size - 9 * 4); avio_skip(pb, size - 9 * 4);
@@ -495,7 +519,11 @@ static int avi_read_header(AVFormatContext *s)
ast->scale = avio_rl32(pb); ast->scale = avio_rl32(pb);
ast->rate = avio_rl32(pb); ast->rate = avio_rl32(pb);
if (!(ast->scale && ast->rate)) { if (!(ast->scale && ast->rate)) {
av_log(s, AV_LOG_WARNING, "scale/rate is %u/%u which is invalid. (This file has been generated by broken software.)\n", ast->scale, ast->rate); av_log(s, AV_LOG_WARNING,
"scale/rate is %u/%u which is invalid. "
"(This file has been generated by broken software.)\n",
ast->scale,
ast->rate);
if (frame_period) { if (frame_period) {
ast->rate = 1000000; ast->rate = 1000000;
ast->scale = frame_period; ast->scale = frame_period;
@@ -562,7 +590,8 @@ static int avi_read_header(AVFormatContext *s)
} }
tag1 = ff_get_bmp_header(pb, st); tag1 = ff_get_bmp_header(pb, st);
if (tag1 == MKTAG('D', 'X', 'S', 'B') || tag1 == MKTAG('D','X','S','A')) { if (tag1 == MKTAG('D', 'X', 'S', 'B') ||
tag1 == MKTAG('D', 'X', 'S', 'A')) {
st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE; st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
st->codec->codec_tag = tag1; st->codec->codec_tag = tag1;
st->codec->codec_id = AV_CODEC_ID_XSUB; st->codec->codec_id = AV_CODEC_ID_XSUB;
@@ -571,26 +600,33 @@ static int avi_read_header(AVFormatContext *s)
if (size > 10 * 4 && size < (1 << 30)) { if (size > 10 * 4 && size < (1 << 30)) {
st->codec->extradata_size = size - 10 * 4; st->codec->extradata_size = size - 10 * 4;
st->codec->extradata= av_malloc(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE); st->codec->extradata = av_malloc(st->codec->extradata_size +
FF_INPUT_BUFFER_PADDING_SIZE);
if (!st->codec->extradata) { if (!st->codec->extradata) {
st->codec->extradata_size = 0; st->codec->extradata_size = 0;
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
} }
avio_read(pb, st->codec->extradata, st->codec->extradata_size); avio_read(pb,
st->codec->extradata,
st->codec->extradata_size);
} }
if(st->codec->extradata_size & 1) //FIXME check if the encoder really did this correctly // FIXME: check if the encoder really did this correctly
if (st->codec->extradata_size & 1)
avio_r8(pb); avio_r8(pb);
/* Extract palette from extradata if bpp <= 8. */ /* Extract palette from extradata if bpp <= 8.
/* This code assumes that extradata contains only palette. */ * This code assumes that extradata contains only palette.
/* This is true for all paletted codecs implemented in Libav. */ * This is true for all paletted codecs implemented in
if (st->codec->extradata_size && (st->codec->bits_per_coded_sample <= 8)) { * Libav. */
if (st->codec->extradata_size &&
(st->codec->bits_per_coded_sample <= 8)) {
int pal_size = (1 << st->codec->bits_per_coded_sample) << 2; int pal_size = (1 << st->codec->bits_per_coded_sample) << 2;
const uint8_t *pal_src; const uint8_t *pal_src;
pal_size = FFMIN(pal_size, st->codec->extradata_size); pal_size = FFMIN(pal_size, st->codec->extradata_size);
pal_src = st->codec->extradata + st->codec->extradata_size - pal_size; pal_src = st->codec->extradata +
st->codec->extradata_size - pal_size;
#if HAVE_BIGENDIAN #if HAVE_BIGENDIAN
for (i = 0; i < pal_size / 4; i++) for (i = 0; i < pal_size / 4; i++)
ast->pal[i] = av_bswap32(((uint32_t *)pal_src)[i]); ast->pal[i] = av_bswap32(((uint32_t *)pal_src)[i]);
@@ -604,19 +640,26 @@ static int avi_read_header(AVFormatContext *s)
st->codec->codec_type = AVMEDIA_TYPE_VIDEO; st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
st->codec->codec_tag = tag1; st->codec->codec_tag = tag1;
st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags, tag1); st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags,
st->need_parsing = AVSTREAM_PARSE_HEADERS; // This is needed to get the pict type which is necessary for generating correct pts. tag1);
/* This is needed to get the pict type which is necessary
* for generating correct pts. */
st->need_parsing = AVSTREAM_PARSE_HEADERS;
// Support "Resolution 1:1" for Avid AVI Codec // Support "Resolution 1:1" for Avid AVI Codec
if (tag1 == MKTAG('A', 'V', 'R', 'n') && if (tag1 == MKTAG('A', 'V', 'R', 'n') &&
st->codec->extradata_size >= 31 && st->codec->extradata_size >= 31 &&
!memcmp(&st->codec->extradata[28], "1:1", 3)) !memcmp(&st->codec->extradata[28], "1:1", 3))
st->codec->codec_id = AV_CODEC_ID_RAWVIDEO; st->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
if(st->codec->codec_tag==0 && st->codec->height > 0 && st->codec->extradata_size < 1U<<30){ if (st->codec->codec_tag == 0 && st->codec->height > 0 &&
st->codec->extradata_size < 1U << 30) {
st->codec->extradata_size += 9; st->codec->extradata_size += 9;
st->codec->extradata= av_realloc(st->codec->extradata, st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE); st->codec->extradata = av_realloc(st->codec->extradata,
st->codec->extradata_size +
FF_INPUT_BUFFER_PADDING_SIZE);
if (st->codec->extradata) if (st->codec->extradata)
memcpy(st->codec->extradata + st->codec->extradata_size - 9, "BottomUp", 9); memcpy(st->codec->extradata + st->codec->extradata_size - 9,
"BottomUp", 9);
} }
st->codec->height = FFABS(st->codec->height); st->codec->height = FFABS(st->codec->height);
@@ -627,11 +670,18 @@ static int avi_read_header(AVFormatContext *s)
if (ret < 0) if (ret < 0)
return ret; return ret;
ast->dshow_block_align = st->codec->block_align; ast->dshow_block_align = st->codec->block_align;
if(ast->sample_size && st->codec->block_align && ast->sample_size != st->codec->block_align){ if (ast->sample_size && st->codec->block_align &&
av_log(s, AV_LOG_WARNING, "sample size (%d) != block align (%d)\n", ast->sample_size, st->codec->block_align); ast->sample_size != st->codec->block_align) {
av_log(s,
AV_LOG_WARNING,
"sample size (%d) != block align (%d)\n",
ast->sample_size,
st->codec->block_align);
ast->sample_size = st->codec->block_align; ast->sample_size = st->codec->block_align;
} }
if (size&1) /* 2-aligned (fix for Stargate SG-1 - 3x18 - Shades of Grey.avi) */ /* 2-aligned
* (fix for Stargate SG-1 - 3x18 - Shades of Grey.avi) */
if (size & 1)
avio_skip(pb, 1); avio_skip(pb, 1);
/* Force parsing as several audio frames can be in /* Force parsing as several audio frames can be in
* one packet and timestamps refer to packet start. */ * one packet and timestamps refer to packet start. */
@@ -639,7 +689,8 @@ static int avi_read_header(AVFormatContext *s)
/* ADTS header is in extradata, AAC without header must be /* ADTS header is in extradata, AAC without header must be
* stored as exact frames. Parser not needed and it will * stored as exact frames. Parser not needed and it will
* fail. */ * fail. */
if (st->codec->codec_id == AV_CODEC_ID_AAC && st->codec->extradata_size) if (st->codec->codec_id == AV_CODEC_ID_AAC &&
st->codec->extradata_size)
st->need_parsing = AVSTREAM_PARSE_NONE; st->need_parsing = AVSTREAM_PARSE_NONE;
/* AVI files with Xan DPCM audio (wrongly) declare PCM /* AVI files with Xan DPCM audio (wrongly) declare PCM
* audio in the header but have Axan as stream_code_tag. */ * audio in the header but have Axan as stream_code_tag. */
@@ -690,7 +741,8 @@ static int avi_read_header(AVFormatContext *s)
active.den = avio_rl32(pb); active.den = avio_rl32(pb);
avio_rl32(pb); // nbFieldsPerFrame avio_rl32(pb); // nbFieldsPerFrame
if(active_aspect.num && active_aspect.den && active.num && active.den){ if (active_aspect.num && active_aspect.den &&
active.num && active.den) {
st->sample_aspect_ratio = av_div_q(active_aspect, active); st->sample_aspect_ratio = av_div_q(active_aspect, active);
av_dlog(s, "vprp %d/%d %d/%d\n", av_dlog(s, "vprp %d/%d %d/%d\n",
active_aspect.num, active_aspect.den, active_aspect.num, active_aspect.den,
@@ -709,7 +761,8 @@ static int avi_read_header(AVFormatContext *s)
} }
default: default:
if (size > 1000000) { if (size > 1000000) {
av_log(s, AV_LOG_ERROR, "Something went wrong during header parsing, " av_log(s, AV_LOG_ERROR,
"Something went wrong during header parsing, "
"I will ignore it and try to continue anyway.\n"); "I will ignore it and try to continue anyway.\n");
if (s->error_recognition & AV_EF_EXPLODE) if (s->error_recognition & AV_EF_EXPLODE)
goto fail; goto fail;
@@ -723,9 +776,11 @@ static int avi_read_header(AVFormatContext *s)
break; break;
} }
} }
end_of_header: end_of_header:
/* check stream number */ /* check stream number */
if (stream_index != s->nb_streams - 1) { if (stream_index != s->nb_streams - 1) {
fail: fail:
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
} }
@@ -740,7 +795,8 @@ static int avi_read_header(AVFormatContext *s)
break; break;
} }
if (i == s->nb_streams && avi->non_interleaved) { if (i == s->nb_streams && avi->non_interleaved) {
av_log(s, AV_LOG_WARNING, "non-interleaved AVI without index, switching to interleaved\n"); av_log(s, AV_LOG_WARNING,
"Non-interleaved AVI without index, switching to interleaved\n");
avi->non_interleaved = 0; avi->non_interleaved = 0;
} }
@@ -755,7 +811,8 @@ static int avi_read_header(AVFormatContext *s)
return 0; return 0;
} }
static int read_gab2_sub(AVStream *st, AVPacket *pkt) { static int read_gab2_sub(AVStream *st, AVPacket *pkt)
{
if (!strcmp(pkt->data, "GAB2") && AV_RL16(pkt->data + 5) == 2) { if (!strcmp(pkt->data, "GAB2") && AV_RL16(pkt->data + 5) == 2) {
uint8_t desc[256]; uint8_t desc[256];
int score = AVPROBE_SCORE_EXTENSION, ret; int score = AVPROBE_SCORE_EXTENSION, ret;
@@ -779,7 +836,8 @@ static int read_gab2_sub(AVStream *st, AVPacket *pkt) {
avio_rl16(pb); /* flags? */ avio_rl16(pb); /* flags? */
avio_rl32(pb); /* data size */ avio_rl32(pb); /* data size */
pd = (AVProbeData) { .buf = pb->buf_ptr, .buf_size = pb->buf_end - pb->buf_ptr }; pd = (AVProbeData) { .buf = pb->buf_ptr,
.buf_size = pb->buf_end - pb->buf_ptr };
if (!(sub_demuxer = av_probe_input_format2(&pd, 1, &score))) if (!(sub_demuxer = av_probe_input_format2(&pd, 1, &score)))
goto error; goto error;
@@ -797,6 +855,7 @@ static int read_gab2_sub(AVStream *st, AVPacket *pkt) {
ast->sub_buffer = pkt->data; ast->sub_buffer = pkt->data;
memset(pkt, 0, sizeof(*pkt)); memset(pkt, 0, sizeof(*pkt));
return 1; return 1;
error: error:
av_freep(&pb); av_freep(&pb);
} }
@@ -830,15 +889,17 @@ static AVStream *get_subtitle_pkt(AVFormatContext *s, AVStream *next_st,
ast = sub_st->priv_data; ast = sub_st->priv_data;
*pkt = ast->sub_pkt; *pkt = ast->sub_pkt;
pkt->stream_index = sub_st->index; pkt->stream_index = sub_st->index;
if (ff_read_packet(ast->sub_ctx, &ast->sub_pkt) < 0) if (ff_read_packet(ast->sub_ctx, &ast->sub_pkt) < 0)
ast->sub_pkt.data = NULL; ast->sub_pkt.data = NULL;
} }
return sub_st; return sub_st;
} }
static int get_stream_idx(int *d){ static int get_stream_idx(int *d)
if( d[0] >= '0' && d[0] <= '9' {
&& d[1] >= '0' && d[1] <= '9'){ if (d[0] >= '0' && d[0] <= '9' &&
d[1] >= '0' && d[1] <= '9') {
return (d[0] - '0') * 10 + (d[1] - '0'); return (d[0] - '0') * 10 + (d[1] - '0');
} else { } else {
return 100; // invalid stream ID return 100; // invalid stream ID
@@ -872,10 +933,10 @@ start_sync:
continue; continue;
// parse ix## // parse ix##
if( (d[0] == 'i' && d[1] == 'x' && n < s->nb_streams) if ((d[0] == 'i' && d[1] == 'x' && n < s->nb_streams) ||
// parse JUNK // parse JUNK
||(d[0] == 'J' && d[1] == 'U' && d[2] == 'N' && d[3] == 'K') (d[0] == 'J' && d[1] == 'U' && d[2] == 'N' && d[3] == 'K') ||
||(d[0] == 'i' && d[1] == 'd' && d[2] == 'x' && d[3] == '1')){ (d[0] == 'i' && d[1] == 'd' && d[2] == 'x' && d[3] == '1')) {
avio_skip(pb, size); avio_skip(pb, size);
goto start_sync; goto start_sync;
} }
@@ -888,7 +949,8 @@ start_sync:
n = get_stream_idx(d); n = get_stream_idx(d);
if(!((i-avi->last_pkt_pos)&1) && get_stream_idx(d+1) < s->nb_streams) if (!((i - avi->last_pkt_pos) & 1) &&
get_stream_idx(d + 1) < s->nb_streams)
continue; continue;
// detect ##ix chunk and skip // detect ##ix chunk and skip
@@ -908,23 +970,22 @@ start_sync:
AVStream *st1 = s->streams[1]; AVStream *st1 = s->streams[1];
AVIStream *ast1 = st1->priv_data; AVIStream *ast1 = st1->priv_data;
// workaround for broken small-file-bug402.avi // workaround for broken small-file-bug402.avi
if( d[2] == 'w' && d[3] == 'b' if (d[2] == 'w' && d[3] == 'b' && n == 0 &&
&& n==0 st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
&& st ->codec->codec_type == AVMEDIA_TYPE_VIDEO st1->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
&& st1->codec->codec_type == AVMEDIA_TYPE_AUDIO ast->prefix == 'd' * 256 + 'c' &&
&& ast->prefix == 'd'*256+'c' (d[2] * 256 + d[3] == ast1->prefix ||
&& (d[2]*256+d[3] == ast1->prefix || !ast1->prefix_count) !ast1->prefix_count)) {
){
n = 1; n = 1;
st = st1; st = st1;
ast = ast1; ast = ast1;
av_log(s, AV_LOG_WARNING, "Invalid stream + prefix combination, assuming audio.\n"); av_log(s, AV_LOG_WARNING,
"Invalid stream + prefix combination, assuming audio.\n");
} }
} }
if ((st->discard >= AVDISCARD_DEFAULT && size == 0) if ((st->discard >= AVDISCARD_DEFAULT && size == 0)
/*|| (st->discard >= AVDISCARD_NONKEY && !(pkt->flags & AV_PKT_FLAG_KEY))*/ //FIXME needs a little reordering /* || (st->discard >= AVDISCARD_NONKEY && !(pkt->flags & AV_PKT_FLAG_KEY)) */ // FIXME: needs a little reordering
|| st->discard >= AVDISCARD_ALL) { || st->discard >= AVDISCARD_ALL) {
if (!exit_early) { if (!exit_early) {
ast->frame_offset += get_duration(ast, size); ast->frame_offset += get_duration(ast, size);
@@ -939,15 +1000,17 @@ start_sync:
avio_rl16(pb); // flags avio_rl16(pb); // flags
// b + (g << 8) + (r << 16);
for (; k <= last; k++) for (; k <= last; k++)
ast->pal[k] = avio_rb32(pb)>>8;// b + (g << 8) + (r << 16); ast->pal[k] = avio_rb32(pb) >> 8;
ast->has_pal = 1; ast->has_pal = 1;
goto start_sync; goto start_sync;
} else if( ((ast->prefix_count<5 || sync+9 > i) && d[2]<128 && d[3]<128) || } else if (((ast->prefix_count < 5 || sync + 9 > i) &&
d[2] < 128 && d[3] < 128) ||
d[2] * 256 + d[3] == ast->prefix /* || d[2] * 256 + d[3] == ast->prefix /* ||
(d[2] == 'd' && d[3] == 'c') || (d[2] == 'd' && d[3] == 'c') ||
(d[2] == 'w' && d[3] == 'b') */) { (d[2] == 'w' && d[3] == 'b') */) {
if (exit_early) if (exit_early)
return 0; return 0;
if (d[2] * 256 + d[3] == ast->prefix) if (d[2] * 256 + d[3] == ast->prefix)
@@ -963,8 +1026,10 @@ start_sync:
if (size || !ast->sample_size) { if (size || !ast->sample_size) {
uint64_t pos = avio_tell(pb) - 8; uint64_t pos = avio_tell(pb) - 8;
if(!st->index_entries || !st->nb_index_entries || st->index_entries[st->nb_index_entries - 1].pos < pos){ if (!st->index_entries || !st->nb_index_entries ||
av_add_index_entry(st, pos, ast->frame_offset, size, 0, AVINDEX_KEYFRAME); st->index_entries[st->nb_index_entries - 1].pos < pos) {
av_add_index_entry(st, pos, ast->frame_offset, size,
0, AVINDEX_KEYFRAME);
} }
} }
return 0; return 0;
@@ -1010,7 +1075,9 @@ static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
if (!ast->remaining && ts > last_ts) if (!ast->remaining && ts > last_ts)
continue; continue;
ts = av_rescale_q(ts, st->time_base, (AVRational){FFMAX(1, ast->sample_size), AV_TIME_BASE}); ts = av_rescale_q(ts, st->time_base,
(AVRational) { FFMAX(1, ast->sample_size),
AV_TIME_BASE });
av_dlog(s, "%"PRId64" %d/%d %"PRId64"\n", ts, av_dlog(s, "%"PRId64" %d/%d %"PRId64"\n", ts,
st->time_base.num, st->time_base.den, ast->frame_offset); st->time_base.num, st->time_base.den, ast->frame_offset);
@@ -1024,10 +1091,16 @@ static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
return AVERROR_EOF; return AVERROR_EOF;
best_ast = best_st->priv_data; best_ast = best_st->priv_data;
best_ts = av_rescale_q(best_ts, (AVRational){FFMAX(1, best_ast->sample_size), AV_TIME_BASE}, best_st->time_base); best_ts = av_rescale_q(best_ts,
if(best_ast->remaining) (AVRational) { FFMAX(1, best_ast->sample_size),
i= av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY | AVSEEK_FLAG_BACKWARD); AV_TIME_BASE },
else{ best_st->time_base);
if (best_ast->remaining) {
i = av_index_search_timestamp(best_st,
best_ts,
AVSEEK_FLAG_ANY |
AVSEEK_FLAG_BACKWARD);
} else {
i = av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY); i = av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY);
if (i >= 0) if (i >= 0)
best_ast->frame_offset = best_st->index_entries[i].timestamp; best_ast->frame_offset = best_st->index_entries[i].timestamp;
@@ -1056,7 +1129,8 @@ resync:
if (get_subtitle_pkt(s, st, pkt)) if (get_subtitle_pkt(s, st, pkt))
return 0; return 0;
if(ast->sample_size <= 1) // minorityreport.AVI block_align=1024 sample_size=1 IMA-ADPCM // minorityreport.AVI block_align=1024 sample_size=1 IMA-ADPCM
if (ast->sample_size <= 1)
size = INT_MAX; size = INT_MAX;
else if (ast->sample_size < 32) else if (ast->sample_size < 32)
// arbitrary multiplier to avoid tiny packets for raw PCM data // arbitrary multiplier to avoid tiny packets for raw PCM data
@@ -1073,9 +1147,12 @@ resync:
if (ast->has_pal && pkt->data && pkt->size < (unsigned)INT_MAX / 2) { if (ast->has_pal && pkt->data && pkt->size < (unsigned)INT_MAX / 2) {
uint8_t *pal; uint8_t *pal;
pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE); pal = av_packet_new_side_data(pkt,
AV_PKT_DATA_PALETTE,
AVPALETTE_SIZE);
if (!pal) { if (!pal) {
av_log(s, AV_LOG_ERROR, "Failed to allocate data for palette\n"); av_log(s, AV_LOG_ERROR,
"Failed to allocate data for palette\n");
} else { } else {
memcpy(pal, ast->pal, AVPALETTE_SIZE); memcpy(pal, ast->pal, AVPALETTE_SIZE);
ast->has_pal = 0; ast->has_pal = 0;
@@ -1096,8 +1173,8 @@ resync:
pkt->flags |= AV_PKT_FLAG_KEY; pkt->flags |= AV_PKT_FLAG_KEY;
if (size < 0) if (size < 0)
av_free_packet(pkt); av_free_packet(pkt);
} else if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE } else if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE &&
&& !st->codec->codec_tag && read_gab2_sub(st, pkt)) { !st->codec->codec_tag && read_gab2_sub(st, pkt)) {
ast->frame_offset++; ast->frame_offset++;
avi->stream_index = -1; avi->stream_index = -1;
ast->remaining = 0; ast->remaining = 0;
@@ -1108,9 +1185,17 @@ resync:
// pkt->dts += ast->start; // pkt->dts += ast->start;
if (ast->sample_size) if (ast->sample_size)
pkt->dts /= ast->sample_size; pkt->dts /= ast->sample_size;
av_dlog(s, "dts:%"PRId64" offset:%"PRId64" %d/%d smpl_siz:%d base:%d st:%d size:%d\n", av_dlog(s,
pkt->dts, ast->frame_offset, ast->scale, ast->rate, "dts:%"PRId64" offset:%"PRId64" %d/%d smpl_siz:%d "
ast->sample_size, AV_TIME_BASE, avi->stream_index, size); "base:%d st:%d size:%d\n",
pkt->dts,
ast->frame_offset,
ast->scale,
ast->rate,
ast->sample_size,
AV_TIME_BASE,
avi->stream_index,
size);
pkt->stream_index = avi->stream_index; pkt->stream_index = avi->stream_index;
if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) { if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
@@ -1121,10 +1206,9 @@ resync:
index = av_index_search_timestamp(st, ast->frame_offset, 0); index = av_index_search_timestamp(st, ast->frame_offset, 0);
e = &st->index_entries[index]; e = &st->index_entries[index];
if(index >= 0 && e->timestamp == ast->frame_offset){ if (index >= 0 && e->timestamp == ast->frame_offset)
if (e->flags & AVINDEX_KEYFRAME) if (e->flags & AVINDEX_KEYFRAME)
pkt->flags |= AV_PKT_FLAG_KEY; pkt->flags |= AV_PKT_FLAG_KEY;
}
} else { } else {
pkt->flags |= AV_PKT_FLAG_KEY; pkt->flags |= AV_PKT_FLAG_KEY;
} }
@@ -1145,7 +1229,7 @@ resync:
} }
/* XXX: We make the implicit supposition that the positions are sorted /* XXX: We make the implicit supposition that the positions are sorted
for each stream. */ * for each stream. */
static int avi_read_idx1(AVFormatContext *s, int size) static int avi_read_idx1(AVFormatContext *s, int size)
{ {
AVIContext *avi = s->priv_data; AVIContext *avi = s->priv_data;
@@ -1163,9 +1247,8 @@ static int avi_read_idx1(AVFormatContext *s, int size)
idx1_pos = avio_tell(pb); idx1_pos = avio_tell(pb);
avio_seek(pb, avi->movi_list + 4, SEEK_SET); avio_seek(pb, avi->movi_list + 4, SEEK_SET);
if (avi_sync(s, 1) == 0) { if (avi_sync(s, 1) == 0)
first_packet_pos = avio_tell(pb) - 8; first_packet_pos = avio_tell(pb) - 8;
}
avi->stream_index = -1; avi->stream_index = -1;
avio_seek(pb, idx1_pos, SEEK_SET); avio_seek(pb, idx1_pos, SEEK_SET);
@@ -1179,7 +1262,7 @@ static int avi_read_idx1(AVFormatContext *s, int size)
i, tag, flags, pos, len); i, tag, flags, pos, len);
index = ((tag & 0xff) - '0') * 10; index = ((tag & 0xff) - '0') * 10;
index += ((tag >> 8) & 0xff) - '0'; index += (tag >> 8 & 0xff) - '0';
if (index >= s->nb_streams) if (index >= s->nb_streams)
continue; continue;
st = s->streams[index]; st = s->streams[index];
@@ -1199,14 +1282,16 @@ static int avi_read_idx1(AVFormatContext *s, int size)
if (last_pos == pos) if (last_pos == pos)
avi->non_interleaved = 1; avi->non_interleaved = 1;
else if (len || !ast->sample_size) else if (len || !ast->sample_size)
av_add_index_entry(st, pos, ast->cum_len, len, 0, (flags&AVIIF_INDEX) ? AVINDEX_KEYFRAME : 0); av_add_index_entry(st, pos, ast->cum_len, len, 0,
(flags & AVIIF_INDEX) ? AVINDEX_KEYFRAME : 0);
ast->cum_len += get_duration(ast, len); ast->cum_len += get_duration(ast, len);
last_pos = pos; last_pos = pos;
} }
return 0; return 0;
} }
static int guess_ni_flag(AVFormatContext *s){ static int guess_ni_flag(AVFormatContext *s)
{
int i; int i;
int64_t last_start = 0; int64_t last_start = 0;
int64_t first_end = INT64_MAX; int64_t first_end = INT64_MAX;
@@ -1270,6 +1355,7 @@ static int avi_load_index(AVFormatContext *s)
if (avio_skip(pb, size) < 0) if (avio_skip(pb, size) < 0)
break; // something is wrong here break; // something is wrong here
} }
the_end: the_end:
avio_seek(pb, pos, SEEK_SET); avio_seek(pb, pos, SEEK_SET);
return ret; return ret;
@@ -1285,7 +1371,8 @@ static void seek_subtitle(AVStream *st, AVStream *st2, int64_t timestamp)
ff_read_packet(ast2->sub_ctx, &ast2->sub_pkt); ff_read_packet(ast2->sub_ctx, &ast2->sub_pkt);
} }
static int avi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags) static int avi_read_seek(AVFormatContext *s, int stream_index,
int64_t timestamp, int flags)
{ {
AVIContext *avi = s->priv_data; AVIContext *avi = s->priv_data;
AVStream *st; AVStream *st;
@@ -1302,7 +1389,9 @@ static int avi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp
st = s->streams[stream_index]; st = s->streams[stream_index];
ast = st->priv_data; ast = st->priv_data;
index= av_index_search_timestamp(st, timestamp * FFMAX(ast->sample_size, 1), flags); index = av_index_search_timestamp(st,
timestamp * FFMAX(ast->sample_size, 1),
flags);
if (index < 0) if (index < 0)
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
@@ -1344,10 +1433,13 @@ static int avi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp
continue; continue;
// assert(st2->codec->block_align); // assert(st2->codec->block_align);
assert((int64_t)st2->time_base.num*ast2->rate == (int64_t)st2->time_base.den*ast2->scale); assert((int64_t)st2->time_base.num * ast2->rate ==
index = av_index_search_timestamp( (int64_t)st2->time_base.den * ast2->scale);
st2, index = av_index_search_timestamp(st2,
av_rescale_q(timestamp, st->time_base, st2->time_base) * FFMAX(ast2->sample_size, 1), av_rescale_q(timestamp,
st->time_base,
st2->time_base) *
FFMAX(ast2->sample_size, 1),
flags | AVSEEK_FLAG_BACKWARD); flags | AVSEEK_FLAG_BACKWARD);
if (index < 0) if (index < 0)
index = 0; index = 0;
@@ -1355,7 +1447,8 @@ static int avi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp
if (!avi->non_interleaved) { if (!avi->non_interleaved) {
while (index > 0 && st2->index_entries[index].pos > pos) while (index > 0 && st2->index_entries[index].pos > pos)
index--; index--;
while(index+1 < st2->nb_index_entries && st2->index_entries[index].pos < pos) while (index + 1 < st2->nb_index_entries &&
st2->index_entries[index].pos < pos)
index++; index++;
} }