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.

228 lines
6.5KB

  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 St, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avformat.h"
  22. #include "voc.h"
  23. typedef struct avs_format {
  24. voc_dec_context_t 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. } avs_format_t;
  35. typedef enum avs_block_type {
  36. AVS_VIDEO = 0x01,
  37. AVS_AUDIO = 0x02,
  38. AVS_PALETTE = 0x03,
  39. AVS_GAME_DATA = 0x04,
  40. } avs_block_type_t;
  41. static int avs_probe(AVProbeData * p)
  42. {
  43. const uint8_t *d;
  44. if (p->buf_size < 2)
  45. return 0;
  46. d = p->buf;
  47. if (d[0] == 'w' && d[1] == 'W' && d[2] == 0x10 && d[3] == 0)
  48. return 50;
  49. return 0;
  50. }
  51. static int avs_read_header(AVFormatContext * s, AVFormatParameters * ap)
  52. {
  53. avs_format_t *avs = s->priv_data;
  54. s->ctx_flags |= AVFMTCTX_NOHEADER;
  55. url_fskip(&s->pb, 4);
  56. avs->width = get_le16(&s->pb);
  57. avs->height = get_le16(&s->pb);
  58. avs->bits_per_sample = get_le16(&s->pb);
  59. avs->fps = get_le16(&s->pb);
  60. avs->nb_frames = get_le32(&s->pb);
  61. avs->remaining_frame_size = 0;
  62. avs->remaining_audio_size = 0;
  63. avs->st_video = avs->st_audio = NULL;
  64. if (avs->width != 318 || avs->height != 198)
  65. av_log(s, AV_LOG_ERROR, "This avs pretend to be %dx%d "
  66. "when the avs format is supposed to be 318x198 only.\n",
  67. avs->width, avs->height);
  68. return 0;
  69. }
  70. static int
  71. avs_read_video_packet(AVFormatContext * s, AVPacket * pkt,
  72. avs_block_type_t type, int sub_type, int size,
  73. uint8_t * palette, int palette_size)
  74. {
  75. avs_format_t *avs = s->priv_data;
  76. int ret;
  77. ret = av_new_packet(pkt, size + palette_size);
  78. if (ret < 0)
  79. return ret;
  80. if (palette_size) {
  81. pkt->data[0] = 0x00;
  82. pkt->data[1] = 0x03;
  83. pkt->data[2] = palette_size & 0xFF;
  84. pkt->data[3] = (palette_size >> 8) & 0xFF;
  85. memcpy(pkt->data + 4, palette, palette_size - 4);
  86. }
  87. pkt->data[palette_size + 0] = sub_type;
  88. pkt->data[palette_size + 1] = type;
  89. pkt->data[palette_size + 2] = size & 0xFF;
  90. pkt->data[palette_size + 3] = (size >> 8) & 0xFF;
  91. ret = get_buffer(&s->pb, pkt->data + palette_size + 4, size - 4) + 4;
  92. if (ret < size) {
  93. av_free_packet(pkt);
  94. return AVERROR_IO;
  95. }
  96. pkt->size = ret + palette_size;
  97. pkt->stream_index = avs->st_video->index;
  98. if (sub_type == 0)
  99. pkt->flags |= PKT_FLAG_KEY;
  100. return 0;
  101. }
  102. static int avs_read_audio_packet(AVFormatContext * s, AVPacket * pkt)
  103. {
  104. avs_format_t *avs = s->priv_data;
  105. int ret, size;
  106. size = url_ftell(&s->pb);
  107. ret = voc_get_packet(s, pkt, avs->st_audio, avs->remaining_audio_size);
  108. size = url_ftell(&s->pb) - size;
  109. avs->remaining_audio_size -= size;
  110. if (ret == AVERROR_IO)
  111. return 0; /* this indicate EOS */
  112. if (ret < 0)
  113. return ret;
  114. pkt->stream_index = avs->st_audio->index;
  115. pkt->flags |= PKT_FLAG_KEY;
  116. return size;
  117. }
  118. static int avs_read_packet(AVFormatContext * s, AVPacket * pkt)
  119. {
  120. avs_format_t *avs = s->priv_data;
  121. int sub_type = 0, size = 0;
  122. avs_block_type_t type = 0;
  123. int palette_size = 0;
  124. uint8_t palette[4 + 3 * 256];
  125. int ret;
  126. if (avs->remaining_audio_size > 0)
  127. if (avs_read_audio_packet(s, pkt) > 0)
  128. return 0;
  129. while (1) {
  130. if (avs->remaining_frame_size <= 0) {
  131. if (!get_le16(&s->pb)) /* found EOF */
  132. return AVERROR_IO;
  133. avs->remaining_frame_size = get_le16(&s->pb) - 4;
  134. }
  135. while (avs->remaining_frame_size > 0) {
  136. sub_type = get_byte(&s->pb);
  137. type = get_byte(&s->pb);
  138. size = get_le16(&s->pb);
  139. avs->remaining_frame_size -= size;
  140. switch (type) {
  141. case AVS_PALETTE:
  142. ret = get_buffer(&s->pb, palette, size - 4);
  143. if (ret < size - 4)
  144. return AVERROR_IO;
  145. palette_size = size;
  146. break;
  147. case AVS_VIDEO:
  148. if (!avs->st_video) {
  149. avs->st_video = av_new_stream(s, AVS_VIDEO);
  150. if (avs->st_video == NULL)
  151. return AVERROR_NOMEM;
  152. avs->st_video->codec->codec_type = CODEC_TYPE_VIDEO;
  153. avs->st_video->codec->codec_id = CODEC_ID_AVS;
  154. avs->st_video->codec->width = avs->width;
  155. avs->st_video->codec->height = avs->height;
  156. avs->st_video->codec->bits_per_sample=avs->bits_per_sample;
  157. avs->st_video->nb_frames = avs->nb_frames;
  158. avs->st_video->codec->time_base = (AVRational) {
  159. 1, avs->fps};
  160. }
  161. return avs_read_video_packet(s, pkt, type, sub_type, size,
  162. palette, palette_size);
  163. case AVS_AUDIO:
  164. if (!avs->st_audio) {
  165. avs->st_audio = av_new_stream(s, AVS_AUDIO);
  166. if (avs->st_audio == NULL)
  167. return AVERROR_NOMEM;
  168. avs->st_audio->codec->codec_type = CODEC_TYPE_AUDIO;
  169. }
  170. avs->remaining_audio_size = size - 4;
  171. size = avs_read_audio_packet(s, pkt);
  172. if (size != 0)
  173. return size;
  174. break;
  175. default:
  176. url_fskip(&s->pb, size - 4);
  177. }
  178. }
  179. }
  180. }
  181. static int avs_read_close(AVFormatContext * s)
  182. {
  183. return 0;
  184. }
  185. AVInputFormat avs_demuxer = {
  186. "avs",
  187. "avs format",
  188. sizeof(avs_format_t),
  189. avs_probe,
  190. avs_read_header,
  191. avs_read_packet,
  192. avs_read_close,
  193. };