You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
4.9KB

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