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.

256 lines
7.5KB

  1. /*
  2. * LucasArts Smush demuxer
  3. * Copyright (c) 2006 Cyril Zorin
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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 (pb->eof_reached)
  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. }
  111. }
  112. avio_skip(pb, size - read);
  113. } else {
  114. av_log(ctx, AV_LOG_ERROR, "Wrong magic\n");
  115. return AVERROR_INVALIDDATA;
  116. }
  117. vst = avformat_new_stream(ctx, 0);
  118. if (!vst)
  119. return AVERROR(ENOMEM);
  120. smush->video_stream_index = vst->index;
  121. avpriv_set_pts_info(vst, 64, 1, 15);
  122. vst->start_time = 0;
  123. vst->duration =
  124. vst->nb_frames = nframes;
  125. vst->avg_frame_rate = av_inv_q(vst->time_base);
  126. vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  127. vst->codecpar->codec_id = AV_CODEC_ID_SANM;
  128. vst->codecpar->codec_tag = 0;
  129. vst->codecpar->width = width;
  130. vst->codecpar->height = height;
  131. if (!smush->version) {
  132. av_free(vst->codecpar->extradata);
  133. vst->codecpar->extradata_size = 1024 + 2;
  134. vst->codecpar->extradata = av_malloc(vst->codecpar->extradata_size +
  135. AV_INPUT_BUFFER_PADDING_SIZE);
  136. if (!vst->codecpar->extradata)
  137. return AVERROR(ENOMEM);
  138. AV_WL16(vst->codecpar->extradata, subversion);
  139. for (i = 0; i < 256; i++)
  140. AV_WL32(vst->codecpar->extradata + 2 + i * 4, palette[i]);
  141. }
  142. if (got_audio) {
  143. ast = avformat_new_stream(ctx, 0);
  144. if (!ast)
  145. return AVERROR(ENOMEM);
  146. smush->audio_stream_index = ast->index;
  147. ast->start_time = 0;
  148. ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  149. ast->codecpar->codec_id = AV_CODEC_ID_ADPCM_VIMA;
  150. ast->codecpar->codec_tag = 0;
  151. ast->codecpar->sample_rate = sample_rate;
  152. ast->codecpar->channels = channels;
  153. avpriv_set_pts_info(ast, 64, 1, ast->codecpar->sample_rate);
  154. }
  155. return 0;
  156. }
  157. static int smush_read_packet(AVFormatContext *ctx, AVPacket *pkt)
  158. {
  159. SMUSHContext *smush = ctx->priv_data;
  160. AVIOContext *pb = ctx->pb;
  161. int done = 0;
  162. int ret;
  163. while (!done) {
  164. uint32_t sig, size;
  165. if (pb->eof_reached)
  166. return AVERROR_EOF;
  167. sig = avio_rb32(pb);
  168. size = avio_rb32(pb);
  169. switch (sig) {
  170. case MKBETAG('F', 'R', 'M', 'E'):
  171. if (smush->version)
  172. break;
  173. if ((ret = av_get_packet(pb, pkt, size)) < 0)
  174. return ret;
  175. pkt->stream_index = smush->video_stream_index;
  176. done = 1;
  177. break;
  178. case MKBETAG('B', 'l', '1', '6'):
  179. if ((ret = av_get_packet(pb, pkt, size)) < 0)
  180. return ret;
  181. pkt->stream_index = smush->video_stream_index;
  182. pkt->duration = 1;
  183. done = 1;
  184. break;
  185. case MKBETAG('W', 'a', 'v', 'e'):
  186. if (size < 13)
  187. return AVERROR_INVALIDDATA;
  188. if (av_get_packet(pb, pkt, size) < 13)
  189. return AVERROR(EIO);
  190. pkt->stream_index = smush->audio_stream_index;
  191. pkt->flags |= AV_PKT_FLAG_KEY;
  192. pkt->duration = AV_RB32(pkt->data);
  193. if (pkt->duration == 0xFFFFFFFFu)
  194. pkt->duration = AV_RB32(pkt->data + 8);
  195. done = 1;
  196. break;
  197. default:
  198. avio_skip(pb, size);
  199. break;
  200. }
  201. }
  202. return 0;
  203. }
  204. AVInputFormat ff_smush_demuxer = {
  205. .name = "smush",
  206. .long_name = NULL_IF_CONFIG_SMALL("LucasArts Smush"),
  207. .priv_data_size = sizeof(SMUSHContext),
  208. .read_probe = smush_read_probe,
  209. .read_header = smush_read_header,
  210. .read_packet = smush_read_packet,
  211. };