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.

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