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.

388 lines
13KB

  1. /*
  2. * Fraps FPS1 decoder
  3. * Copyright (c) 2005 Roine Gustafsson
  4. * Copyright (c) 2006 Konstantin Shishkov
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * Lossless Fraps 'FPS1' decoder
  25. * @author Roine Gustafsson (roine at users sf net)
  26. * @author Konstantin Shishkov
  27. *
  28. * Codec algorithm for version 0 is taken from Transcode <www.transcoding.org>
  29. *
  30. * Version 2 files support by Konstantin Shishkov
  31. */
  32. #include "avcodec.h"
  33. #include "get_bits.h"
  34. #include "huffman.h"
  35. #include "bytestream.h"
  36. #include "dsputil.h"
  37. #define FPS_TAG MKTAG('F', 'P', 'S', 'x')
  38. /**
  39. * local variable storage
  40. */
  41. typedef struct FrapsContext {
  42. AVCodecContext *avctx;
  43. AVFrame frame;
  44. uint8_t *tmpbuf;
  45. int tmpbuf_size;
  46. DSPContext dsp;
  47. } FrapsContext;
  48. /**
  49. * initializes decoder
  50. * @param avctx codec context
  51. * @return 0 on success or negative if fails
  52. */
  53. static av_cold int decode_init(AVCodecContext *avctx)
  54. {
  55. FrapsContext * const s = avctx->priv_data;
  56. avctx->coded_frame = &s->frame;
  57. avctx->pix_fmt = AV_PIX_FMT_NONE; /* set in decode_frame */
  58. s->avctx = avctx;
  59. s->tmpbuf = NULL;
  60. ff_dsputil_init(&s->dsp, avctx);
  61. return 0;
  62. }
  63. /**
  64. * Comparator - our nodes should ascend by count
  65. * but with preserved symbol order
  66. */
  67. static int huff_cmp(const void *va, const void *vb)
  68. {
  69. const Node *a = va, *b = vb;
  70. return (a->count - b->count)*256 + a->sym - b->sym;
  71. }
  72. /**
  73. * decode Fraps v2 packed plane
  74. */
  75. static int fraps2_decode_plane(FrapsContext *s, uint8_t *dst, int stride, int w,
  76. int h, const uint8_t *src, int size, int Uoff,
  77. const int step)
  78. {
  79. int i, j, ret;
  80. GetBitContext gb;
  81. VLC vlc;
  82. Node nodes[512];
  83. for (i = 0; i < 256; i++)
  84. nodes[i].count = bytestream_get_le32(&src);
  85. size -= 1024;
  86. if ((ret = ff_huff_build_tree(s->avctx, &vlc, 256, nodes, huff_cmp,
  87. FF_HUFFMAN_FLAG_ZERO_COUNT)) < 0)
  88. return ret;
  89. /* we have built Huffman table and are ready to decode plane */
  90. /* convert bits so they may be used by standard bitreader */
  91. s->dsp.bswap_buf((uint32_t *)s->tmpbuf, (const uint32_t *)src, size >> 2);
  92. init_get_bits(&gb, s->tmpbuf, size * 8);
  93. for (j = 0; j < h; j++) {
  94. for (i = 0; i < w*step; i += step) {
  95. dst[i] = get_vlc2(&gb, vlc.table, 9, 3);
  96. /* lines are stored as deltas between previous lines
  97. * and we need to add 0x80 to the first lines of chroma planes
  98. */
  99. if (j)
  100. dst[i] += dst[i - stride];
  101. else if (Uoff)
  102. dst[i] += 0x80;
  103. if (get_bits_left(&gb) < 0) {
  104. ff_free_vlc(&vlc);
  105. return AVERROR_INVALIDDATA;
  106. }
  107. }
  108. dst += stride;
  109. }
  110. ff_free_vlc(&vlc);
  111. return 0;
  112. }
  113. static int decode_frame(AVCodecContext *avctx,
  114. void *data, int *got_frame,
  115. AVPacket *avpkt)
  116. {
  117. FrapsContext * const s = avctx->priv_data;
  118. const uint8_t *buf = avpkt->data;
  119. int buf_size = avpkt->size;
  120. AVFrame *frame = data;
  121. AVFrame * const f = &s->frame;
  122. uint32_t header;
  123. unsigned int version,header_size;
  124. unsigned int x, y;
  125. const uint32_t *buf32;
  126. uint32_t *luma1,*luma2,*cb,*cr;
  127. uint32_t offs[4];
  128. int i, j, ret, is_chroma, planes;
  129. enum AVPixelFormat pix_fmt;
  130. header = AV_RL32(buf);
  131. version = header & 0xff;
  132. header_size = (header & (1<<30))? 8 : 4; /* bit 30 means pad to 8 bytes */
  133. if (version > 5) {
  134. av_log(avctx, AV_LOG_ERROR,
  135. "This file is encoded with Fraps version %d. " \
  136. "This codec can only decode versions <= 5.\n", version);
  137. return AVERROR_PATCHWELCOME;
  138. }
  139. buf += 4;
  140. if (header_size == 8)
  141. buf += 4;
  142. pix_fmt = version & 1 ? AV_PIX_FMT_BGR24 : AV_PIX_FMT_YUVJ420P;
  143. if (avctx->pix_fmt != pix_fmt && f->data[0]) {
  144. avctx->release_buffer(avctx, f);
  145. }
  146. avctx->pix_fmt = pix_fmt;
  147. switch (version) {
  148. case 0:
  149. default:
  150. /* Fraps v0 is a reordered YUV420 */
  151. if ((buf_size != avctx->width * avctx->height * 3 / 2 + header_size) &&
  152. (buf_size != header_size)) {
  153. av_log(avctx, AV_LOG_ERROR,
  154. "Invalid frame length %d (should be %d)\n",
  155. buf_size,
  156. avctx->width * avctx->height * 3 / 2 + header_size);
  157. return AVERROR_INVALIDDATA;
  158. }
  159. if (((avctx->width % 8) != 0) || ((avctx->height % 2) != 0)) {
  160. av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n",
  161. avctx->width, avctx->height);
  162. return AVERROR_INVALIDDATA;
  163. }
  164. f->reference = 1;
  165. f->buffer_hints = FF_BUFFER_HINTS_VALID |
  166. FF_BUFFER_HINTS_PRESERVE |
  167. FF_BUFFER_HINTS_REUSABLE;
  168. if ((ret = avctx->reget_buffer(avctx, f)) < 0) {
  169. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  170. return ret;
  171. }
  172. /* bit 31 means same as previous pic */
  173. f->pict_type = (header & (1U << 31)) ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
  174. f->key_frame = f->pict_type == AV_PICTURE_TYPE_I;
  175. if (f->pict_type == AV_PICTURE_TYPE_I) {
  176. buf32 = (const uint32_t*)buf;
  177. for (y = 0; y < avctx->height / 2; y++) {
  178. luma1 = (uint32_t*)&f->data[0][ y * 2 * f->linesize[0]];
  179. luma2 = (uint32_t*)&f->data[0][(y * 2 + 1) * f->linesize[0]];
  180. cr = (uint32_t*)&f->data[1][ y * f->linesize[1]];
  181. cb = (uint32_t*)&f->data[2][ y * f->linesize[2]];
  182. for (x = 0; x < avctx->width; x += 8) {
  183. *(luma1++) = *(buf32++);
  184. *(luma1++) = *(buf32++);
  185. *(luma2++) = *(buf32++);
  186. *(luma2++) = *(buf32++);
  187. *(cr++) = *(buf32++);
  188. *(cb++) = *(buf32++);
  189. }
  190. }
  191. }
  192. break;
  193. case 1:
  194. /* Fraps v1 is an upside-down BGR24 */
  195. if ((buf_size != avctx->width * avctx->height * 3 + header_size) &&
  196. (buf_size != header_size) ) {
  197. av_log(avctx, AV_LOG_ERROR,
  198. "Invalid frame length %d (should be %d)\n",
  199. buf_size, avctx->width * avctx->height * 3 + header_size);
  200. return AVERROR_INVALIDDATA;
  201. }
  202. f->reference = 1;
  203. f->buffer_hints = FF_BUFFER_HINTS_VALID |
  204. FF_BUFFER_HINTS_PRESERVE |
  205. FF_BUFFER_HINTS_REUSABLE;
  206. if ((ret = avctx->reget_buffer(avctx, f)) < 0) {
  207. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  208. return ret;
  209. }
  210. /* bit 31 means same as previous pic */
  211. f->pict_type = (header & (1U<<31))? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
  212. f->key_frame = f->pict_type == AV_PICTURE_TYPE_I;
  213. if (f->pict_type == AV_PICTURE_TYPE_I) {
  214. for (y = 0; y<avctx->height; y++)
  215. memcpy(&f->data[0][(avctx->height - y) * f->linesize[0]],
  216. &buf[y * avctx->width * 3],
  217. 3 * avctx->width);
  218. }
  219. break;
  220. case 2:
  221. case 4:
  222. /**
  223. * Fraps v2 is Huffman-coded YUV420 planes
  224. * Fraps v4 is virtually the same
  225. */
  226. planes = 3;
  227. f->reference = 1;
  228. f->buffer_hints = FF_BUFFER_HINTS_VALID |
  229. FF_BUFFER_HINTS_PRESERVE |
  230. FF_BUFFER_HINTS_REUSABLE;
  231. if ((ret = avctx->reget_buffer(avctx, f)) < 0) {
  232. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  233. return ret;
  234. }
  235. /* skip frame */
  236. if (buf_size == 8) {
  237. f->pict_type = AV_PICTURE_TYPE_P;
  238. f->key_frame = 0;
  239. break;
  240. }
  241. f->pict_type = AV_PICTURE_TYPE_I;
  242. f->key_frame = 1;
  243. if ((AV_RL32(buf) != FPS_TAG) || (buf_size < (planes * 1024 + 24))) {
  244. av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
  245. return AVERROR_INVALIDDATA;
  246. }
  247. for (i = 0; i < planes; i++) {
  248. offs[i] = AV_RL32(buf + 4 + i * 4);
  249. if (offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
  250. av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
  251. return AVERROR_INVALIDDATA;
  252. }
  253. }
  254. offs[planes] = buf_size;
  255. for (i = 0; i < planes; i++) {
  256. is_chroma = !!i;
  257. av_fast_padded_malloc(&s->tmpbuf, &s->tmpbuf_size,
  258. offs[i + 1] - offs[i] - 1024);
  259. if (!s->tmpbuf)
  260. return AVERROR(ENOMEM);
  261. if ((ret = fraps2_decode_plane(s, f->data[i], f->linesize[i],
  262. avctx->width >> is_chroma,
  263. avctx->height >> is_chroma,
  264. buf + offs[i], offs[i + 1] - offs[i],
  265. is_chroma, 1)) < 0) {
  266. av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
  267. return ret;
  268. }
  269. }
  270. break;
  271. case 3:
  272. case 5:
  273. /* Virtually the same as version 4, but is for RGB24 */
  274. planes = 3;
  275. f->reference = 1;
  276. f->buffer_hints = FF_BUFFER_HINTS_VALID |
  277. FF_BUFFER_HINTS_PRESERVE |
  278. FF_BUFFER_HINTS_REUSABLE;
  279. if ((ret = avctx->reget_buffer(avctx, f)) < 0) {
  280. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  281. return ret;
  282. }
  283. /* skip frame */
  284. if (buf_size == 8) {
  285. f->pict_type = AV_PICTURE_TYPE_P;
  286. f->key_frame = 0;
  287. break;
  288. }
  289. f->pict_type = AV_PICTURE_TYPE_I;
  290. f->key_frame = 1;
  291. if ((AV_RL32(buf) != FPS_TAG)||(buf_size < (planes*1024 + 24))) {
  292. av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
  293. return AVERROR_INVALIDDATA;
  294. }
  295. for (i = 0; i < planes; i++) {
  296. offs[i] = AV_RL32(buf + 4 + i * 4);
  297. if (offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
  298. av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
  299. return AVERROR_INVALIDDATA;
  300. }
  301. }
  302. offs[planes] = buf_size;
  303. for (i = 0; i < planes; i++) {
  304. av_fast_padded_malloc(&s->tmpbuf, &s->tmpbuf_size,
  305. offs[i + 1] - offs[i] - 1024);
  306. if (!s->tmpbuf)
  307. return AVERROR(ENOMEM);
  308. if ((ret = fraps2_decode_plane(s, f->data[0] + i + (f->linesize[0] * (avctx->height - 1)),
  309. -f->linesize[0], avctx->width, avctx->height,
  310. buf + offs[i], offs[i + 1] - offs[i], 0, 3)) < 0) {
  311. av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
  312. return ret;
  313. }
  314. }
  315. // convert pseudo-YUV into real RGB
  316. for (j = 0; j < avctx->height; j++) {
  317. for (i = 0; i < avctx->width; i++) {
  318. f->data[0][0 + i*3 + j*f->linesize[0]] += f->data[0][1 + i*3 + j*f->linesize[0]];
  319. f->data[0][2 + i*3 + j*f->linesize[0]] += f->data[0][1 + i*3 + j*f->linesize[0]];
  320. }
  321. }
  322. break;
  323. }
  324. *frame = *f;
  325. *got_frame = 1;
  326. return buf_size;
  327. }
  328. /**
  329. * closes decoder
  330. * @param avctx codec context
  331. * @return 0 on success or negative if fails
  332. */
  333. static av_cold int decode_end(AVCodecContext *avctx)
  334. {
  335. FrapsContext *s = (FrapsContext*)avctx->priv_data;
  336. if (s->frame.data[0])
  337. avctx->release_buffer(avctx, &s->frame);
  338. av_freep(&s->tmpbuf);
  339. return 0;
  340. }
  341. AVCodec ff_fraps_decoder = {
  342. .name = "fraps",
  343. .type = AVMEDIA_TYPE_VIDEO,
  344. .id = AV_CODEC_ID_FRAPS,
  345. .priv_data_size = sizeof(FrapsContext),
  346. .init = decode_init,
  347. .close = decode_end,
  348. .decode = decode_frame,
  349. .capabilities = CODEC_CAP_DR1,
  350. .long_name = NULL_IF_CONFIG_SMALL("Fraps"),
  351. };