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