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.

360 lines
11KB

  1. /*
  2. * Smacker demuxer
  3. * Copyright (c) 2006 Konstantin Shishkov
  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. /*
  22. * Based on http://wiki.multimedia.cx/index.php?title=Smacker
  23. */
  24. #include <inttypes.h>
  25. #include "libavutil/channel_layout.h"
  26. #include "libavutil/intreadwrite.h"
  27. #include "avformat.h"
  28. #include "avio_internal.h"
  29. #include "internal.h"
  30. #define SMACKER_PAL 0x01
  31. #define SMACKER_FLAG_RING_FRAME 0x01
  32. enum SAudFlags {
  33. SMK_AUD_PACKED = 0x80,
  34. SMK_AUD_16BITS = 0x20,
  35. SMK_AUD_STEREO = 0x10,
  36. SMK_AUD_BINKAUD = 0x08,
  37. SMK_AUD_USEDCT = 0x04
  38. };
  39. typedef struct SmackerContext {
  40. uint32_t frames;
  41. /* frame info */
  42. uint32_t *frm_size;
  43. uint8_t *frm_flags;
  44. /* internal variables */
  45. int64_t next_frame_pos;
  46. int cur_frame;
  47. int videoindex;
  48. int indexes[7];
  49. /* current frame for demuxing */
  50. uint32_t frame_size;
  51. int flags;
  52. int next_audio_index;
  53. int new_palette;
  54. uint8_t pal[768];
  55. int64_t aud_pts[7];
  56. } SmackerContext;
  57. /* palette used in Smacker */
  58. static const uint8_t smk_pal[64] = {
  59. 0x00, 0x04, 0x08, 0x0C, 0x10, 0x14, 0x18, 0x1C,
  60. 0x20, 0x24, 0x28, 0x2C, 0x30, 0x34, 0x38, 0x3C,
  61. 0x41, 0x45, 0x49, 0x4D, 0x51, 0x55, 0x59, 0x5D,
  62. 0x61, 0x65, 0x69, 0x6D, 0x71, 0x75, 0x79, 0x7D,
  63. 0x82, 0x86, 0x8A, 0x8E, 0x92, 0x96, 0x9A, 0x9E,
  64. 0xA2, 0xA6, 0xAA, 0xAE, 0xB2, 0xB6, 0xBA, 0xBE,
  65. 0xC3, 0xC7, 0xCB, 0xCF, 0xD3, 0xD7, 0xDB, 0xDF,
  66. 0xE3, 0xE7, 0xEB, 0xEF, 0xF3, 0xF7, 0xFB, 0xFF
  67. };
  68. static int smacker_probe(const AVProbeData *p)
  69. {
  70. if ( AV_RL32(p->buf) != MKTAG('S', 'M', 'K', '2')
  71. && AV_RL32(p->buf) != MKTAG('S', 'M', 'K', '4'))
  72. return 0;
  73. if (AV_RL32(p->buf+4) > 32768U || AV_RL32(p->buf+8) > 32768U)
  74. return AVPROBE_SCORE_MAX/4;
  75. return AVPROBE_SCORE_MAX;
  76. }
  77. static int smacker_read_header(AVFormatContext *s)
  78. {
  79. AVIOContext *pb = s->pb;
  80. SmackerContext *smk = s->priv_data;
  81. AVStream *st;
  82. AVCodecParameters *par;
  83. uint32_t magic, width, height, flags, treesize;
  84. int i, ret, pts_inc;
  85. int tbase;
  86. /* read and check header */
  87. magic = avio_rl32(pb);
  88. if (magic != MKTAG('S', 'M', 'K', '2') && magic != MKTAG('S', 'M', 'K', '4'))
  89. return AVERROR_INVALIDDATA;
  90. width = avio_rl32(pb);
  91. height = avio_rl32(pb);
  92. smk->frames = avio_rl32(pb);
  93. pts_inc = avio_rl32(pb);
  94. if (pts_inc > INT_MAX / 100) {
  95. av_log(s, AV_LOG_ERROR, "pts_inc %d is too large\n", pts_inc);
  96. return AVERROR_INVALIDDATA;
  97. }
  98. flags = avio_rl32(pb);
  99. if (flags & SMACKER_FLAG_RING_FRAME)
  100. smk->frames++;
  101. if (smk->frames > 0xFFFFFF) {
  102. av_log(s, AV_LOG_ERROR, "Too many frames: %"PRIu32"\n", smk->frames);
  103. return AVERROR_INVALIDDATA;
  104. }
  105. avio_skip(pb, 28); /* Unused audio related data */
  106. treesize = avio_rl32(pb);
  107. if (treesize >= UINT_MAX/4) {
  108. // treesize + 16 must not overflow (this check is probably redundant)
  109. av_log(s, AV_LOG_ERROR, "treesize too large\n");
  110. return AVERROR_INVALIDDATA;
  111. }
  112. st = avformat_new_stream(s, NULL);
  113. if (!st)
  114. return AVERROR(ENOMEM);
  115. smk->videoindex = st->index;
  116. /* Smacker uses 100000 as internal timebase */
  117. if (pts_inc < 0)
  118. pts_inc = -pts_inc;
  119. else
  120. pts_inc *= 100;
  121. tbase = 100000;
  122. av_reduce(&tbase, &pts_inc, tbase, pts_inc, (1UL << 31) - 1);
  123. avpriv_set_pts_info(st, 33, pts_inc, tbase);
  124. st->duration = smk->frames;
  125. /* init video codec */
  126. par = st->codecpar;
  127. par->width = width;
  128. par->height = height;
  129. par->format = AV_PIX_FMT_PAL8;
  130. par->codec_type = AVMEDIA_TYPE_VIDEO;
  131. par->codec_id = AV_CODEC_ID_SMACKVIDEO;
  132. par->codec_tag = magic;
  133. if ((ret = ff_alloc_extradata(par, treesize + 16)) < 0) {
  134. av_log(s, AV_LOG_ERROR,
  135. "Cannot allocate %"PRIu32" bytes of extradata\n",
  136. treesize + 16);
  137. return ret;
  138. }
  139. if ((ret = ffio_read_size(pb, par->extradata, 16)) < 0)
  140. return ret;
  141. /* handle possible audio streams */
  142. for (i = 0; i < 7; i++) {
  143. uint32_t rate = avio_rl24(pb);
  144. uint8_t aflag = avio_r8(pb);
  145. smk->indexes[i] = -1;
  146. if (rate) {
  147. AVStream *ast = avformat_new_stream(s, NULL);
  148. AVCodecParameters *par;
  149. if (!ast)
  150. return AVERROR(ENOMEM);
  151. smk->indexes[i] = ast->index;
  152. par = ast->codecpar;
  153. par->codec_type = AVMEDIA_TYPE_AUDIO;
  154. if (aflag & SMK_AUD_BINKAUD) {
  155. par->codec_id = AV_CODEC_ID_BINKAUDIO_RDFT;
  156. } else if (aflag & SMK_AUD_USEDCT) {
  157. par->codec_id = AV_CODEC_ID_BINKAUDIO_DCT;
  158. } else if (aflag & SMK_AUD_PACKED) {
  159. par->codec_id = AV_CODEC_ID_SMACKAUDIO;
  160. par->codec_tag = MKTAG('S', 'M', 'K', 'A');
  161. } else {
  162. par->codec_id = AV_CODEC_ID_PCM_U8;
  163. }
  164. if (aflag & SMK_AUD_STEREO) {
  165. par->channels = 2;
  166. par->channel_layout = AV_CH_LAYOUT_STEREO;
  167. } else {
  168. par->channels = 1;
  169. par->channel_layout = AV_CH_LAYOUT_MONO;
  170. }
  171. par->sample_rate = rate;
  172. par->bits_per_coded_sample = (aflag & SMK_AUD_16BITS) ? 16 : 8;
  173. if (par->bits_per_coded_sample == 16 &&
  174. par->codec_id == AV_CODEC_ID_PCM_U8)
  175. par->codec_id = AV_CODEC_ID_PCM_S16LE;
  176. avpriv_set_pts_info(ast, 64, 1, par->sample_rate * par->channels
  177. * par->bits_per_coded_sample / 8);
  178. }
  179. }
  180. avio_rl32(pb); /* padding */
  181. /* setup data */
  182. st->priv_data = av_malloc_array(smk->frames, sizeof(*smk->frm_size) +
  183. sizeof(*smk->frm_flags));
  184. if (!st->priv_data)
  185. return AVERROR(ENOMEM);
  186. smk->frm_size = st->priv_data;
  187. smk->frm_flags = (void*)(smk->frm_size + smk->frames);
  188. /* read frame info */
  189. for (i = 0; i < smk->frames; i++) {
  190. smk->frm_size[i] = avio_rl32(pb);
  191. }
  192. if ((ret = ffio_read_size(pb, smk->frm_flags, smk->frames)) < 0 ||
  193. /* load trees to extradata, they will be unpacked by decoder */
  194. (ret = ffio_read_size(pb, par->extradata + 16,
  195. par->extradata_size - 16)) < 0) {
  196. return ret;
  197. }
  198. return 0;
  199. }
  200. static int smacker_read_packet(AVFormatContext *s, AVPacket *pkt)
  201. {
  202. SmackerContext *smk = s->priv_data;
  203. int flags;
  204. int ret;
  205. if (avio_feof(s->pb) || smk->cur_frame >= smk->frames)
  206. return AVERROR_EOF;
  207. /* if we demuxed all streams, pass another frame */
  208. if (!smk->next_audio_index) {
  209. smk->frame_size = smk->frm_size[smk->cur_frame] & (~3);
  210. smk->next_frame_pos = avio_tell(s->pb) + smk->frame_size;
  211. flags = smk->frm_flags[smk->cur_frame];
  212. smk->flags = flags >> 1;
  213. /* handle palette change event */
  214. if (flags & SMACKER_PAL) {
  215. int size, sz, t, off, j, pos;
  216. uint8_t *pal = smk->pal;
  217. uint8_t oldpal[768];
  218. memcpy(oldpal, pal, 768);
  219. size = avio_r8(s->pb);
  220. size = size * 4;
  221. if (size > smk->frame_size) {
  222. ret = AVERROR_INVALIDDATA;
  223. goto next_frame;
  224. }
  225. smk->frame_size -= size--;
  226. sz = 0;
  227. pos = avio_tell(s->pb) + size;
  228. while (sz < 256) {
  229. t = avio_r8(s->pb);
  230. if (t & 0x80) { /* skip palette entries */
  231. sz += (t & 0x7F) + 1;
  232. pal += ((t & 0x7F) + 1) * 3;
  233. } else if (t & 0x40) { /* copy with offset */
  234. off = avio_r8(s->pb);
  235. j = (t & 0x3F) + 1;
  236. if (off + j > 0x100) {
  237. av_log(s, AV_LOG_ERROR,
  238. "Invalid palette update, offset=%d length=%d extends beyond palette size\n",
  239. off, j);
  240. ret = AVERROR_INVALIDDATA;
  241. goto next_frame;
  242. }
  243. off *= 3;
  244. while (j-- && sz < 256) {
  245. *pal++ = oldpal[off + 0];
  246. *pal++ = oldpal[off + 1];
  247. *pal++ = oldpal[off + 2];
  248. sz++;
  249. off += 3;
  250. }
  251. } else { /* new entries */
  252. *pal++ = smk_pal[t];
  253. *pal++ = smk_pal[avio_r8(s->pb) & 0x3F];
  254. *pal++ = smk_pal[avio_r8(s->pb) & 0x3F];
  255. sz++;
  256. }
  257. }
  258. avio_seek(s->pb, pos, 0);
  259. smk->new_palette = 1;
  260. }
  261. }
  262. for (int i = smk->next_audio_index; i < 7; i++) {
  263. if (smk->flags & (1 << i)) {
  264. uint32_t size;
  265. size = avio_rl32(s->pb);
  266. if ((int)size < 8 || size > smk->frame_size) {
  267. av_log(s, AV_LOG_ERROR, "Invalid audio part size\n");
  268. ret = AVERROR_INVALIDDATA;
  269. goto next_frame;
  270. }
  271. smk->frame_size -= size;
  272. size -= 4;
  273. if (smk->indexes[i] < 0) {
  274. avio_skip(s->pb, size);
  275. continue;
  276. }
  277. if ((ret = av_get_packet(s->pb, pkt, size)) != size) {
  278. ret = ret < 0 ? ret : AVERROR_INVALIDDATA;
  279. goto next_frame;
  280. }
  281. pkt->stream_index = smk->indexes[i];
  282. pkt->pts = smk->aud_pts[i];
  283. smk->aud_pts[i] += AV_RL32(pkt->data);
  284. smk->next_audio_index = i + 1;
  285. return 0;
  286. }
  287. }
  288. if (smk->frame_size >= INT_MAX/2) {
  289. ret = AVERROR_INVALIDDATA;
  290. goto next_frame;
  291. }
  292. if ((ret = av_new_packet(pkt, smk->frame_size + 769)) < 0)
  293. goto next_frame;
  294. flags = smk->new_palette;
  295. if (smk->frm_size[smk->cur_frame] & 1)
  296. flags |= 2;
  297. pkt->data[0] = flags;
  298. memcpy(pkt->data + 1, smk->pal, 768);
  299. ret = ffio_read_size(s->pb, pkt->data + 769, smk->frame_size);
  300. if (ret < 0)
  301. goto next_frame;
  302. pkt->stream_index = smk->videoindex;
  303. pkt->pts = smk->cur_frame;
  304. smk->next_audio_index = 0;
  305. smk->new_palette = 0;
  306. smk->cur_frame++;
  307. return 0;
  308. next_frame:
  309. avio_seek(s->pb, smk->next_frame_pos, SEEK_SET);
  310. smk->next_audio_index = 0;
  311. smk->cur_frame++;
  312. return ret;
  313. }
  314. AVInputFormat ff_smacker_demuxer = {
  315. .name = "smk",
  316. .long_name = NULL_IF_CONFIG_SMALL("Smacker"),
  317. .priv_data_size = sizeof(SmackerContext),
  318. .read_probe = smacker_probe,
  319. .read_header = smacker_read_header,
  320. .read_packet = smacker_read_packet,
  321. };