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.

300 lines
9.9KB

  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/channel_layout.h"
  29. #include "libavutil/intreadwrite.h"
  30. #include "avformat.h"
  31. #include "internal.h"
  32. #include "libavcodec/bethsoftvideo.h"
  33. #define BVID_PALETTE_SIZE 3 * 256
  34. #define DEFAULT_SAMPLE_RATE 11111
  35. typedef struct BVID_DemuxContext
  36. {
  37. int nframes;
  38. int sample_rate; /**< audio sample rate */
  39. int width; /**< video width */
  40. int height; /**< video height */
  41. /** delay value between frames, added to individual frame delay.
  42. * custom units, which will be added to other custom units (~=16ms according
  43. * to free, unofficial documentation) */
  44. int bethsoft_global_delay;
  45. int video_index; /**< video stream index */
  46. int audio_index; /**< audio stream index */
  47. uint8_t *palette;
  48. int is_finished;
  49. } BVID_DemuxContext;
  50. static int vid_probe(AVProbeData *p)
  51. {
  52. // little-endian VID tag, file starts with "VID\0"
  53. if (AV_RL32(p->buf) != MKTAG('V', 'I', 'D', 0))
  54. return 0;
  55. if (p->buf[4] != 2)
  56. return AVPROBE_SCORE_MAX / 4;
  57. return AVPROBE_SCORE_MAX;
  58. }
  59. static int vid_read_header(AVFormatContext *s)
  60. {
  61. BVID_DemuxContext *vid = s->priv_data;
  62. AVIOContext *pb = s->pb;
  63. /* load main header. Contents:
  64. * bytes: 'V' 'I' 'D'
  65. * int16s: always_512, nframes, width, height, delay, always_14
  66. */
  67. avio_skip(pb, 5);
  68. vid->nframes = avio_rl16(pb);
  69. vid->width = avio_rl16(pb);
  70. vid->height = avio_rl16(pb);
  71. vid->bethsoft_global_delay = avio_rl16(pb);
  72. avio_rl16(pb);
  73. // wait until the first packet to create each stream
  74. vid->video_index = -1;
  75. vid->audio_index = -1;
  76. vid->sample_rate = DEFAULT_SAMPLE_RATE;
  77. s->ctx_flags |= AVFMTCTX_NOHEADER;
  78. return 0;
  79. }
  80. #define BUFFER_PADDING_SIZE 1000
  81. static int read_frame(BVID_DemuxContext *vid, AVIOContext *pb, AVPacket *pkt,
  82. uint8_t block_type, AVFormatContext *s)
  83. {
  84. uint8_t * vidbuf_start = NULL;
  85. int vidbuf_nbytes = 0;
  86. int code;
  87. int bytes_copied = 0;
  88. int position, duration, npixels;
  89. unsigned int vidbuf_capacity;
  90. int ret = 0;
  91. AVStream *st;
  92. if (vid->video_index < 0) {
  93. st = avformat_new_stream(s, NULL);
  94. if (!st)
  95. return AVERROR(ENOMEM);
  96. vid->video_index = st->index;
  97. if (vid->audio_index < 0) {
  98. avpriv_request_sample(s, "Using default video time base since "
  99. "having no audio packet before the first "
  100. "video packet");
  101. }
  102. avpriv_set_pts_info(st, 64, 185, vid->sample_rate);
  103. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  104. st->codec->codec_id = AV_CODEC_ID_BETHSOFTVID;
  105. st->codec->width = vid->width;
  106. st->codec->height = vid->height;
  107. }
  108. st = s->streams[vid->video_index];
  109. npixels = st->codec->width * st->codec->height;
  110. vidbuf_start = av_malloc(vidbuf_capacity = BUFFER_PADDING_SIZE);
  111. if(!vidbuf_start)
  112. return AVERROR(ENOMEM);
  113. // save the file position for the packet, include block type
  114. position = avio_tell(pb) - 1;
  115. vidbuf_start[vidbuf_nbytes++] = block_type;
  116. // get the current packet duration
  117. duration = vid->bethsoft_global_delay + avio_rl16(pb);
  118. // set the y offset if it exists (decoder header data should be in data section)
  119. if(block_type == VIDEO_YOFF_P_FRAME){
  120. if (avio_read(pb, &vidbuf_start[vidbuf_nbytes], 2) != 2) {
  121. ret = AVERROR(EIO);
  122. goto fail;
  123. }
  124. vidbuf_nbytes += 2;
  125. }
  126. do{
  127. vidbuf_start = av_fast_realloc(vidbuf_start, &vidbuf_capacity, vidbuf_nbytes + BUFFER_PADDING_SIZE);
  128. if(!vidbuf_start)
  129. return AVERROR(ENOMEM);
  130. code = avio_r8(pb);
  131. vidbuf_start[vidbuf_nbytes++] = code;
  132. if(code >= 0x80){ // rle sequence
  133. if(block_type == VIDEO_I_FRAME)
  134. vidbuf_start[vidbuf_nbytes++] = avio_r8(pb);
  135. } else if(code){ // plain sequence
  136. if (avio_read(pb, &vidbuf_start[vidbuf_nbytes], code) != code) {
  137. ret = AVERROR(EIO);
  138. goto fail;
  139. }
  140. vidbuf_nbytes += code;
  141. }
  142. bytes_copied += code & 0x7F;
  143. if(bytes_copied == npixels){ // sometimes no stop character is given, need to keep track of bytes copied
  144. // may contain a 0 byte even if read all pixels
  145. if(avio_r8(pb))
  146. avio_seek(pb, -1, SEEK_CUR);
  147. break;
  148. }
  149. if (bytes_copied > npixels) {
  150. ret = AVERROR_INVALIDDATA;
  151. goto fail;
  152. }
  153. } while(code);
  154. // copy data into packet
  155. if ((ret = av_new_packet(pkt, vidbuf_nbytes)) < 0)
  156. goto fail;
  157. memcpy(pkt->data, vidbuf_start, vidbuf_nbytes);
  158. av_free(vidbuf_start);
  159. pkt->pos = position;
  160. pkt->stream_index = vid->video_index;
  161. pkt->duration = duration;
  162. if (block_type == VIDEO_I_FRAME)
  163. pkt->flags |= AV_PKT_FLAG_KEY;
  164. /* if there is a new palette available, add it to packet side data */
  165. if (vid->palette) {
  166. uint8_t *pdata = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE,
  167. BVID_PALETTE_SIZE);
  168. if (pdata)
  169. memcpy(pdata, vid->palette, BVID_PALETTE_SIZE);
  170. av_freep(&vid->palette);
  171. }
  172. vid->nframes--; // used to check if all the frames were read
  173. return 0;
  174. fail:
  175. av_free(vidbuf_start);
  176. return ret;
  177. }
  178. static int vid_read_packet(AVFormatContext *s,
  179. AVPacket *pkt)
  180. {
  181. BVID_DemuxContext *vid = s->priv_data;
  182. AVIOContext *pb = s->pb;
  183. unsigned char block_type;
  184. int audio_length;
  185. int ret_value;
  186. if(vid->is_finished || url_feof(pb))
  187. return AVERROR_EOF;
  188. block_type = avio_r8(pb);
  189. switch(block_type){
  190. case PALETTE_BLOCK:
  191. if (vid->palette) {
  192. av_log(s, AV_LOG_WARNING, "discarding unused palette\n");
  193. av_freep(&vid->palette);
  194. }
  195. vid->palette = av_malloc(BVID_PALETTE_SIZE);
  196. if (!vid->palette)
  197. return AVERROR(ENOMEM);
  198. if (avio_read(pb, vid->palette, BVID_PALETTE_SIZE) != BVID_PALETTE_SIZE) {
  199. av_freep(&vid->palette);
  200. return AVERROR(EIO);
  201. }
  202. return vid_read_packet(s, pkt);
  203. case FIRST_AUDIO_BLOCK:
  204. avio_rl16(pb);
  205. // soundblaster DAC used for sample rate, as on specification page (link above)
  206. vid->sample_rate = 1000000 / (256 - avio_r8(pb));
  207. case AUDIO_BLOCK:
  208. if (vid->audio_index < 0) {
  209. AVStream *st = avformat_new_stream(s, NULL);
  210. if (!st)
  211. return AVERROR(ENOMEM);
  212. vid->audio_index = st->index;
  213. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  214. st->codec->codec_id = AV_CODEC_ID_PCM_U8;
  215. st->codec->channels = 1;
  216. st->codec->channel_layout = AV_CH_LAYOUT_MONO;
  217. st->codec->bits_per_coded_sample = 8;
  218. st->codec->sample_rate = vid->sample_rate;
  219. st->codec->bit_rate = 8 * st->codec->sample_rate;
  220. st->start_time = 0;
  221. avpriv_set_pts_info(st, 64, 1, vid->sample_rate);
  222. }
  223. audio_length = avio_rl16(pb);
  224. if ((ret_value = av_get_packet(pb, pkt, audio_length)) != audio_length) {
  225. if (ret_value < 0)
  226. return ret_value;
  227. av_log(s, AV_LOG_ERROR, "incomplete audio block\n");
  228. return AVERROR(EIO);
  229. }
  230. pkt->stream_index = vid->audio_index;
  231. pkt->duration = audio_length;
  232. pkt->flags |= AV_PKT_FLAG_KEY;
  233. return 0;
  234. case VIDEO_P_FRAME:
  235. case VIDEO_YOFF_P_FRAME:
  236. case VIDEO_I_FRAME:
  237. return read_frame(vid, pb, pkt, block_type, s);
  238. case EOF_BLOCK:
  239. if(vid->nframes != 0)
  240. av_log(s, AV_LOG_VERBOSE, "reached terminating character but not all frames read.\n");
  241. vid->is_finished = 1;
  242. return AVERROR(EIO);
  243. default:
  244. av_log(s, AV_LOG_ERROR, "unknown block (character = %c, decimal = %d, hex = %x)!!!\n",
  245. block_type, block_type, block_type);
  246. return AVERROR_INVALIDDATA;
  247. }
  248. }
  249. static int vid_read_close(AVFormatContext *s)
  250. {
  251. BVID_DemuxContext *vid = s->priv_data;
  252. av_freep(&vid->palette);
  253. return 0;
  254. }
  255. AVInputFormat ff_bethsoftvid_demuxer = {
  256. .name = "bethsoftvid",
  257. .long_name = NULL_IF_CONFIG_SMALL("Bethesda Softworks VID"),
  258. .priv_data_size = sizeof(BVID_DemuxContext),
  259. .read_probe = vid_probe,
  260. .read_header = vid_read_header,
  261. .read_packet = vid_read_packet,
  262. .read_close = vid_read_close,
  263. };