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, AVFormatParameters *ap)
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 = CODEC_ID_BINKVIDEO;
00117 vst->codec->extradata = av_mallocz(4 + FF_INPUT_BUFFER_PADDING_SIZE);
00118 vst->codec->extradata_size = 4;
00119 avio_read(pb, vst->codec->extradata, 4);
00120
00121 bink->num_audio_tracks = avio_rl32(pb);
00122
00123 if (bink->num_audio_tracks > BINK_MAX_AUDIO_TRACKS) {
00124 av_log(s, AV_LOG_ERROR,
00125 "invalid header: more than "AV_STRINGIFY(BINK_MAX_AUDIO_TRACKS)" audio tracks (%d)\n",
00126 bink->num_audio_tracks);
00127 return AVERROR(EIO);
00128 }
00129
00130 if (bink->num_audio_tracks) {
00131 avio_skip(pb, 4 * bink->num_audio_tracks);
00132
00133 for (i = 0; i < bink->num_audio_tracks; i++) {
00134 ast = avformat_new_stream(s, NULL);
00135 if (!ast)
00136 return AVERROR(ENOMEM);
00137 ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00138 ast->codec->codec_tag = 0;
00139 ast->codec->sample_rate = avio_rl16(pb);
00140 avpriv_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
00141 flags = avio_rl16(pb);
00142 ast->codec->codec_id = flags & BINK_AUD_USEDCT ?
00143 CODEC_ID_BINKAUDIO_DCT : CODEC_ID_BINKAUDIO_RDFT;
00144 ast->codec->channels = flags & BINK_AUD_STEREO ? 2 : 1;
00145 ast->codec->extradata = av_mallocz(4 + FF_INPUT_BUFFER_PADDING_SIZE);
00146 if (!ast->codec->extradata)
00147 return AVERROR(ENOMEM);
00148 ast->codec->extradata_size = 4;
00149 AV_WL32(ast->codec->extradata, vst->codec->codec_tag);
00150 }
00151
00152 for (i = 0; i < bink->num_audio_tracks; i++)
00153 s->streams[i + 1]->id = avio_rl32(pb);
00154 }
00155
00156
00157 next_pos = avio_rl32(pb);
00158 for (i = 0; i < vst->duration; i++) {
00159 pos = next_pos;
00160 if (i == vst->duration - 1) {
00161 next_pos = bink->file_size;
00162 keyframe = 0;
00163 } else {
00164 next_pos = avio_rl32(pb);
00165 keyframe = pos & 1;
00166 }
00167 pos &= ~1;
00168 next_pos &= ~1;
00169
00170 if (next_pos <= pos) {
00171 av_log(s, AV_LOG_ERROR, "invalid frame index table\n");
00172 return AVERROR(EIO);
00173 }
00174 av_add_index_entry(vst, pos, i, next_pos - pos, 0,
00175 keyframe ? AVINDEX_KEYFRAME : 0);
00176 }
00177
00178 avio_skip(pb, 4);
00179
00180 bink->current_track = -1;
00181 return 0;
00182 }
00183
00184 static int read_packet(AVFormatContext *s, AVPacket *pkt)
00185 {
00186 BinkDemuxContext *bink = s->priv_data;
00187 AVIOContext *pb = s->pb;
00188 int ret;
00189
00190 if (bink->current_track < 0) {
00191 int index_entry;
00192 AVStream *st = s->streams[0];
00193
00194 if (bink->video_pts >= st->duration)
00195 return AVERROR(EIO);
00196
00197 index_entry = av_index_search_timestamp(st, bink->video_pts,
00198 AVSEEK_FLAG_ANY);
00199 if (index_entry < 0) {
00200 av_log(s, AV_LOG_ERROR,
00201 "could not find index entry for frame %"PRId64"\n",
00202 bink->video_pts);
00203 return AVERROR(EIO);
00204 }
00205
00206 bink->remain_packet_size = st->index_entries[index_entry].size;
00207 bink->current_track = 0;
00208 }
00209
00210 while (bink->current_track < bink->num_audio_tracks) {
00211 uint32_t audio_size = avio_rl32(pb);
00212 if (audio_size > bink->remain_packet_size - 4) {
00213 av_log(s, AV_LOG_ERROR,
00214 "frame %"PRId64": audio size in header (%u) > size of packet left (%u)\n",
00215 bink->video_pts, audio_size, bink->remain_packet_size);
00216 return AVERROR(EIO);
00217 }
00218 bink->remain_packet_size -= 4 + audio_size;
00219 bink->current_track++;
00220 if (audio_size >= 4) {
00221
00222 if ((ret = av_get_packet(pb, pkt, audio_size)) < 0)
00223 return ret;
00224 pkt->stream_index = bink->current_track;
00225 pkt->pts = bink->audio_pts[bink->current_track - 1];
00226
00227
00228
00229 if (pkt->size >= 4)
00230 bink->audio_pts[bink->current_track -1] +=
00231 AV_RL32(pkt->data) / (2 * s->streams[bink->current_track]->codec->channels);
00232 return 0;
00233 } else {
00234 avio_skip(pb, audio_size);
00235 }
00236 }
00237
00238
00239 if ((ret = av_get_packet(pb, pkt, bink->remain_packet_size)) < 0)
00240 return ret;
00241 pkt->stream_index = 0;
00242 pkt->pts = bink->video_pts++;
00243 pkt->flags |= AV_PKT_FLAG_KEY;
00244
00245
00246 bink->current_track = -1;
00247
00248 return 0;
00249 }
00250
00251 static int read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
00252 {
00253 BinkDemuxContext *bink = s->priv_data;
00254 AVStream *vst = s->streams[0];
00255
00256 if (!s->pb->seekable)
00257 return -1;
00258
00259
00260 if (avio_seek(s->pb, vst->index_entries[0].pos, SEEK_SET) < 0)
00261 return -1;
00262
00263 bink->video_pts = 0;
00264 memset(bink->audio_pts, 0, sizeof(bink->audio_pts));
00265 bink->current_track = -1;
00266 return 0;
00267 }
00268
00269 AVInputFormat ff_bink_demuxer = {
00270 .name = "bink",
00271 .long_name = NULL_IF_CONFIG_SMALL("Bink"),
00272 .priv_data_size = sizeof(BinkDemuxContext),
00273 .read_probe = probe,
00274 .read_header = read_header,
00275 .read_packet = read_packet,
00276 .read_seek = read_seek,
00277 };