bfi.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Brute Force & Ignorance (BFI) demuxer
  3. * Copyright (c) 2008 Sisir Koppaka
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * @brief Brute Force & Ignorance (.bfi) file demuxer
  24. * @author Sisir Koppaka ( sisir.koppaka at gmail dot com )
  25. * @sa http://wiki.multimedia.cx/index.php?title=BFI
  26. */
  27. #include "libavutil/intreadwrite.h"
  28. #include "avformat.h"
  29. typedef struct BFIContext {
  30. int nframes;
  31. int audio_frame;
  32. int video_frame;
  33. int video_size;
  34. int avflag;
  35. } BFIContext;
  36. static int bfi_probe(AVProbeData * p)
  37. {
  38. /* Check file header */
  39. if (AV_RL32(p->buf) == MKTAG('B', 'F', '&', 'I'))
  40. return AVPROBE_SCORE_MAX;
  41. else
  42. return 0;
  43. }
  44. static int bfi_read_header(AVFormatContext * s, AVFormatParameters * ap)
  45. {
  46. BFIContext *bfi = s->priv_data;
  47. AVIOContext *pb = s->pb;
  48. AVStream *vstream;
  49. AVStream *astream;
  50. int fps, chunk_header;
  51. /* Initialize the video codec... */
  52. vstream = av_new_stream(s, 0);
  53. if (!vstream)
  54. return AVERROR(ENOMEM);
  55. /* Initialize the audio codec... */
  56. astream = av_new_stream(s, 0);
  57. if (!astream)
  58. return AVERROR(ENOMEM);
  59. /* Set the total number of frames. */
  60. avio_skip(pb, 8);
  61. chunk_header = avio_rl32(pb);
  62. bfi->nframes = avio_rl32(pb);
  63. avio_rl32(pb);
  64. avio_rl32(pb);
  65. avio_rl32(pb);
  66. fps = avio_rl32(pb);
  67. avio_skip(pb, 12);
  68. vstream->codec->width = avio_rl32(pb);
  69. vstream->codec->height = avio_rl32(pb);
  70. /*Load the palette to extradata */
  71. avio_skip(pb, 8);
  72. vstream->codec->extradata = av_malloc(768);
  73. vstream->codec->extradata_size = 768;
  74. avio_read(pb, vstream->codec->extradata,
  75. vstream->codec->extradata_size);
  76. astream->codec->sample_rate = avio_rl32(pb);
  77. /* Set up the video codec... */
  78. av_set_pts_info(vstream, 32, 1, fps);
  79. vstream->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  80. vstream->codec->codec_id = CODEC_ID_BFI;
  81. vstream->codec->pix_fmt = PIX_FMT_PAL8;
  82. /* Set up the audio codec now... */
  83. astream->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  84. astream->codec->codec_id = CODEC_ID_PCM_U8;
  85. astream->codec->channels = 1;
  86. astream->codec->bits_per_coded_sample = 8;
  87. astream->codec->bit_rate =
  88. astream->codec->sample_rate * astream->codec->bits_per_coded_sample;
  89. avio_seek(pb, chunk_header - 3, SEEK_SET);
  90. av_set_pts_info(astream, 64, 1, astream->codec->sample_rate);
  91. return 0;
  92. }
  93. static int bfi_read_packet(AVFormatContext * s, AVPacket * pkt)
  94. {
  95. BFIContext *bfi = s->priv_data;
  96. AVIOContext *pb = s->pb;
  97. int ret, audio_offset, video_offset, chunk_size, audio_size = 0;
  98. if (bfi->nframes == 0 || url_feof(pb)) {
  99. return AVERROR(EIO);
  100. }
  101. /* If all previous chunks were completely read, then find a new one... */
  102. if (!bfi->avflag) {
  103. uint32_t state = 0;
  104. while(state != MKTAG('S','A','V','I')){
  105. if (url_feof(pb))
  106. return AVERROR(EIO);
  107. state = 256*state + avio_r8(pb);
  108. }
  109. /* Now that the chunk's location is confirmed, we proceed... */
  110. chunk_size = avio_rl32(pb);
  111. avio_rl32(pb);
  112. audio_offset = avio_rl32(pb);
  113. avio_rl32(pb);
  114. video_offset = avio_rl32(pb);
  115. audio_size = video_offset - audio_offset;
  116. bfi->video_size = chunk_size - video_offset;
  117. //Tossing an audio packet at the audio decoder.
  118. ret = av_get_packet(pb, pkt, audio_size);
  119. if (ret < 0)
  120. return ret;
  121. pkt->pts = bfi->audio_frame;
  122. bfi->audio_frame += ret;
  123. }
  124. else {
  125. //Tossing a video packet at the video decoder.
  126. ret = av_get_packet(pb, pkt, bfi->video_size);
  127. if (ret < 0)
  128. return ret;
  129. pkt->pts = bfi->video_frame;
  130. bfi->video_frame += ret / bfi->video_size;
  131. /* One less frame to read. A cursory decrement. */
  132. bfi->nframes--;
  133. }
  134. bfi->avflag = !bfi->avflag;
  135. pkt->stream_index = bfi->avflag;
  136. return ret;
  137. }
  138. AVInputFormat ff_bfi_demuxer = {
  139. "bfi",
  140. NULL_IF_CONFIG_SMALL("Brute Force & Ignorance"),
  141. sizeof(BFIContext),
  142. bfi_probe,
  143. bfi_read_header,
  144. bfi_read_packet,
  145. };