bethsoftvid.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Bethsoft VID format Demuxer
  3. * Copyright (c) 2007 Nicholas Tung
  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 Bethesda Softworks VID (.vid) file demuxer
  24. * @author Nicholas Tung [ntung (at. ntung com] (2007-03)
  25. * @see http://wiki.multimedia.cx/index.php?title=Bethsoft_VID
  26. * @see http://www.svatopluk.com/andux/docs/dfvid.html
  27. */
  28. #include "libavutil/intreadwrite.h"
  29. #include "avformat.h"
  30. #include "libavcodec/bethsoftvideo.h"
  31. typedef struct BVID_DemuxContext
  32. {
  33. int nframes;
  34. /** delay value between frames, added to individual frame delay.
  35. * custom units, which will be added to other custom units (~=16ms according
  36. * to free, unofficial documentation) */
  37. int bethsoft_global_delay;
  38. /** video presentation time stamp.
  39. * delay = 16 milliseconds * (global_delay + per_frame_delay) */
  40. int video_pts;
  41. int is_finished;
  42. } BVID_DemuxContext;
  43. static int vid_probe(AVProbeData *p)
  44. {
  45. // little endian VID tag, file starts with "VID\0"
  46. if (AV_RL32(p->buf) != MKTAG('V', 'I', 'D', 0))
  47. return 0;
  48. return AVPROBE_SCORE_MAX;
  49. }
  50. static int vid_read_header(AVFormatContext *s,
  51. AVFormatParameters *ap)
  52. {
  53. BVID_DemuxContext *vid = s->priv_data;
  54. AVIOContext *pb = s->pb;
  55. AVStream *stream;
  56. /* load main header. Contents:
  57. * bytes: 'V' 'I' 'D'
  58. * int16s: always_512, nframes, width, height, delay, always_14
  59. */
  60. avio_skip(pb, 5);
  61. vid->nframes = avio_rl16(pb);
  62. stream = av_new_stream(s, 0);
  63. if (!stream)
  64. return AVERROR(ENOMEM);
  65. av_set_pts_info(stream, 32, 1, 60); // 16 ms increments, i.e. 60 fps
  66. stream->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  67. stream->codec->codec_id = CODEC_ID_BETHSOFTVID;
  68. stream->codec->width = avio_rl16(pb);
  69. stream->codec->height = avio_rl16(pb);
  70. stream->codec->pix_fmt = PIX_FMT_PAL8;
  71. vid->bethsoft_global_delay = avio_rl16(pb);
  72. avio_rl16(pb);
  73. // done with video codec, set up audio codec
  74. stream = av_new_stream(s, 0);
  75. if (!stream)
  76. return AVERROR(ENOMEM);
  77. stream->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  78. stream->codec->codec_id = CODEC_ID_PCM_U8;
  79. stream->codec->channels = 1;
  80. stream->codec->sample_rate = 11025;
  81. stream->codec->bits_per_coded_sample = 8;
  82. stream->codec->bit_rate = stream->codec->channels * stream->codec->sample_rate * stream->codec->bits_per_coded_sample;
  83. return 0;
  84. }
  85. #define BUFFER_PADDING_SIZE 1000
  86. static int read_frame(BVID_DemuxContext *vid, AVIOContext *pb, AVPacket *pkt,
  87. uint8_t block_type, AVFormatContext *s, int npixels)
  88. {
  89. uint8_t * vidbuf_start = NULL;
  90. int vidbuf_nbytes = 0;
  91. int code;
  92. int bytes_copied = 0;
  93. int position;
  94. unsigned int vidbuf_capacity;
  95. vidbuf_start = av_malloc(vidbuf_capacity = BUFFER_PADDING_SIZE);
  96. if(!vidbuf_start)
  97. return AVERROR(ENOMEM);
  98. // save the file position for the packet, include block type
  99. position = avio_tell(pb) - 1;
  100. vidbuf_start[vidbuf_nbytes++] = block_type;
  101. // get the video delay (next int16), and set the presentation time
  102. vid->video_pts += vid->bethsoft_global_delay + avio_rl16(pb);
  103. // set the y offset if it exists (decoder header data should be in data section)
  104. if(block_type == VIDEO_YOFF_P_FRAME){
  105. if(avio_read(pb, &vidbuf_start[vidbuf_nbytes], 2) != 2)
  106. goto fail;
  107. vidbuf_nbytes += 2;
  108. }
  109. do{
  110. vidbuf_start = av_fast_realloc(vidbuf_start, &vidbuf_capacity, vidbuf_nbytes + BUFFER_PADDING_SIZE);
  111. if(!vidbuf_start)
  112. return AVERROR(ENOMEM);
  113. code = avio_r8(pb);
  114. vidbuf_start[vidbuf_nbytes++] = code;
  115. if(code >= 0x80){ // rle sequence
  116. if(block_type == VIDEO_I_FRAME)
  117. vidbuf_start[vidbuf_nbytes++] = avio_r8(pb);
  118. } else if(code){ // plain sequence
  119. if(avio_read(pb, &vidbuf_start[vidbuf_nbytes], code) != code)
  120. goto fail;
  121. vidbuf_nbytes += code;
  122. }
  123. bytes_copied += code & 0x7F;
  124. if(bytes_copied == npixels){ // sometimes no stop character is given, need to keep track of bytes copied
  125. // may contain a 0 byte even if read all pixels
  126. if(avio_r8(pb))
  127. avio_seek(pb, -1, SEEK_CUR);
  128. break;
  129. }
  130. if(bytes_copied > npixels)
  131. goto fail;
  132. } while(code);
  133. // copy data into packet
  134. if(av_new_packet(pkt, vidbuf_nbytes) < 0)
  135. goto fail;
  136. memcpy(pkt->data, vidbuf_start, vidbuf_nbytes);
  137. av_free(vidbuf_start);
  138. pkt->pos = position;
  139. pkt->stream_index = 0; // use the video decoder, which was initialized as the first stream
  140. pkt->pts = vid->video_pts;
  141. vid->nframes--; // used to check if all the frames were read
  142. return vidbuf_nbytes;
  143. fail:
  144. av_free(vidbuf_start);
  145. return -1;
  146. }
  147. static int vid_read_packet(AVFormatContext *s,
  148. AVPacket *pkt)
  149. {
  150. BVID_DemuxContext *vid = s->priv_data;
  151. AVIOContext *pb = s->pb;
  152. unsigned char block_type;
  153. int audio_length;
  154. int ret_value;
  155. if(vid->is_finished || url_feof(pb))
  156. return AVERROR(EIO);
  157. block_type = avio_r8(pb);
  158. switch(block_type){
  159. case PALETTE_BLOCK:
  160. avio_seek(pb, -1, SEEK_CUR); // include block type
  161. ret_value = av_get_packet(pb, pkt, 3 * 256 + 1);
  162. if(ret_value != 3 * 256 + 1){
  163. av_free_packet(pkt);
  164. return AVERROR(EIO);
  165. }
  166. pkt->stream_index = 0;
  167. return ret_value;
  168. case FIRST_AUDIO_BLOCK:
  169. avio_rl16(pb);
  170. // soundblaster DAC used for sample rate, as on specification page (link above)
  171. s->streams[1]->codec->sample_rate = 1000000 / (256 - avio_r8(pb));
  172. s->streams[1]->codec->bit_rate = s->streams[1]->codec->channels * s->streams[1]->codec->sample_rate * s->streams[1]->codec->bits_per_coded_sample;
  173. case AUDIO_BLOCK:
  174. audio_length = avio_rl16(pb);
  175. ret_value = av_get_packet(pb, pkt, audio_length);
  176. pkt->stream_index = 1;
  177. return ret_value != audio_length ? AVERROR(EIO) : ret_value;
  178. case VIDEO_P_FRAME:
  179. case VIDEO_YOFF_P_FRAME:
  180. case VIDEO_I_FRAME:
  181. return read_frame(vid, pb, pkt, block_type, s,
  182. s->streams[0]->codec->width * s->streams[0]->codec->height);
  183. case EOF_BLOCK:
  184. if(vid->nframes != 0)
  185. av_log(s, AV_LOG_VERBOSE, "reached terminating character but not all frames read.\n");
  186. vid->is_finished = 1;
  187. return AVERROR(EIO);
  188. default:
  189. av_log(s, AV_LOG_ERROR, "unknown block (character = %c, decimal = %d, hex = %x)!!!\n",
  190. block_type, block_type, block_type); return -1;
  191. }
  192. }
  193. AVInputFormat ff_bethsoftvid_demuxer = {
  194. .name = "bethsoftvid",
  195. .long_name = NULL_IF_CONFIG_SMALL("Bethesda Softworks VID format"),
  196. .priv_data_size = sizeof(BVID_DemuxContext),
  197. .read_probe = vid_probe,
  198. .read_header = vid_read_header,
  199. .read_packet = vid_read_packet,
  200. };