bfi.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. * @see http://wiki.multimedia.cx/index.php?title=BFI
  26. */
  27. #include "libavutil/intreadwrite.h"
  28. #include "avformat.h"
  29. #include "internal.h"
  30. typedef struct BFIContext {
  31. int nframes;
  32. int audio_frame;
  33. int video_frame;
  34. int video_size;
  35. int avflag;
  36. } BFIContext;
  37. static int bfi_probe(AVProbeData * p)
  38. {
  39. /* Check file header */
  40. if (AV_RL32(p->buf) == MKTAG('B', 'F', '&', 'I'))
  41. return AVPROBE_SCORE_MAX;
  42. else
  43. return 0;
  44. }
  45. static int bfi_read_header(AVFormatContext * s)
  46. {
  47. BFIContext *bfi = s->priv_data;
  48. AVIOContext *pb = s->pb;
  49. AVStream *vstream;
  50. AVStream *astream;
  51. int fps, chunk_header;
  52. /* Initialize the video codec... */
  53. vstream = avformat_new_stream(s, NULL);
  54. if (!vstream)
  55. return AVERROR(ENOMEM);
  56. /* Initialize the audio codec... */
  57. astream = avformat_new_stream(s, NULL);
  58. if (!astream)
  59. return AVERROR(ENOMEM);
  60. /* Set the total number of frames. */
  61. avio_skip(pb, 8);
  62. chunk_header = avio_rl32(pb);
  63. bfi->nframes = avio_rl32(pb);
  64. avio_rl32(pb);
  65. avio_rl32(pb);
  66. avio_rl32(pb);
  67. fps = avio_rl32(pb);
  68. avio_skip(pb, 12);
  69. vstream->codec->width = avio_rl32(pb);
  70. vstream->codec->height = avio_rl32(pb);
  71. /*Load the palette to extradata */
  72. avio_skip(pb, 8);
  73. vstream->codec->extradata = av_malloc(768);
  74. vstream->codec->extradata_size = 768;
  75. avio_read(pb, vstream->codec->extradata,
  76. vstream->codec->extradata_size);
  77. astream->codec->sample_rate = avio_rl32(pb);
  78. /* Set up the video codec... */
  79. avpriv_set_pts_info(vstream, 32, 1, fps);
  80. vstream->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  81. vstream->codec->codec_id = AV_CODEC_ID_BFI;
  82. vstream->codec->pix_fmt = PIX_FMT_PAL8;
  83. /* Set up the audio codec now... */
  84. astream->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  85. astream->codec->codec_id = AV_CODEC_ID_PCM_U8;
  86. astream->codec->channels = 1;
  87. astream->codec->bits_per_coded_sample = 8;
  88. astream->codec->bit_rate =
  89. astream->codec->sample_rate * astream->codec->bits_per_coded_sample;
  90. avio_seek(pb, chunk_header - 3, SEEK_SET);
  91. avpriv_set_pts_info(astream, 64, 1, astream->codec->sample_rate);
  92. return 0;
  93. }
  94. static int bfi_read_packet(AVFormatContext * s, AVPacket * pkt)
  95. {
  96. BFIContext *bfi = s->priv_data;
  97. AVIOContext *pb = s->pb;
  98. int ret, audio_offset, video_offset, chunk_size, audio_size = 0;
  99. if (bfi->nframes == 0 || url_feof(pb)) {
  100. return AVERROR(EIO);
  101. }
  102. /* If all previous chunks were completely read, then find a new one... */
  103. if (!bfi->avflag) {
  104. uint32_t state = 0;
  105. while(state != MKTAG('S','A','V','I')){
  106. if (url_feof(pb))
  107. return AVERROR(EIO);
  108. state = 256*state + avio_r8(pb);
  109. }
  110. /* Now that the chunk's location is confirmed, we proceed... */
  111. chunk_size = avio_rl32(pb);
  112. avio_rl32(pb);
  113. audio_offset = avio_rl32(pb);
  114. avio_rl32(pb);
  115. video_offset = avio_rl32(pb);
  116. audio_size = video_offset - audio_offset;
  117. bfi->video_size = chunk_size - video_offset;
  118. //Tossing an audio packet at the audio decoder.
  119. ret = av_get_packet(pb, pkt, audio_size);
  120. if (ret < 0)
  121. return ret;
  122. pkt->pts = bfi->audio_frame;
  123. bfi->audio_frame += ret;
  124. }
  125. else {
  126. //Tossing a video packet at the video decoder.
  127. ret = av_get_packet(pb, pkt, bfi->video_size);
  128. if (ret < 0)
  129. return ret;
  130. pkt->pts = bfi->video_frame;
  131. bfi->video_frame += ret / bfi->video_size;
  132. /* One less frame to read. A cursory decrement. */
  133. bfi->nframes--;
  134. }
  135. bfi->avflag = !bfi->avflag;
  136. pkt->stream_index = bfi->avflag;
  137. return ret;
  138. }
  139. AVInputFormat ff_bfi_demuxer = {
  140. .name = "bfi",
  141. .long_name = NULL_IF_CONFIG_SMALL("Brute Force & Ignorance"),
  142. .priv_data_size = sizeof(BFIContext),
  143. .read_probe = bfi_probe,
  144. .read_header = bfi_read_header,
  145. .read_packet = bfi_read_packet,
  146. };