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.

307 lines
10KB

  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->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  104. st->codecpar->codec_id = AV_CODEC_ID_BETHSOFTVID;
  105. st->codecpar->width = vid->width;
  106. st->codecpar->height = vid->height;
  107. }
  108. st = s->streams[vid->video_index];
  109. npixels = st->codecpar->width * st->codecpar->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. uint8_t *tmp = av_fast_realloc(vidbuf_start, &vidbuf_capacity,
  128. vidbuf_nbytes + BUFFER_PADDING_SIZE);
  129. if (!tmp) {
  130. ret = AVERROR(ENOMEM);
  131. goto fail;
  132. }
  133. vidbuf_start = tmp;
  134. code = avio_r8(pb);
  135. vidbuf_start[vidbuf_nbytes++] = code;
  136. if(code >= 0x80){ // rle sequence
  137. if(block_type == VIDEO_I_FRAME)
  138. vidbuf_start[vidbuf_nbytes++] = avio_r8(pb);
  139. } else if(code){ // plain sequence
  140. if (avio_read(pb, &vidbuf_start[vidbuf_nbytes], code) != code) {
  141. ret = AVERROR(EIO);
  142. goto fail;
  143. }
  144. vidbuf_nbytes += code;
  145. }
  146. bytes_copied += code & 0x7F;
  147. if(bytes_copied == npixels){ // sometimes no stop character is given, need to keep track of bytes copied
  148. // may contain a 0 byte even if read all pixels
  149. if(avio_r8(pb))
  150. avio_seek(pb, -1, SEEK_CUR);
  151. break;
  152. }
  153. if (bytes_copied > npixels) {
  154. ret = AVERROR_INVALIDDATA;
  155. goto fail;
  156. }
  157. } while(code);
  158. // copy data into packet
  159. if ((ret = av_new_packet(pkt, vidbuf_nbytes)) < 0)
  160. goto fail;
  161. memcpy(pkt->data, vidbuf_start, vidbuf_nbytes);
  162. pkt->pos = position;
  163. pkt->stream_index = vid->video_index;
  164. pkt->duration = duration;
  165. if (block_type == VIDEO_I_FRAME)
  166. pkt->flags |= AV_PKT_FLAG_KEY;
  167. /* if there is a new palette available, add it to packet side data */
  168. if (vid->palette) {
  169. uint8_t *pdata = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE,
  170. BVID_PALETTE_SIZE);
  171. if (!pdata) {
  172. ret = AVERROR(ENOMEM);
  173. av_log(s, AV_LOG_ERROR, "Failed to allocate palette side data\n");
  174. goto fail;
  175. }
  176. memcpy(pdata, vid->palette, BVID_PALETTE_SIZE);
  177. av_freep(&vid->palette);
  178. }
  179. vid->nframes--; // used to check if all the frames were read
  180. fail:
  181. av_free(vidbuf_start);
  182. return ret;
  183. }
  184. static int vid_read_packet(AVFormatContext *s,
  185. AVPacket *pkt)
  186. {
  187. BVID_DemuxContext *vid = s->priv_data;
  188. AVIOContext *pb = s->pb;
  189. unsigned char block_type;
  190. int audio_length;
  191. int ret_value;
  192. if(vid->is_finished || avio_feof(pb))
  193. return AVERROR_EOF;
  194. block_type = avio_r8(pb);
  195. switch(block_type){
  196. case PALETTE_BLOCK:
  197. if (vid->palette) {
  198. av_log(s, AV_LOG_WARNING, "discarding unused palette\n");
  199. av_freep(&vid->palette);
  200. }
  201. vid->palette = av_malloc(BVID_PALETTE_SIZE);
  202. if (!vid->palette)
  203. return AVERROR(ENOMEM);
  204. if (avio_read(pb, vid->palette, BVID_PALETTE_SIZE) != BVID_PALETTE_SIZE) {
  205. av_freep(&vid->palette);
  206. return AVERROR(EIO);
  207. }
  208. return vid_read_packet(s, pkt);
  209. case FIRST_AUDIO_BLOCK:
  210. avio_rl16(pb);
  211. // soundblaster DAC used for sample rate, as on specification page (link above)
  212. vid->sample_rate = 1000000 / (256 - avio_r8(pb));
  213. case AUDIO_BLOCK:
  214. if (vid->audio_index < 0) {
  215. AVStream *st = avformat_new_stream(s, NULL);
  216. if (!st)
  217. return AVERROR(ENOMEM);
  218. vid->audio_index = st->index;
  219. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  220. st->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
  221. st->codecpar->channels = 1;
  222. st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
  223. st->codecpar->bits_per_coded_sample = 8;
  224. st->codecpar->sample_rate = vid->sample_rate;
  225. st->codecpar->bit_rate = 8 * st->codecpar->sample_rate;
  226. st->start_time = 0;
  227. avpriv_set_pts_info(st, 64, 1, vid->sample_rate);
  228. }
  229. audio_length = avio_rl16(pb);
  230. if ((ret_value = av_get_packet(pb, pkt, audio_length)) != audio_length) {
  231. if (ret_value < 0)
  232. return ret_value;
  233. av_log(s, AV_LOG_ERROR, "incomplete audio block\n");
  234. return AVERROR(EIO);
  235. }
  236. pkt->stream_index = vid->audio_index;
  237. pkt->duration = audio_length;
  238. pkt->flags |= AV_PKT_FLAG_KEY;
  239. return 0;
  240. case VIDEO_P_FRAME:
  241. case VIDEO_YOFF_P_FRAME:
  242. case VIDEO_I_FRAME:
  243. return read_frame(vid, pb, pkt, block_type, s);
  244. case EOF_BLOCK:
  245. if(vid->nframes != 0)
  246. av_log(s, AV_LOG_VERBOSE, "reached terminating character but not all frames read.\n");
  247. vid->is_finished = 1;
  248. return AVERROR(EIO);
  249. default:
  250. av_log(s, AV_LOG_ERROR, "unknown block (character = %c, decimal = %d, hex = %x)!!!\n",
  251. block_type, block_type, block_type);
  252. return AVERROR_INVALIDDATA;
  253. }
  254. }
  255. static int vid_read_close(AVFormatContext *s)
  256. {
  257. BVID_DemuxContext *vid = s->priv_data;
  258. av_freep(&vid->palette);
  259. return 0;
  260. }
  261. AVInputFormat ff_bethsoftvid_demuxer = {
  262. .name = "bethsoftvid",
  263. .long_name = NULL_IF_CONFIG_SMALL("Bethesda Softworks VID"),
  264. .priv_data_size = sizeof(BVID_DemuxContext),
  265. .read_probe = vid_probe,
  266. .read_header = vid_read_header,
  267. .read_packet = vid_read_packet,
  268. .read_close = vid_read_close,
  269. };