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.

239 lines
6.7KB

  1. /*
  2. * AVS demuxer.
  3. * Copyright (c) 2006 Aurelien Jacobs <aurel@gnuage.org>
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "avformat.h"
  20. #include "voc.h"
  21. typedef struct avs_format {
  22. voc_dec_context_t voc;
  23. AVStream *st_video;
  24. AVStream *st_audio;
  25. int width;
  26. int height;
  27. int bits_per_sample;
  28. int fps;
  29. int nb_frames;
  30. int remaining_frame_size;
  31. int remaining_audio_size;
  32. } avs_format_t;
  33. typedef enum avs_block_type {
  34. AVS_VIDEO = 0x01,
  35. AVS_AUDIO = 0x02,
  36. AVS_PALETTE = 0x03,
  37. AVS_GAME_DATA = 0x04,
  38. } avs_block_type_t;
  39. #ifdef CONFIG_DEMUXERS
  40. static int avs_probe(AVProbeData * p)
  41. {
  42. const uint8_t *d;
  43. if (p->buf_size < 2)
  44. return 0;
  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. avs_format_t *avs = s->priv_data;
  53. s->ctx_flags |= AVFMTCTX_NOHEADER;
  54. url_fskip(&s->pb, 4);
  55. avs->width = get_le16(&s->pb);
  56. avs->height = get_le16(&s->pb);
  57. avs->bits_per_sample = get_le16(&s->pb);
  58. avs->fps = get_le16(&s->pb);
  59. avs->nb_frames = get_le32(&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. avs_block_type_t type, int sub_type, int size,
  72. uint8_t * palette, int palette_size)
  73. {
  74. avs_format_t *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 = get_buffer(&s->pb, pkt->data + palette_size + 4, size - 4) + 4;
  91. if (ret < size) {
  92. av_free_packet(pkt);
  93. return AVERROR_IO;
  94. }
  95. pkt->size = ret + palette_size;
  96. pkt->stream_index = avs->st_video->index;
  97. if (sub_type == 0)
  98. pkt->flags |= PKT_FLAG_KEY;
  99. return 0;
  100. }
  101. static int avs_read_audio_packet(AVFormatContext * s, AVPacket * pkt)
  102. {
  103. avs_format_t *avs = s->priv_data;
  104. int ret, size;
  105. size = url_ftell(&s->pb);
  106. ret = voc_get_packet(s, pkt, avs->st_audio, avs->remaining_audio_size);
  107. size = url_ftell(&s->pb) - size;
  108. avs->remaining_audio_size -= size;
  109. if (ret == AVERROR_IO)
  110. return 0; /* this indicate EOS */
  111. if (ret < 0)
  112. return ret;
  113. pkt->stream_index = avs->st_audio->index;
  114. pkt->flags |= PKT_FLAG_KEY;
  115. return size;
  116. }
  117. static int avs_read_packet(AVFormatContext * s, AVPacket * pkt)
  118. {
  119. avs_format_t *avs = s->priv_data;
  120. int sub_type = 0, size = 0;
  121. avs_block_type_t type = 0;
  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 (!get_le16(&s->pb)) /* found EOF */
  131. return AVERROR_IO;
  132. avs->remaining_frame_size = get_le16(&s->pb) - 4;
  133. }
  134. while (avs->remaining_frame_size > 0) {
  135. sub_type = get_byte(&s->pb);
  136. type = get_byte(&s->pb);
  137. size = get_le16(&s->pb);
  138. avs->remaining_frame_size -= size;
  139. switch (type) {
  140. case AVS_PALETTE:
  141. ret = get_buffer(&s->pb, palette, size - 4);
  142. if (ret < size - 4)
  143. return AVERROR_IO;
  144. palette_size = size;
  145. break;
  146. case AVS_VIDEO:
  147. if (!avs->st_video) {
  148. avs->st_video = av_new_stream(s, AVS_VIDEO);
  149. if (avs->st_video == NULL)
  150. return AVERROR_NOMEM;
  151. avs->st_video->codec->codec_type = CODEC_TYPE_VIDEO;
  152. avs->st_video->codec->codec_id = CODEC_ID_AVS;
  153. avs->st_video->codec->width = avs->width;
  154. avs->st_video->codec->height = avs->height;
  155. avs->st_video->codec->bits_per_sample=avs->bits_per_sample;
  156. avs->st_video->nb_frames = avs->nb_frames;
  157. avs->st_video->codec->time_base = (AVRational) {
  158. 1, avs->fps};
  159. }
  160. return avs_read_video_packet(s, pkt, type, sub_type, size,
  161. palette, palette_size);
  162. case AVS_AUDIO:
  163. if (!avs->st_audio) {
  164. avs->st_audio = av_new_stream(s, AVS_AUDIO);
  165. if (avs->st_audio == NULL)
  166. return AVERROR_NOMEM;
  167. avs->st_audio->codec->codec_type = CODEC_TYPE_AUDIO;
  168. }
  169. avs->remaining_audio_size = size - 4;
  170. size = avs_read_audio_packet(s, pkt);
  171. if (size != 0)
  172. return size;
  173. break;
  174. default:
  175. url_fskip(&s->pb, size - 4);
  176. }
  177. }
  178. }
  179. }
  180. static int avs_read_close(AVFormatContext * s)
  181. {
  182. return 0;
  183. }
  184. static AVInputFormat avs_iformat = {
  185. "avs",
  186. "avs format",
  187. sizeof(avs_format_t),
  188. avs_probe,
  189. avs_read_header,
  190. avs_read_packet,
  191. avs_read_close,
  192. };
  193. #endif /* CONFIG_DEMUXERS */
  194. int avs_init(void)
  195. {
  196. #ifdef CONFIG_DEMUXERS
  197. av_register_input_format(&avs_iformat);
  198. #endif /* CONFIG_DEMUXERS */
  199. return 0;
  200. }