/libavformat/avs.c

http://github.com/FFmpeg/FFmpeg · C · 233 lines · 172 code · 34 blank · 27 comment · 36 complexity · 828c2aab8da9590c026f183298290d02 MD5 · raw file

  1. /*
  2. * AVS demuxer.
  3. * Copyright (c) 2006 Aurelien Jacobs <aurel@gnuage.org>
  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. * Argonaut Games' Creature Shock demuxer
  24. * @see http://wiki.multimedia.cx/index.php?title=AVS
  25. */
  26. #include "avformat.h"
  27. #include "voc.h"
  28. typedef struct avs_format {
  29. VocDecContext voc;
  30. AVStream *st_video;
  31. AVStream *st_audio;
  32. int width;
  33. int height;
  34. int bits_per_sample;
  35. int fps;
  36. int nb_frames;
  37. int remaining_frame_size;
  38. int remaining_audio_size;
  39. } AvsFormat;
  40. typedef enum avs_block_type {
  41. AVS_NONE = 0x00,
  42. AVS_VIDEO = 0x01,
  43. AVS_AUDIO = 0x02,
  44. AVS_PALETTE = 0x03,
  45. AVS_GAME_DATA = 0x04,
  46. } AvsBlockType;
  47. static int avs_probe(const AVProbeData * p)
  48. {
  49. const uint8_t *d;
  50. d = p->buf;
  51. if (d[0] == 'w' && d[1] == 'W' && d[2] == 0x10 && d[3] == 0)
  52. /* Ensure the buffer probe scores higher than the extension probe.
  53. * This avoids problems with misdetection as AviSynth scripts. */
  54. return AVPROBE_SCORE_EXTENSION + 5;
  55. return 0;
  56. }
  57. static int avs_read_header(AVFormatContext * s)
  58. {
  59. AvsFormat *avs = s->priv_data;
  60. s->ctx_flags |= AVFMTCTX_NOHEADER;
  61. avio_skip(s->pb, 4);
  62. avs->width = avio_rl16(s->pb);
  63. avs->height = avio_rl16(s->pb);
  64. avs->bits_per_sample = avio_rl16(s->pb);
  65. avs->fps = avio_rl16(s->pb);
  66. avs->nb_frames = avio_rl32(s->pb);
  67. avs->remaining_frame_size = 0;
  68. avs->remaining_audio_size = 0;
  69. avs->st_video = avs->st_audio = NULL;
  70. if (avs->width != 318 || avs->height != 198)
  71. av_log(s, AV_LOG_ERROR, "This avs pretend to be %dx%d "
  72. "when the avs format is supposed to be 318x198 only.\n",
  73. avs->width, avs->height);
  74. return 0;
  75. }
  76. static int
  77. avs_read_video_packet(AVFormatContext * s, AVPacket * pkt,
  78. AvsBlockType type, int sub_type, int size,
  79. uint8_t * palette, int palette_size)
  80. {
  81. AvsFormat *avs = s->priv_data;
  82. int ret;
  83. ret = av_new_packet(pkt, size + palette_size);
  84. if (ret < 0)
  85. return ret;
  86. if (palette_size) {
  87. pkt->data[0] = 0x00;
  88. pkt->data[1] = 0x03;
  89. pkt->data[2] = palette_size & 0xFF;
  90. pkt->data[3] = (palette_size >> 8) & 0xFF;
  91. memcpy(pkt->data + 4, palette, palette_size - 4);
  92. }
  93. pkt->data[palette_size + 0] = sub_type;
  94. pkt->data[palette_size + 1] = type;
  95. pkt->data[palette_size + 2] = size & 0xFF;
  96. pkt->data[palette_size + 3] = (size >> 8) & 0xFF;
  97. ret = avio_read(s->pb, pkt->data + palette_size + 4, size - 4) + 4;
  98. if (ret < size) {
  99. return AVERROR(EIO);
  100. }
  101. pkt->size = ret + palette_size;
  102. pkt->stream_index = avs->st_video->index;
  103. if (sub_type == 0)
  104. pkt->flags |= AV_PKT_FLAG_KEY;
  105. return 0;
  106. }
  107. static int avs_read_audio_packet(AVFormatContext * s, AVPacket * pkt)
  108. {
  109. AvsFormat *avs = s->priv_data;
  110. int ret, size;
  111. size = avio_tell(s->pb);
  112. ret = ff_voc_get_packet(s, pkt, avs->st_audio, avs->remaining_audio_size);
  113. size = avio_tell(s->pb) - size;
  114. avs->remaining_audio_size -= size;
  115. if (ret == AVERROR(EIO))
  116. return 0; /* this indicate EOS */
  117. if (ret < 0)
  118. return ret;
  119. pkt->stream_index = avs->st_audio->index;
  120. pkt->flags |= AV_PKT_FLAG_KEY;
  121. return size;
  122. }
  123. static int avs_read_packet(AVFormatContext * s, AVPacket * pkt)
  124. {
  125. AvsFormat *avs = s->priv_data;
  126. int sub_type = 0, size = 0;
  127. AvsBlockType type = AVS_NONE;
  128. int palette_size = 0;
  129. uint8_t palette[4 + 3 * 256];
  130. int ret;
  131. if (avs->remaining_audio_size > 0)
  132. if (avs_read_audio_packet(s, pkt) > 0)
  133. return 0;
  134. while (1) {
  135. if (avs->remaining_frame_size <= 0) {
  136. if (!avio_rl16(s->pb)) /* found EOF */
  137. return AVERROR(EIO);
  138. avs->remaining_frame_size = avio_rl16(s->pb) - 4;
  139. }
  140. while (avs->remaining_frame_size > 0) {
  141. sub_type = avio_r8(s->pb);
  142. type = avio_r8(s->pb);
  143. size = avio_rl16(s->pb);
  144. if (size < 4)
  145. return AVERROR_INVALIDDATA;
  146. avs->remaining_frame_size -= size;
  147. switch (type) {
  148. case AVS_PALETTE:
  149. if (size - 4 > sizeof(palette))
  150. return AVERROR_INVALIDDATA;
  151. ret = avio_read(s->pb, palette, size - 4);
  152. if (ret < size - 4)
  153. return AVERROR(EIO);
  154. palette_size = size;
  155. break;
  156. case AVS_VIDEO:
  157. if (!avs->st_video) {
  158. avs->st_video = avformat_new_stream(s, NULL);
  159. if (!avs->st_video)
  160. return AVERROR(ENOMEM);
  161. avs->st_video->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  162. avs->st_video->codecpar->codec_id = AV_CODEC_ID_AVS;
  163. avs->st_video->codecpar->width = avs->width;
  164. avs->st_video->codecpar->height = avs->height;
  165. avs->st_video->codecpar->bits_per_coded_sample=avs->bits_per_sample;
  166. avs->st_video->nb_frames = avs->nb_frames;
  167. #if FF_API_R_FRAME_RATE
  168. avs->st_video->r_frame_rate =
  169. #endif
  170. avs->st_video->avg_frame_rate = (AVRational){avs->fps, 1};
  171. }
  172. return avs_read_video_packet(s, pkt, type, sub_type, size,
  173. palette, palette_size);
  174. case AVS_AUDIO:
  175. if (!avs->st_audio) {
  176. avs->st_audio = avformat_new_stream(s, NULL);
  177. if (!avs->st_audio)
  178. return AVERROR(ENOMEM);
  179. avs->st_audio->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  180. }
  181. avs->remaining_audio_size = size - 4;
  182. size = avs_read_audio_packet(s, pkt);
  183. if (size != 0)
  184. return size;
  185. break;
  186. default:
  187. avio_skip(s->pb, size - 4);
  188. }
  189. }
  190. }
  191. }
  192. AVInputFormat ff_avs_demuxer = {
  193. .name = "avs",
  194. .long_name = NULL_IF_CONFIG_SMALL("Argonaut Games Creature Shock"),
  195. .priv_data_size = sizeof(AvsFormat),
  196. .read_probe = avs_probe,
  197. .read_header = avs_read_header,
  198. .read_packet = avs_read_packet,
  199. };