diff options
Diffstat (limited to 'libavformat/bink.c')
-rw-r--r-- | libavformat/bink.c | 85 |
1 files changed, 60 insertions, 25 deletions
diff --git a/libavformat/bink.c b/libavformat/bink.c index 8f28212..8a05082 100644 --- a/libavformat/bink.c +++ b/libavformat/bink.c @@ -3,20 +3,20 @@ * Copyright (c) 2008-2010 Peter Ross (pross@xvid.org) * Copyright (c) 2009 Daniel Verkamp (daniel@drv.nu) * - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * 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. * - * Libav is distributed in the hope that it will be useful, + * 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 Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -45,6 +45,7 @@ enum BinkAudFlags { #define BINK_MAX_AUDIO_TRACKS 256 #define BINK_MAX_WIDTH 7680 #define BINK_MAX_HEIGHT 4800 +#define SMUSH_BLOCK_SIZE 512 typedef struct BinkDemuxContext { uint32_t file_size; @@ -55,19 +56,28 @@ typedef struct BinkDemuxContext { int64_t audio_pts[BINK_MAX_AUDIO_TRACKS]; uint32_t remain_packet_size; + int smush_size; } BinkDemuxContext; static int probe(AVProbeData *p) { const uint8_t *b = p->buf; - - if ( b[0] == 'B' && b[1] == 'I' && b[2] == 'K' && - (b[3] == 'b' || b[3] == 'f' || b[3] == 'g' || b[3] == 'h' || b[3] == 'i') && - AV_RL32(b+8) > 0 && // num_frames - AV_RL32(b+20) > 0 && AV_RL32(b+20) <= BINK_MAX_WIDTH && - AV_RL32(b+24) > 0 && AV_RL32(b+24) <= BINK_MAX_HEIGHT && - AV_RL32(b+28) > 0 && AV_RL32(b+32) > 0) // fps num,den - return AVPROBE_SCORE_MAX; + int smush = AV_RN32(p->buf) == AV_RN32("SMUS"); + + do { + if (((b[0] == 'B' && b[1] == 'I' && b[2] == 'K' && /* Bink 1 */ + (b[3] == 'b' || b[3] == 'f' || b[3] == 'g' || b[3] == 'h' || b[3] == 'i' || + b[3] == 'k')) || + (b[0] == 'K' && b[1] == 'B' && b[2] == '2' && /* Bink 2 */ + (b[3] == 'a' || b[3] == 'd' || b[3] == 'f' || b[3] == 'g' || b[3] == 'h' || + b[3] == 'i' || b[3] == 'j' || b[3] == 'k'))) && + AV_RL32(b+8) > 0 && // num_frames + AV_RL32(b+20) > 0 && AV_RL32(b+20) <= BINK_MAX_WIDTH && + AV_RL32(b+24) > 0 && AV_RL32(b+24) <= BINK_MAX_HEIGHT && + AV_RL32(b+28) > 0 && AV_RL32(b+32) > 0) // fps num,den + return AVPROBE_SCORE_MAX; + b += SMUSH_BLOCK_SIZE; + } while (smush && b < p->buf + p->buf_size - 32); return 0; } @@ -81,12 +91,24 @@ static int read_header(AVFormatContext *s) uint32_t pos, next_pos; uint16_t flags; int keyframe; + int ret; vst = avformat_new_stream(s, NULL); if (!vst) return AVERROR(ENOMEM); vst->codecpar->codec_tag = avio_rl32(pb); + if (vst->codecpar->codec_tag == AV_RL32("SMUS")) { + do { + bink->smush_size += SMUSH_BLOCK_SIZE; + avio_skip(pb, SMUSH_BLOCK_SIZE - 4); + vst->codecpar->codec_tag = avio_rl32(pb); + } while (!avio_feof(pb) && (vst->codecpar->codec_tag & 0xFFFFFF) != AV_RL32("BIK")); + if (avio_feof(pb)) { + av_log(s, AV_LOG_ERROR, "invalid SMUSH header: BIK not found\n"); + return AVERROR_INVALIDDATA; + } + } bink->file_size = avio_rl32(pb) + 8; vst->duration = avio_rl32(pb); @@ -120,11 +142,14 @@ static int read_header(AVFormatContext *s) vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; vst->codecpar->codec_id = AV_CODEC_ID_BINKVIDEO; - vst->codecpar->extradata = av_mallocz(4 + AV_INPUT_BUFFER_PADDING_SIZE); - if (!vst->codecpar->extradata) + + if ((vst->codecpar->codec_tag & 0xFFFFFF) == MKTAG('K', 'B', '2', 0)) { + av_log(s, AV_LOG_WARNING, "Bink 2 video is not implemented\n"); + vst->codecpar->codec_id = AV_CODEC_ID_NONE; + } + + if (ff_get_extradata(s, vst->codecpar, pb, 4) < 0) return AVERROR(ENOMEM); - vst->codecpar->extradata_size = 4; - avio_read(pb, vst->codecpar->extradata, 4); bink->num_audio_tracks = avio_rl32(pb); @@ -136,7 +161,14 @@ static int read_header(AVFormatContext *s) } if (bink->num_audio_tracks) { - avio_skip(pb, 4 * bink->num_audio_tracks); + uint32_t signature = (vst->codecpar->codec_tag & 0xFFFFFF); + uint8_t revision = ((vst->codecpar->codec_tag >> 24) % 0xFF); + + if ((signature == AV_RL32("BIK") && (revision == 0x6b)) || /* k */ + (signature == AV_RL32("KB2") && (revision == 0x69 || revision == 0x6a || revision == 0x6b))) /* i,j,k */ + avio_skip(pb, 4); /* unknown new field */ + + avio_skip(pb, 4 * bink->num_audio_tracks); /* max decoded size */ for (i = 0; i < bink->num_audio_tracks; i++) { ast = avformat_new_stream(s, NULL); @@ -156,10 +188,8 @@ static int read_header(AVFormatContext *s) ast->codecpar->channels = 1; ast->codecpar->channel_layout = AV_CH_LAYOUT_MONO; } - ast->codecpar->extradata = av_mallocz(4 + AV_INPUT_BUFFER_PADDING_SIZE); - if (!ast->codecpar->extradata) + if (ff_alloc_extradata(ast->codecpar, 4)) return AVERROR(ENOMEM); - ast->codecpar->extradata_size = 4; AV_WL32(ast->codecpar->extradata, vst->codecpar->codec_tag); } @@ -185,11 +215,15 @@ static int read_header(AVFormatContext *s) av_log(s, AV_LOG_ERROR, "invalid frame index table\n"); return AVERROR(EIO); } - av_add_index_entry(vst, pos, i, next_pos - pos, 0, - keyframe ? AVINDEX_KEYFRAME : 0); + if ((ret = av_add_index_entry(vst, pos, i, next_pos - pos, 0, + keyframe ? AVINDEX_KEYFRAME : 0)) < 0) + return ret; } - avio_skip(pb, 4); + if (vst->index_entries) + avio_seek(pb, vst->index_entries[0].pos + bink->smush_size, SEEK_SET); + else + avio_skip(pb, 4); bink->current_track = -1; return 0; @@ -206,7 +240,7 @@ static int read_packet(AVFormatContext *s, AVPacket *pkt) AVStream *st = s->streams[0]; // stream 0 is video stream with index if (bink->video_pts >= st->duration) - return AVERROR(EIO); + return AVERROR_EOF; index_entry = av_index_search_timestamp(st, bink->video_pts, AVSEEK_FLAG_ANY); @@ -271,7 +305,7 @@ static int read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, in return -1; /* seek to the first frame */ - if (avio_seek(s->pb, vst->index_entries[0].pos, SEEK_SET) < 0) + if (avio_seek(s->pb, vst->index_entries[0].pos + bink->smush_size, SEEK_SET) < 0) return -1; bink->video_pts = 0; @@ -288,4 +322,5 @@ AVInputFormat ff_bink_demuxer = { .read_header = read_header, .read_packet = read_packet, .read_seek = read_seek, + .flags = AVFMT_SHOW_IDS, }; |