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