/libavformat/bethsoftvid.c

http://github.com/FFmpeg/FFmpeg · C · 295 lines · 217 code · 35 blank · 43 comment · 43 complexity · f51e92d4791e1c746efbb0eaf7f360ab MD5 · raw file

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