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.

235 lines
7.0KB

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