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.

327 lines
9.7KB

  1. /*
  2. * Fraps FPS1 decoder
  3. * Copyright (c) 2005 Roine Gustafsson
  4. * Copyright (c) 2006 Konstantin Shishkov
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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. #include "thread.h"
  38. #define FPS_TAG MKTAG('F', 'P', 'S', 'x')
  39. /**
  40. * local variable storage
  41. */
  42. typedef struct FrapsContext{
  43. AVCodecContext *avctx;
  44. AVFrame frame;
  45. uint8_t *tmpbuf;
  46. int tmpbuf_size;
  47. DSPContext dsp;
  48. } FrapsContext;
  49. /**
  50. * initializes decoder
  51. * @param avctx codec context
  52. * @return 0 on success or negative if fails
  53. */
  54. static av_cold int decode_init(AVCodecContext *avctx)
  55. {
  56. FrapsContext * const s = avctx->priv_data;
  57. avcodec_get_frame_defaults(&s->frame);
  58. avctx->coded_frame = &s->frame;
  59. s->avctx = avctx;
  60. s->tmpbuf = NULL;
  61. ff_dsputil_init(&s->dsp, avctx);
  62. return 0;
  63. }
  64. /**
  65. * Comparator - our nodes should ascend by count
  66. * but with preserved symbol order
  67. */
  68. static int huff_cmp(const void *va, const void *vb){
  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;
  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 (ff_huff_build_tree(s->avctx, &vlc, 256, nodes, huff_cmp,
  87. FF_HUFFMAN_FLAG_ZERO_COUNT) < 0)
  88. return -1;
  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) dst[i] += dst[i - stride];
  100. else if(Uoff) dst[i] += 0x80;
  101. if (get_bits_left(&gb) < 0) {
  102. ff_free_vlc(&vlc);
  103. return AVERROR_INVALIDDATA;
  104. }
  105. }
  106. dst += stride;
  107. }
  108. ff_free_vlc(&vlc);
  109. return 0;
  110. }
  111. static int decode_frame(AVCodecContext *avctx,
  112. void *data, int *data_size,
  113. AVPacket *avpkt)
  114. {
  115. const uint8_t *buf = avpkt->data;
  116. int buf_size = avpkt->size;
  117. FrapsContext * const s = avctx->priv_data;
  118. AVFrame *frame = data;
  119. AVFrame * const f = &s->frame;
  120. uint32_t header;
  121. unsigned int version,header_size;
  122. unsigned int x, y;
  123. const uint32_t *buf32;
  124. uint32_t *luma1,*luma2,*cb,*cr;
  125. uint32_t offs[4];
  126. int i, j, is_chroma;
  127. const int planes = 3;
  128. uint8_t *out;
  129. enum PixelFormat 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 -1;
  138. }
  139. buf += header_size;
  140. if (version < 2) {
  141. unsigned needed_size = avctx->width*avctx->height*3;
  142. if (version == 0) needed_size /= 2;
  143. needed_size += header_size;
  144. /* bit 31 means same as previous pic */
  145. if (header & (1U<<31)) {
  146. *data_size = 0;
  147. return buf_size;
  148. }
  149. if (buf_size != needed_size) {
  150. av_log(avctx, AV_LOG_ERROR,
  151. "Invalid frame length %d (should be %d)\n",
  152. buf_size, needed_size);
  153. return -1;
  154. }
  155. } else {
  156. /* skip frame */
  157. if (buf_size == 8) {
  158. *data_size = 0;
  159. return buf_size;
  160. }
  161. if (AV_RL32(buf) != FPS_TAG || buf_size < planes*1024 + 24) {
  162. av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
  163. return -1;
  164. }
  165. for(i = 0; i < planes; i++) {
  166. offs[i] = AV_RL32(buf + 4 + i * 4);
  167. if(offs[i] >= buf_size - header_size || (i && offs[i] <= offs[i - 1] + 1024)) {
  168. av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
  169. return -1;
  170. }
  171. }
  172. offs[planes] = buf_size - header_size;
  173. for(i = 0; i < planes; i++) {
  174. av_fast_padded_malloc(&s->tmpbuf, &s->tmpbuf_size, offs[i + 1] - offs[i] - 1024);
  175. if (!s->tmpbuf)
  176. return AVERROR(ENOMEM);
  177. }
  178. }
  179. if (f->data[0])
  180. ff_thread_release_buffer(avctx, f);
  181. f->pict_type = AV_PICTURE_TYPE_I;
  182. f->key_frame = 1;
  183. f->reference = 0;
  184. f->buffer_hints = FF_BUFFER_HINTS_VALID;
  185. pix_fmt = version & 1 ? PIX_FMT_BGR24 : PIX_FMT_YUVJ420P;
  186. if (avctx->pix_fmt != pix_fmt && f->data[0]) {
  187. avctx->release_buffer(avctx, f);
  188. }
  189. avctx->pix_fmt = pix_fmt;
  190. if (ff_thread_get_buffer(avctx, f)) {
  191. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  192. return -1;
  193. }
  194. switch(version) {
  195. case 0:
  196. default:
  197. /* Fraps v0 is a reordered YUV420 */
  198. if ( (avctx->width % 8) != 0 || (avctx->height % 2) != 0 ) {
  199. av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n",
  200. avctx->width, avctx->height);
  201. return -1;
  202. }
  203. buf32=(const uint32_t*)buf;
  204. for(y=0; y<avctx->height/2; y++){
  205. luma1=(uint32_t*)&f->data[0][ y*2*f->linesize[0] ];
  206. luma2=(uint32_t*)&f->data[0][ (y*2+1)*f->linesize[0] ];
  207. cr=(uint32_t*)&f->data[1][ y*f->linesize[1] ];
  208. cb=(uint32_t*)&f->data[2][ y*f->linesize[2] ];
  209. for(x=0; x<avctx->width; x+=8){
  210. *luma1++ = *buf32++;
  211. *luma1++ = *buf32++;
  212. *luma2++ = *buf32++;
  213. *luma2++ = *buf32++;
  214. *cr++ = *buf32++;
  215. *cb++ = *buf32++;
  216. }
  217. }
  218. break;
  219. case 1:
  220. /* Fraps v1 is an upside-down BGR24 */
  221. for(y=0; y<avctx->height; y++)
  222. memcpy(&f->data[0][ (avctx->height-y)*f->linesize[0] ],
  223. &buf[y*avctx->width*3],
  224. 3*avctx->width);
  225. break;
  226. case 2:
  227. case 4:
  228. /**
  229. * Fraps v2 is Huffman-coded YUV420 planes
  230. * Fraps v4 is virtually the same
  231. */
  232. for(i = 0; i < planes; i++){
  233. is_chroma = !!i;
  234. if(fraps2_decode_plane(s, f->data[i], f->linesize[i], avctx->width >> is_chroma,
  235. avctx->height >> is_chroma, buf + offs[i], offs[i + 1] - offs[i], is_chroma, 1) < 0) {
  236. av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
  237. return -1;
  238. }
  239. }
  240. break;
  241. case 3:
  242. case 5:
  243. /* Virtually the same as version 4, but is for RGB24 */
  244. for(i = 0; i < planes; i++){
  245. if(fraps2_decode_plane(s, f->data[0] + i + (f->linesize[0] * (avctx->height - 1)), -f->linesize[0],
  246. avctx->width, avctx->height, buf + offs[i], offs[i + 1] - offs[i], 0, 3) < 0) {
  247. av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
  248. return -1;
  249. }
  250. }
  251. out = f->data[0];
  252. // convert pseudo-YUV into real RGB
  253. for(j = 0; j < avctx->height; j++){
  254. uint8_t *line_end = out + 3*avctx->width;
  255. while (out < line_end) {
  256. out[0] += out[1];
  257. out[2] += out[1];
  258. out += 3;
  259. }
  260. out += f->linesize[0] - 3*avctx->width;
  261. }
  262. break;
  263. }
  264. *frame = *f;
  265. *data_size = sizeof(AVFrame);
  266. return buf_size;
  267. }
  268. /**
  269. * closes decoder
  270. * @param avctx codec context
  271. * @return 0 on success or negative if fails
  272. */
  273. static av_cold int decode_end(AVCodecContext *avctx)
  274. {
  275. FrapsContext *s = (FrapsContext*)avctx->priv_data;
  276. if (s->frame.data[0])
  277. avctx->release_buffer(avctx, &s->frame);
  278. av_freep(&s->tmpbuf);
  279. return 0;
  280. }
  281. AVCodec ff_fraps_decoder = {
  282. .name = "fraps",
  283. .type = AVMEDIA_TYPE_VIDEO,
  284. .id = AV_CODEC_ID_FRAPS,
  285. .priv_data_size = sizeof(FrapsContext),
  286. .init = decode_init,
  287. .close = decode_end,
  288. .decode = decode_frame,
  289. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
  290. .long_name = NULL_IF_CONFIG_SMALL("Fraps"),
  291. };