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.9KB

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