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.

253 lines
7.2KB

  1. /*
  2. * LucasArts Smush demuxer
  3. * Copyright (c) 2006 Cyril Zorin
  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 "libavutil/intreadwrite.h"
  22. #include "avformat.h"
  23. #include "avio.h"
  24. #include "internal.h"
  25. typedef struct SMUSHContext {
  26. int version;
  27. int audio_stream_index;
  28. int video_stream_index;
  29. } SMUSHContext;
  30. static int smush_read_probe(AVProbeData *p)
  31. {
  32. if (((AV_RL32(p->buf) == MKTAG('S', 'A', 'N', 'M') &&
  33. AV_RL32(p->buf + 8) == MKTAG('S', 'H', 'D', 'R')) ||
  34. (AV_RL32(p->buf) == MKTAG('A', 'N', 'I', 'M') &&
  35. AV_RL32(p->buf + 8) == MKTAG('A', 'H', 'D', 'R')))) {
  36. return AVPROBE_SCORE_MAX;
  37. }
  38. return 0;
  39. }
  40. static int smush_read_header(AVFormatContext *ctx)
  41. {
  42. SMUSHContext *smush = ctx->priv_data;
  43. AVIOContext *pb = ctx->pb;
  44. AVStream *vst, *ast;
  45. uint32_t magic, nframes, size, subversion, i;
  46. uint32_t width = 0, height = 0, got_audio = 0, read = 0;
  47. uint32_t sample_rate, channels, palette[256];
  48. magic = avio_rb32(pb);
  49. avio_skip(pb, 4); // skip movie size
  50. if (magic == MKBETAG('A', 'N', 'I', 'M')) {
  51. if (avio_rb32(pb) != MKBETAG('A', 'H', 'D', 'R'))
  52. return AVERROR_INVALIDDATA;
  53. size = avio_rb32(pb);
  54. if (size < 3 * 256 + 6)
  55. return AVERROR_INVALIDDATA;
  56. smush->version = 0;
  57. subversion = avio_rl16(pb);
  58. nframes = avio_rl16(pb);
  59. if (!nframes)
  60. return AVERROR_INVALIDDATA;
  61. avio_skip(pb, 2); // skip pad
  62. for (i = 0; i < 256; i++)
  63. palette[i] = avio_rb24(pb);
  64. avio_skip(pb, size - (3 * 256 + 6));
  65. } else if (magic == MKBETAG('S', 'A', 'N', 'M')) {
  66. if (avio_rb32(pb) != MKBETAG('S', 'H', 'D', 'R'))
  67. return AVERROR_INVALIDDATA;
  68. size = avio_rb32(pb);
  69. if (size < 14)
  70. return AVERROR_INVALIDDATA;
  71. smush->version = 1;
  72. subversion = avio_rl16(pb);
  73. nframes = avio_rl32(pb);
  74. if (!nframes)
  75. return AVERROR_INVALIDDATA;
  76. avio_skip(pb, 2); // skip pad
  77. width = avio_rl16(pb);
  78. height = avio_rl16(pb);
  79. avio_skip(pb, 2); // skip pad
  80. avio_skip(pb, size - 14);
  81. if (avio_rb32(pb) != MKBETAG('F', 'L', 'H', 'D'))
  82. return AVERROR_INVALIDDATA;
  83. size = avio_rb32(pb);
  84. while (!got_audio && ((read + 8) < size)) {
  85. uint32_t sig, chunk_size;
  86. if (url_feof(pb))
  87. return AVERROR_EOF;
  88. sig = avio_rb32(pb);
  89. chunk_size = avio_rb32(pb);
  90. read += 8;
  91. switch (sig) {
  92. case MKBETAG('W', 'a', 'v', 'e'):
  93. got_audio = 1;
  94. sample_rate = avio_rl32(pb);
  95. if (!sample_rate)
  96. return AVERROR_INVALIDDATA;
  97. channels = avio_rl32(pb);
  98. if (!channels)
  99. return AVERROR_INVALIDDATA;
  100. avio_skip(pb, chunk_size - 8);
  101. read += chunk_size;
  102. break;
  103. case MKBETAG('B', 'l', '1', '6'):
  104. case MKBETAG('A', 'N', 'N', 'O'):
  105. avio_skip(pb, chunk_size);
  106. read += chunk_size;
  107. break;
  108. default:
  109. return AVERROR_INVALIDDATA;
  110. break;
  111. }
  112. }
  113. avio_skip(pb, size - read);
  114. } else {
  115. av_log(ctx, AV_LOG_ERROR, "Wrong magic\n");
  116. return AVERROR_INVALIDDATA;
  117. }
  118. vst = avformat_new_stream(ctx, 0);
  119. if (!vst)
  120. return AVERROR(ENOMEM);
  121. smush->video_stream_index = vst->index;
  122. avpriv_set_pts_info(vst, 64, 1, 15);
  123. vst->start_time = 0;
  124. vst->duration =
  125. vst->nb_frames = nframes;
  126. vst->avg_frame_rate = av_inv_q(vst->time_base);
  127. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  128. vst->codec->codec_id = AV_CODEC_ID_SANM;
  129. vst->codec->codec_tag = 0;
  130. vst->codec->width = width;
  131. vst->codec->height = height;
  132. if (!smush->version) {
  133. if (ff_alloc_extradata(vst->codec, 1024 + 2))
  134. return AVERROR(ENOMEM);
  135. AV_WL16(vst->codec->extradata, subversion);
  136. for (i = 0; i < 256; i++)
  137. AV_WL32(vst->codec->extradata + 2 + i * 4, palette[i]);
  138. }
  139. if (got_audio) {
  140. ast = avformat_new_stream(ctx, 0);
  141. if (!ast)
  142. return AVERROR(ENOMEM);
  143. smush->audio_stream_index = ast->index;
  144. ast->start_time = 0;
  145. ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  146. ast->codec->codec_id = AV_CODEC_ID_ADPCM_VIMA;
  147. ast->codec->codec_tag = 0;
  148. ast->codec->sample_rate = sample_rate;
  149. ast->codec->channels = channels;
  150. avpriv_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
  151. }
  152. return 0;
  153. }
  154. static int smush_read_packet(AVFormatContext *ctx, AVPacket *pkt)
  155. {
  156. SMUSHContext *smush = ctx->priv_data;
  157. AVIOContext *pb = ctx->pb;
  158. int done = 0;
  159. int ret;
  160. while (!done) {
  161. uint32_t sig, size;
  162. if (url_feof(pb))
  163. return AVERROR_EOF;
  164. sig = avio_rb32(pb);
  165. size = avio_rb32(pb);
  166. switch (sig) {
  167. case MKBETAG('F', 'R', 'M', 'E'):
  168. if (smush->version)
  169. break;
  170. if ((ret = av_get_packet(pb, pkt, size)) < 0)
  171. return ret;
  172. pkt->stream_index = smush->video_stream_index;
  173. done = 1;
  174. break;
  175. case MKBETAG('B', 'l', '1', '6'):
  176. if ((ret = av_get_packet(pb, pkt, size)) < 0)
  177. return ret;
  178. pkt->stream_index = smush->video_stream_index;
  179. pkt->duration = 1;
  180. done = 1;
  181. break;
  182. case MKBETAG('W', 'a', 'v', 'e'):
  183. if (size < 13)
  184. return AVERROR_INVALIDDATA;
  185. if (av_get_packet(pb, pkt, size) < 13)
  186. return AVERROR(EIO);
  187. pkt->stream_index = smush->audio_stream_index;
  188. pkt->flags |= AV_PKT_FLAG_KEY;
  189. pkt->duration = AV_RB32(pkt->data);
  190. if (pkt->duration == 0xFFFFFFFFu)
  191. pkt->duration = AV_RB32(pkt->data + 8);
  192. done = 1;
  193. break;
  194. default:
  195. avio_skip(pb, size);
  196. break;
  197. }
  198. }
  199. return 0;
  200. }
  201. AVInputFormat ff_smush_demuxer = {
  202. .name = "smush",
  203. .long_name = NULL_IF_CONFIG_SMALL("LucasArts Smush"),
  204. .priv_data_size = sizeof(SMUSHContext),
  205. .read_probe = smush_read_probe,
  206. .read_header = smush_read_header,
  207. .read_packet = smush_read_packet,
  208. };