smush.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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) == MKTAG('A', 'N', 'I', 'M'))) {
  34. return AVPROBE_SCORE_MAX;
  35. }
  36. return 0;
  37. }
  38. static int smush_read_header(AVFormatContext *ctx)
  39. {
  40. SMUSHContext *smush = ctx->priv_data;
  41. AVIOContext *pb = ctx->pb;
  42. AVStream *vst, *ast;
  43. uint32_t magic, nframes, size, subversion, i;
  44. uint32_t width = 0, height = 0, got_audio = 0, read = 0;
  45. uint32_t sample_rate, channels, palette[256];
  46. magic = avio_rb32(pb);
  47. avio_skip(pb, 4); // skip movie size
  48. if (magic == MKBETAG('A', 'N', 'I', 'M')) {
  49. if (avio_rb32(pb) != MKBETAG('A', 'H', 'D', 'R'))
  50. return AVERROR_INVALIDDATA;
  51. size = avio_rb32(pb);
  52. if (size < 3 * 256 + 6)
  53. return AVERROR_INVALIDDATA;
  54. smush->version = 0;
  55. subversion = avio_rl16(pb);
  56. nframes = avio_rl16(pb);
  57. avio_skip(pb, 2); // skip pad
  58. for (i = 0; i < 256; i++)
  59. palette[i] = avio_rb24(pb);
  60. avio_skip(pb, size - (3 * 256 + 6));
  61. } else if (magic == MKBETAG('S', 'A', 'N', 'M') ) {
  62. if (avio_rb32(pb) != MKBETAG('S', 'H', 'D', 'R'))
  63. return AVERROR_INVALIDDATA;
  64. size = avio_rb32(pb);
  65. if (size < 14)
  66. return AVERROR_INVALIDDATA;
  67. smush->version = 1;
  68. subversion = avio_rl16(pb);
  69. nframes = avio_rl32(pb);
  70. avio_skip(pb, 2); // skip pad
  71. width = avio_rl16(pb);
  72. height = avio_rl16(pb);
  73. avio_skip(pb, 2); // skip pad
  74. avio_skip(pb, size - 14);
  75. if (avio_rb32(pb) != MKBETAG('F', 'L', 'H', 'D'))
  76. return AVERROR_INVALIDDATA;
  77. size = avio_rb32(pb);
  78. while (!got_audio && ((read + 8) < size)) {
  79. uint32_t sig, chunk_size;
  80. if (url_feof(pb))
  81. return AVERROR_EOF;
  82. sig = avio_rb32(pb);
  83. chunk_size = avio_rb32(pb);
  84. read += 8;
  85. switch (sig) {
  86. case MKBETAG('W', 'a', 'v', 'e'):
  87. got_audio = 1;
  88. sample_rate = avio_rl32(pb);
  89. channels = avio_rl32(pb);
  90. avio_skip(pb, chunk_size - 8);
  91. read += chunk_size;
  92. break;
  93. case MKBETAG('B', 'l', '1', '6'):
  94. case MKBETAG('A', 'N', 'N', 'O'):
  95. avio_skip(pb, chunk_size);
  96. read += chunk_size;
  97. break;
  98. default:
  99. return AVERROR_INVALIDDATA;
  100. break;
  101. }
  102. }
  103. avio_skip(pb, size - read);
  104. } else {
  105. av_log(ctx, AV_LOG_ERROR, "Wrong magic\n");
  106. return AVERROR_INVALIDDATA;
  107. }
  108. vst = avformat_new_stream(ctx, 0);
  109. if (!vst)
  110. return AVERROR(ENOMEM);
  111. smush->video_stream_index = vst->index;
  112. vst->start_time = 0;
  113. vst->duration =
  114. vst->nb_frames = nframes;
  115. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  116. vst->codec->codec_id = AV_CODEC_ID_SANM;
  117. vst->codec->codec_tag = 0;
  118. vst->codec->width = width;
  119. vst->codec->height = height;
  120. avpriv_set_pts_info(vst, 64, 66667, 1000000);
  121. if (!smush->version) {
  122. vst->codec->extradata = av_malloc(1024 + 2 + FF_INPUT_BUFFER_PADDING_SIZE);
  123. if (!vst->codec->extradata)
  124. return AVERROR(ENOMEM);
  125. vst->codec->extradata_size = 1024 + 2;
  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) < 0)
  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. };