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.

391 lines
12KB

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