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.

270 lines
8.8KB

  1. /*
  2. * Bethsoft VID format Demuxer
  3. * Copyright (c) 2007 Nicholas Tung
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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 "internal.h"
  31. #include "libavcodec/bethsoftvideo.h"
  32. #define BVID_PALETTE_SIZE 3 * 256
  33. typedef struct BVID_DemuxContext
  34. {
  35. int nframes;
  36. /** delay value between frames, added to individual frame delay.
  37. * custom units, which will be added to other custom units (~=16ms according
  38. * to free, unofficial documentation) */
  39. int bethsoft_global_delay;
  40. uint8_t *palette;
  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. {
  52. BVID_DemuxContext *vid = s->priv_data;
  53. AVIOContext *pb = s->pb;
  54. AVStream *stream;
  55. /* load main header. Contents:
  56. * bytes: 'V' 'I' 'D'
  57. * int16s: always_512, nframes, width, height, delay, always_14
  58. */
  59. avio_skip(pb, 5);
  60. vid->nframes = avio_rl16(pb);
  61. stream = avformat_new_stream(s, NULL);
  62. if (!stream)
  63. return AVERROR(ENOMEM);
  64. stream->start_time = 0;
  65. avpriv_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 = avformat_new_stream(s, NULL);
  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, duration;
  94. unsigned int vidbuf_capacity;
  95. int ret = 0;
  96. vidbuf_start = av_malloc(vidbuf_capacity = BUFFER_PADDING_SIZE);
  97. if(!vidbuf_start)
  98. return AVERROR(ENOMEM);
  99. // save the file position for the packet, include block type
  100. position = avio_tell(pb) - 1;
  101. vidbuf_start[vidbuf_nbytes++] = block_type;
  102. // get the current packet duration
  103. duration = vid->bethsoft_global_delay + avio_rl16(pb);
  104. // set the y offset if it exists (decoder header data should be in data section)
  105. if(block_type == VIDEO_YOFF_P_FRAME){
  106. if (avio_read(pb, &vidbuf_start[vidbuf_nbytes], 2) != 2) {
  107. ret = AVERROR(EIO);
  108. goto fail;
  109. }
  110. vidbuf_nbytes += 2;
  111. }
  112. do{
  113. vidbuf_start = av_fast_realloc(vidbuf_start, &vidbuf_capacity, vidbuf_nbytes + BUFFER_PADDING_SIZE);
  114. if(!vidbuf_start)
  115. return AVERROR(ENOMEM);
  116. code = avio_r8(pb);
  117. vidbuf_start[vidbuf_nbytes++] = code;
  118. if(code >= 0x80){ // rle sequence
  119. if(block_type == VIDEO_I_FRAME)
  120. vidbuf_start[vidbuf_nbytes++] = avio_r8(pb);
  121. } else if(code){ // plain sequence
  122. if (avio_read(pb, &vidbuf_start[vidbuf_nbytes], code) != code) {
  123. ret = AVERROR(EIO);
  124. goto fail;
  125. }
  126. vidbuf_nbytes += code;
  127. }
  128. bytes_copied += code & 0x7F;
  129. if(bytes_copied == npixels){ // sometimes no stop character is given, need to keep track of bytes copied
  130. // may contain a 0 byte even if read all pixels
  131. if(avio_r8(pb))
  132. avio_seek(pb, -1, SEEK_CUR);
  133. break;
  134. }
  135. if (bytes_copied > npixels) {
  136. ret = AVERROR_INVALIDDATA;
  137. goto fail;
  138. }
  139. } while(code);
  140. // copy data into packet
  141. if ((ret = av_new_packet(pkt, vidbuf_nbytes)) < 0)
  142. goto fail;
  143. memcpy(pkt->data, vidbuf_start, vidbuf_nbytes);
  144. av_free(vidbuf_start);
  145. pkt->pos = position;
  146. pkt->stream_index = 0; // use the video decoder, which was initialized as the first stream
  147. pkt->duration = duration;
  148. if (block_type == VIDEO_I_FRAME)
  149. pkt->flags |= AV_PKT_FLAG_KEY;
  150. /* if there is a new palette available, add it to packet side data */
  151. if (vid->palette) {
  152. uint8_t *pdata = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE,
  153. BVID_PALETTE_SIZE);
  154. memcpy(pdata, vid->palette, BVID_PALETTE_SIZE);
  155. av_freep(&vid->palette);
  156. }
  157. vid->nframes--; // used to check if all the frames were read
  158. return 0;
  159. fail:
  160. av_free(vidbuf_start);
  161. return ret;
  162. }
  163. static int vid_read_packet(AVFormatContext *s,
  164. AVPacket *pkt)
  165. {
  166. BVID_DemuxContext *vid = s->priv_data;
  167. AVIOContext *pb = s->pb;
  168. unsigned char block_type;
  169. int audio_length;
  170. int ret_value;
  171. if(vid->is_finished || pb->eof_reached)
  172. return AVERROR(EIO);
  173. block_type = avio_r8(pb);
  174. switch(block_type){
  175. case PALETTE_BLOCK:
  176. if (vid->palette) {
  177. av_log(s, AV_LOG_WARNING, "discarding unused palette\n");
  178. av_freep(&vid->palette);
  179. }
  180. vid->palette = av_malloc(BVID_PALETTE_SIZE);
  181. if (!vid->palette)
  182. return AVERROR(ENOMEM);
  183. if (avio_read(pb, vid->palette, BVID_PALETTE_SIZE) != BVID_PALETTE_SIZE) {
  184. av_freep(&vid->palette);
  185. return AVERROR(EIO);
  186. }
  187. return vid_read_packet(s, pkt);
  188. case FIRST_AUDIO_BLOCK:
  189. avio_rl16(pb);
  190. // soundblaster DAC used for sample rate, as on specification page (link above)
  191. s->streams[1]->codec->sample_rate = 1000000 / (256 - avio_r8(pb));
  192. 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;
  193. case AUDIO_BLOCK:
  194. audio_length = avio_rl16(pb);
  195. if ((ret_value = av_get_packet(pb, pkt, audio_length)) != audio_length) {
  196. if (ret_value < 0)
  197. return ret_value;
  198. av_log(s, AV_LOG_ERROR, "incomplete audio block\n");
  199. return AVERROR(EIO);
  200. }
  201. pkt->stream_index = 1;
  202. pkt->flags |= AV_PKT_FLAG_KEY;
  203. return 0;
  204. case VIDEO_P_FRAME:
  205. case VIDEO_YOFF_P_FRAME:
  206. case VIDEO_I_FRAME:
  207. return read_frame(vid, pb, pkt, block_type, s,
  208. s->streams[0]->codec->width * s->streams[0]->codec->height);
  209. case EOF_BLOCK:
  210. if(vid->nframes != 0)
  211. av_log(s, AV_LOG_VERBOSE, "reached terminating character but not all frames read.\n");
  212. vid->is_finished = 1;
  213. return AVERROR(EIO);
  214. default:
  215. av_log(s, AV_LOG_ERROR, "unknown block (character = %c, decimal = %d, hex = %x)!!!\n",
  216. block_type, block_type, block_type);
  217. return AVERROR_INVALIDDATA;
  218. }
  219. }
  220. static int vid_read_close(AVFormatContext *s)
  221. {
  222. BVID_DemuxContext *vid = s->priv_data;
  223. av_freep(&vid->palette);
  224. return 0;
  225. }
  226. AVInputFormat ff_bethsoftvid_demuxer = {
  227. .name = "bethsoftvid",
  228. .long_name = NULL_IF_CONFIG_SMALL("Bethesda Softworks VID format"),
  229. .priv_data_size = sizeof(BVID_DemuxContext),
  230. .read_probe = vid_probe,
  231. .read_header = vid_read_header,
  232. .read_packet = vid_read_packet,
  233. .read_close = vid_read_close,
  234. };