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.

185 lines
5.8KB

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