123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- #include "libavutil/intreadwrite.h"
- #include "avformat.h"
- #include "internal.h"
- typedef struct BFIContext {
- int nframes;
- int audio_frame;
- int video_frame;
- int video_size;
- int avflag;
- } BFIContext;
- static int bfi_probe(AVProbeData * p)
- {
-
- if (AV_RL32(p->buf) == MKTAG('B', 'F', '&', 'I'))
- return AVPROBE_SCORE_MAX;
- else
- return 0;
- }
- static int bfi_read_header(AVFormatContext * s)
- {
- BFIContext *bfi = s->priv_data;
- AVIOContext *pb = s->pb;
- AVStream *vstream;
- AVStream *astream;
- int fps, chunk_header;
-
- vstream = avformat_new_stream(s, NULL);
- if (!vstream)
- return AVERROR(ENOMEM);
-
- astream = avformat_new_stream(s, NULL);
- if (!astream)
- return AVERROR(ENOMEM);
-
- avio_skip(pb, 8);
- chunk_header = avio_rl32(pb);
- bfi->nframes = avio_rl32(pb);
- avio_rl32(pb);
- avio_rl32(pb);
- avio_rl32(pb);
- fps = avio_rl32(pb);
- avio_skip(pb, 12);
- vstream->codec->width = avio_rl32(pb);
- vstream->codec->height = avio_rl32(pb);
-
- avio_skip(pb, 8);
- vstream->codec->extradata = av_malloc(768);
- vstream->codec->extradata_size = 768;
- avio_read(pb, vstream->codec->extradata,
- vstream->codec->extradata_size);
- astream->codec->sample_rate = avio_rl32(pb);
-
- avpriv_set_pts_info(vstream, 32, 1, fps);
- vstream->codec->codec_type = AVMEDIA_TYPE_VIDEO;
- vstream->codec->codec_id = CODEC_ID_BFI;
- vstream->codec->pix_fmt = PIX_FMT_PAL8;
-
- astream->codec->codec_type = AVMEDIA_TYPE_AUDIO;
- astream->codec->codec_id = CODEC_ID_PCM_U8;
- astream->codec->channels = 1;
- astream->codec->bits_per_coded_sample = 8;
- astream->codec->bit_rate =
- astream->codec->sample_rate * astream->codec->bits_per_coded_sample;
- avio_seek(pb, chunk_header - 3, SEEK_SET);
- avpriv_set_pts_info(astream, 64, 1, astream->codec->sample_rate);
- return 0;
- }
- static int bfi_read_packet(AVFormatContext * s, AVPacket * pkt)
- {
- BFIContext *bfi = s->priv_data;
- AVIOContext *pb = s->pb;
- int ret, audio_offset, video_offset, chunk_size, audio_size = 0;
- if (bfi->nframes == 0 || pb->eof_reached) {
- return AVERROR(EIO);
- }
-
- if (!bfi->avflag) {
- uint32_t state = 0;
- while(state != MKTAG('S','A','V','I')){
- if (pb->eof_reached)
- return AVERROR(EIO);
- state = 256*state + avio_r8(pb);
- }
-
- chunk_size = avio_rl32(pb);
- avio_rl32(pb);
- audio_offset = avio_rl32(pb);
- avio_rl32(pb);
- video_offset = avio_rl32(pb);
- audio_size = video_offset - audio_offset;
- bfi->video_size = chunk_size - video_offset;
-
- ret = av_get_packet(pb, pkt, audio_size);
- if (ret < 0)
- return ret;
- pkt->pts = bfi->audio_frame;
- bfi->audio_frame += ret;
- }
- else {
-
- ret = av_get_packet(pb, pkt, bfi->video_size);
- if (ret < 0)
- return ret;
- pkt->pts = bfi->video_frame;
- bfi->video_frame += ret / bfi->video_size;
-
- bfi->nframes--;
- }
- bfi->avflag = !bfi->avflag;
- pkt->stream_index = bfi->avflag;
- return ret;
- }
- AVInputFormat ff_bfi_demuxer = {
- .name = "bfi",
- .long_name = NULL_IF_CONFIG_SMALL("Brute Force & Ignorance"),
- .priv_data_size = sizeof(BFIContext),
- .read_probe = bfi_probe,
- .read_header = bfi_read_header,
- .read_packet = bfi_read_packet,
- };
|