avs.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. return 50;
  48. return 0;
  49. }
  50. static int avs_read_header(AVFormatContext * s, AVFormatParameters * ap)
  51. {
  52. AvsFormat *avs = s->priv_data;
  53. s->ctx_flags |= AVFMTCTX_NOHEADER;
  54. avio_skip(s->pb, 4);
  55. avs->width = avio_rl16(s->pb);
  56. avs->height = avio_rl16(s->pb);
  57. avs->bits_per_sample = avio_rl16(s->pb);
  58. avs->fps = avio_rl16(s->pb);
  59. avs->nb_frames = avio_rl32(s->pb);
  60. avs->remaining_frame_size = 0;
  61. avs->remaining_audio_size = 0;
  62. avs->st_video = avs->st_audio = NULL;
  63. if (avs->width != 318 || avs->height != 198)
  64. av_log(s, AV_LOG_ERROR, "This avs pretend to be %dx%d "
  65. "when the avs format is supposed to be 318x198 only.\n",
  66. avs->width, avs->height);
  67. return 0;
  68. }
  69. static int
  70. avs_read_video_packet(AVFormatContext * s, AVPacket * pkt,
  71. AvsBlockType type, int sub_type, int size,
  72. uint8_t * palette, int palette_size)
  73. {
  74. AvsFormat *avs = s->priv_data;
  75. int ret;
  76. ret = av_new_packet(pkt, size + palette_size);
  77. if (ret < 0)
  78. return ret;
  79. if (palette_size) {
  80. pkt->data[0] = 0x00;
  81. pkt->data[1] = 0x03;
  82. pkt->data[2] = palette_size & 0xFF;
  83. pkt->data[3] = (palette_size >> 8) & 0xFF;
  84. memcpy(pkt->data + 4, palette, palette_size - 4);
  85. }
  86. pkt->data[palette_size + 0] = sub_type;
  87. pkt->data[palette_size + 1] = type;
  88. pkt->data[palette_size + 2] = size & 0xFF;
  89. pkt->data[palette_size + 3] = (size >> 8) & 0xFF;
  90. ret = avio_read(s->pb, pkt->data + palette_size + 4, size - 4) + 4;
  91. if (ret < size) {
  92. av_free_packet(pkt);
  93. return AVERROR(EIO);
  94. }
  95. pkt->size = ret + palette_size;
  96. pkt->stream_index = avs->st_video->index;
  97. if (sub_type == 0)
  98. pkt->flags |= AV_PKT_FLAG_KEY;
  99. return 0;
  100. }
  101. static int avs_read_audio_packet(AVFormatContext * s, AVPacket * pkt)
  102. {
  103. AvsFormat *avs = s->priv_data;
  104. int ret, size;
  105. size = avio_tell(s->pb);
  106. ret = voc_get_packet(s, pkt, avs->st_audio, avs->remaining_audio_size);
  107. size = avio_tell(s->pb) - size;
  108. avs->remaining_audio_size -= size;
  109. if (ret == AVERROR(EIO))
  110. return 0; /* this indicate EOS */
  111. if (ret < 0)
  112. return ret;
  113. pkt->stream_index = avs->st_audio->index;
  114. pkt->flags |= AV_PKT_FLAG_KEY;
  115. return size;
  116. }
  117. static int avs_read_packet(AVFormatContext * s, AVPacket * pkt)
  118. {
  119. AvsFormat *avs = s->priv_data;
  120. int sub_type = 0, size = 0;
  121. AvsBlockType type = AVS_NONE;
  122. int palette_size = 0;
  123. uint8_t palette[4 + 3 * 256];
  124. int ret;
  125. if (avs->remaining_audio_size > 0)
  126. if (avs_read_audio_packet(s, pkt) > 0)
  127. return 0;
  128. while (1) {
  129. if (avs->remaining_frame_size <= 0) {
  130. if (!avio_rl16(s->pb)) /* found EOF */
  131. return AVERROR(EIO);
  132. avs->remaining_frame_size = avio_rl16(s->pb) - 4;
  133. }
  134. while (avs->remaining_frame_size > 0) {
  135. sub_type = avio_r8(s->pb);
  136. type = avio_r8(s->pb);
  137. size = avio_rl16(s->pb);
  138. if (size < 4)
  139. return AVERROR_INVALIDDATA;
  140. avs->remaining_frame_size -= size;
  141. switch (type) {
  142. case AVS_PALETTE:
  143. if (size - 4 > sizeof(palette))
  144. return AVERROR_INVALIDDATA;
  145. ret = avio_read(s->pb, palette, size - 4);
  146. if (ret < size - 4)
  147. return AVERROR(EIO);
  148. palette_size = size;
  149. break;
  150. case AVS_VIDEO:
  151. if (!avs->st_video) {
  152. avs->st_video = av_new_stream(s, AVS_VIDEO);
  153. if (avs->st_video == NULL)
  154. return AVERROR(ENOMEM);
  155. avs->st_video->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  156. avs->st_video->codec->codec_id = CODEC_ID_AVS;
  157. avs->st_video->codec->width = avs->width;
  158. avs->st_video->codec->height = avs->height;
  159. avs->st_video->codec->bits_per_coded_sample=avs->bits_per_sample;
  160. avs->st_video->nb_frames = avs->nb_frames;
  161. avs->st_video->codec->time_base = (AVRational) {
  162. 1, avs->fps};
  163. }
  164. return avs_read_video_packet(s, pkt, type, sub_type, size,
  165. palette, palette_size);
  166. case AVS_AUDIO:
  167. if (!avs->st_audio) {
  168. avs->st_audio = av_new_stream(s, AVS_AUDIO);
  169. if (avs->st_audio == NULL)
  170. return AVERROR(ENOMEM);
  171. avs->st_audio->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  172. }
  173. avs->remaining_audio_size = size - 4;
  174. size = avs_read_audio_packet(s, pkt);
  175. if (size != 0)
  176. return size;
  177. break;
  178. default:
  179. avio_skip(s->pb, size - 4);
  180. }
  181. }
  182. }
  183. }
  184. static int avs_read_close(AVFormatContext * s)
  185. {
  186. return 0;
  187. }
  188. AVInputFormat ff_avs_demuxer = {
  189. .name = "avs",
  190. .long_name = NULL_IF_CONFIG_SMALL("AVS format"),
  191. .priv_data_size = sizeof(AvsFormat),
  192. .read_probe = avs_probe,
  193. .read_header = avs_read_header,
  194. .read_packet = avs_read_packet,
  195. .read_close = avs_read_close,
  196. };