00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00031 #include "libavutil/intreadwrite.h"
00032 #include "avformat.h"
00033 #include "internal.h"
00034
00035 enum BinkAudFlags {
00036 BINK_AUD_16BITS = 0x4000,
00037 BINK_AUD_STEREO = 0x2000,
00038 BINK_AUD_USEDCT = 0x1000,
00039 };
00040
00041 #define BINK_EXTRADATA_SIZE 1
00042 #define BINK_MAX_AUDIO_TRACKS 256
00043 #define BINK_MAX_WIDTH 7680
00044 #define BINK_MAX_HEIGHT 4800
00045
00046 typedef struct {
00047 uint32_t file_size;
00048
00049 uint32_t num_audio_tracks;
00050 int current_track;
00051 int64_t video_pts;
00052 int64_t audio_pts[BINK_MAX_AUDIO_TRACKS];
00053
00054 uint32_t remain_packet_size;
00055 } BinkDemuxContext;
00056
00057 static int probe(AVProbeData *p)
00058 {
00059 const uint8_t *b = p->buf;
00060
00061 if ( b[0] == 'B' && b[1] == 'I' && b[2] == 'K' &&
00062 (b[3] == 'b' || b[3] == 'f' || b[3] == 'g' || b[3] == 'h' || b[3] == 'i') &&
00063 AV_RL32(b+8) > 0 &&
00064 AV_RL32(b+20) > 0 && AV_RL32(b+20) <= BINK_MAX_WIDTH &&
00065 AV_RL32(b+24) > 0 && AV_RL32(b+24) <= BINK_MAX_HEIGHT &&
00066 AV_RL32(b+28) > 0 && AV_RL32(b+32) > 0)
00067 return AVPROBE_SCORE_MAX;
00068 return 0;
00069 }
00070
00071 static int read_header(AVFormatContext *s)
00072 {
00073 BinkDemuxContext *bink = s->priv_data;
00074 AVIOContext *pb = s->pb;
00075 uint32_t fps_num, fps_den;
00076 AVStream *vst, *ast;
00077 unsigned int i;
00078 uint32_t pos, next_pos;
00079 uint16_t flags;
00080 int keyframe;
00081
00082 vst = avformat_new_stream(s, NULL);
00083 if (!vst)
00084 return AVERROR(ENOMEM);
00085
00086 vst->codec->codec_tag = avio_rl32(pb);
00087
00088 bink->file_size = avio_rl32(pb) + 8;
00089 vst->duration = avio_rl32(pb);
00090
00091 if (vst->duration > 1000000) {
00092 av_log(s, AV_LOG_ERROR, "invalid header: more than 1000000 frames\n");
00093 return AVERROR(EIO);
00094 }
00095
00096 if (avio_rl32(pb) > bink->file_size) {
00097 av_log(s, AV_LOG_ERROR,
00098 "invalid header: largest frame size greater than file size\n");
00099 return AVERROR(EIO);
00100 }
00101
00102 avio_skip(pb, 4);
00103
00104 vst->codec->width = avio_rl32(pb);
00105 vst->codec->height = avio_rl32(pb);
00106
00107 fps_num = avio_rl32(pb);
00108 fps_den = avio_rl32(pb);
00109 if (fps_num == 0 || fps_den == 0) {
00110 av_log(s, AV_LOG_ERROR, "invalid header: invalid fps (%d/%d)\n", fps_num, fps_den);
00111 return AVERROR(EIO);
00112 }
00113 avpriv_set_pts_info(vst, 64, fps_den, fps_num);
00114
00115 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00116 vst->codec->codec_id = AV_CODEC_ID_BINKVIDEO;
00117 vst->codec->extradata = av_mallocz(4 + FF_INPUT_BUFFER_PADDING_SIZE);
00118 if (!vst->codec->extradata)
00119 return AVERROR(ENOMEM);
00120 vst->codec->extradata_size = 4;
00121 avio_read(pb, vst->codec->extradata, 4);
00122
00123 bink->num_audio_tracks = avio_rl32(pb);
00124
00125 if (bink->num_audio_tracks > BINK_MAX_AUDIO_TRACKS) {
00126 av_log(s, AV_LOG_ERROR,
00127 "invalid header: more than "AV_STRINGIFY(BINK_MAX_AUDIO_TRACKS)" audio tracks (%d)\n",
00128 bink->num_audio_tracks);
00129 return AVERROR(EIO);
00130 }
00131
00132 if (bink->num_audio_tracks) {
00133 avio_skip(pb, 4 * bink->num_audio_tracks);
00134
00135 for (i = 0; i < bink->num_audio_tracks; i++) {
00136 ast = avformat_new_stream(s, NULL);
00137 if (!ast)
00138 return AVERROR(ENOMEM);
00139 ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00140 ast->codec->codec_tag = 0;
00141 ast->codec->sample_rate = avio_rl16(pb);
00142 avpriv_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
00143 flags = avio_rl16(pb);
00144 ast->codec->codec_id = flags & BINK_AUD_USEDCT ?
00145 AV_CODEC_ID_BINKAUDIO_DCT : AV_CODEC_ID_BINKAUDIO_RDFT;
00146 ast->codec->channels = flags & BINK_AUD_STEREO ? 2 : 1;
00147 ast->codec->extradata = av_mallocz(4 + FF_INPUT_BUFFER_PADDING_SIZE);
00148 if (!ast->codec->extradata)
00149 return AVERROR(ENOMEM);
00150 ast->codec->extradata_size = 4;
00151 AV_WL32(ast->codec->extradata, vst->codec->codec_tag);
00152 }
00153
00154 for (i = 0; i < bink->num_audio_tracks; i++)
00155 s->streams[i + 1]->id = avio_rl32(pb);
00156 }
00157
00158
00159 next_pos = avio_rl32(pb);
00160 for (i = 0; i < vst->duration; i++) {
00161 pos = next_pos;
00162 if (i == vst->duration - 1) {
00163 next_pos = bink->file_size;
00164 keyframe = 0;
00165 } else {
00166 next_pos = avio_rl32(pb);
00167 keyframe = pos & 1;
00168 }
00169 pos &= ~1;
00170 next_pos &= ~1;
00171
00172 if (next_pos <= pos) {
00173 av_log(s, AV_LOG_ERROR, "invalid frame index table\n");
00174 return AVERROR(EIO);
00175 }
00176 av_add_index_entry(vst, pos, i, next_pos - pos, 0,
00177 keyframe ? AVINDEX_KEYFRAME : 0);
00178 }
00179
00180 avio_skip(pb, 4);
00181
00182 bink->current_track = -1;
00183 return 0;
00184 }
00185
00186 static int read_packet(AVFormatContext *s, AVPacket *pkt)
00187 {
00188 BinkDemuxContext *bink = s->priv_data;
00189 AVIOContext *pb = s->pb;
00190 int ret;
00191
00192 if (bink->current_track < 0) {
00193 int index_entry;
00194 AVStream *st = s->streams[0];
00195
00196 if (bink->video_pts >= st->duration)
00197 return AVERROR(EIO);
00198
00199 index_entry = av_index_search_timestamp(st, bink->video_pts,
00200 AVSEEK_FLAG_ANY);
00201 if (index_entry < 0) {
00202 av_log(s, AV_LOG_ERROR,
00203 "could not find index entry for frame %"PRId64"\n",
00204 bink->video_pts);
00205 return AVERROR(EIO);
00206 }
00207
00208 bink->remain_packet_size = st->index_entries[index_entry].size;
00209 bink->current_track = 0;
00210 }
00211
00212 while (bink->current_track < bink->num_audio_tracks) {
00213 uint32_t audio_size = avio_rl32(pb);
00214 if (audio_size > bink->remain_packet_size - 4) {
00215 av_log(s, AV_LOG_ERROR,
00216 "frame %"PRId64": audio size in header (%u) > size of packet left (%u)\n",
00217 bink->video_pts, audio_size, bink->remain_packet_size);
00218 return AVERROR(EIO);
00219 }
00220 bink->remain_packet_size -= 4 + audio_size;
00221 bink->current_track++;
00222 if (audio_size >= 4) {
00223
00224 if ((ret = av_get_packet(pb, pkt, audio_size)) < 0)
00225 return ret;
00226 pkt->stream_index = bink->current_track;
00227 pkt->pts = bink->audio_pts[bink->current_track - 1];
00228
00229
00230
00231 if (pkt->size >= 4)
00232 bink->audio_pts[bink->current_track -1] +=
00233 AV_RL32(pkt->data) / (2 * s->streams[bink->current_track]->codec->channels);
00234 return 0;
00235 } else {
00236 avio_skip(pb, audio_size);
00237 }
00238 }
00239
00240
00241 if ((ret = av_get_packet(pb, pkt, bink->remain_packet_size)) < 0)
00242 return ret;
00243 pkt->stream_index = 0;
00244 pkt->pts = bink->video_pts++;
00245 pkt->flags |= AV_PKT_FLAG_KEY;
00246
00247
00248 bink->current_track = -1;
00249
00250 return 0;
00251 }
00252
00253 static int read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
00254 {
00255 BinkDemuxContext *bink = s->priv_data;
00256 AVStream *vst = s->streams[0];
00257
00258 if (!s->pb->seekable)
00259 return -1;
00260
00261
00262 if (avio_seek(s->pb, vst->index_entries[0].pos, SEEK_SET) < 0)
00263 return -1;
00264
00265 bink->video_pts = 0;
00266 memset(bink->audio_pts, 0, sizeof(bink->audio_pts));
00267 bink->current_track = -1;
00268 return 0;
00269 }
00270
00271 AVInputFormat ff_bink_demuxer = {
00272 .name = "bink",
00273 .long_name = NULL_IF_CONFIG_SMALL("Bink"),
00274 .priv_data_size = sizeof(BinkDemuxContext),
00275 .read_probe = probe,
00276 .read_header = read_header,
00277 .read_packet = read_packet,
00278 .read_seek = read_seek,
00279 };