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.

326 lines
9.8KB

  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 fraps.c
  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 "bitstream.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. DSPContext dsp;
  46. } FrapsContext;
  47. /**
  48. * initializes decoder
  49. * @param avctx codec context
  50. * @return 0 on success or negative if fails
  51. */
  52. static int decode_init(AVCodecContext *avctx)
  53. {
  54. FrapsContext * const s = avctx->priv_data;
  55. avctx->coded_frame = (AVFrame*)&s->frame;
  56. avctx->pix_fmt= PIX_FMT_NONE; /* set in decode_frame */
  57. s->avctx = avctx;
  58. s->frame.data[0] = NULL;
  59. s->tmpbuf = NULL;
  60. 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. const Node *a = va, *b = vb;
  69. return (a->count - b->count)*256 + a->sym - b->sym;
  70. }
  71. /**
  72. * decode Fraps v2 packed plane
  73. */
  74. static int fraps2_decode_plane(FrapsContext *s, uint8_t *dst, int stride, int w,
  75. int h, const uint8_t *src, int size, int Uoff)
  76. {
  77. int i, j;
  78. GetBitContext gb;
  79. VLC vlc;
  80. Node nodes[512];
  81. for(i = 0; i < 256; i++)
  82. nodes[i].count = bytestream_get_le32(&src);
  83. size -= 1024;
  84. if (ff_huff_build_tree(s->avctx, &vlc, 256, nodes, huff_cmp, 0) < 0)
  85. return -1;
  86. /* we have built Huffman table and are ready to decode plane */
  87. /* convert bits so they may be used by standard bitreader */
  88. s->dsp.bswap_buf(s->tmpbuf, src, size >> 2);
  89. init_get_bits(&gb, s->tmpbuf, size * 8);
  90. for(j = 0; j < h; j++){
  91. for(i = 0; i < w; i++){
  92. dst[i] = get_vlc2(&gb, vlc.table, 9, 3);
  93. /* lines are stored as deltas between previous lines
  94. * and we need to add 0x80 to the first lines of chroma planes
  95. */
  96. if(j) dst[i] += dst[i - stride];
  97. else if(Uoff) dst[i] += 0x80;
  98. }
  99. dst += stride;
  100. }
  101. free_vlc(&vlc);
  102. return 0;
  103. }
  104. /**
  105. * decode a frame
  106. * @param avctx codec context
  107. * @param data output AVFrame
  108. * @param data_size size of output data or 0 if no picture is returned
  109. * @param buf input data frame
  110. * @param buf_size size of input data frame
  111. * @return number of consumed bytes on success or negative if decode fails
  112. */
  113. static int decode_frame(AVCodecContext *avctx,
  114. void *data, int *data_size,
  115. const uint8_t *buf, int buf_size)
  116. {
  117. FrapsContext * const s = avctx->priv_data;
  118. AVFrame *frame = data;
  119. AVFrame * const f = (AVFrame*)&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, is_chroma, planes;
  127. header = AV_RL32(buf);
  128. version = header & 0xff;
  129. header_size = (header & (1<<30))? 8 : 4; /* bit 30 means pad to 8 bytes */
  130. if (version > 2 && version != 4) {
  131. av_log(avctx, AV_LOG_ERROR,
  132. "This file is encoded with Fraps version %d. " \
  133. "This codec can only decode version 0, 1, 2 and 4.\n", version);
  134. return -1;
  135. }
  136. buf+=4;
  137. if (header_size == 8)
  138. buf+=4;
  139. switch(version) {
  140. case 0:
  141. default:
  142. /* Fraps v0 is a reordered YUV420 */
  143. avctx->pix_fmt = PIX_FMT_YUV420P;
  144. if ( (buf_size != avctx->width*avctx->height*3/2+header_size) &&
  145. (buf_size != header_size) ) {
  146. av_log(avctx, AV_LOG_ERROR,
  147. "Invalid frame length %d (should be %d)\n",
  148. buf_size, avctx->width*avctx->height*3/2+header_size);
  149. return -1;
  150. }
  151. if (( (avctx->width % 8) != 0) || ( (avctx->height % 2) != 0 )) {
  152. av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n",
  153. avctx->width, avctx->height);
  154. return -1;
  155. }
  156. f->reference = 1;
  157. f->buffer_hints = FF_BUFFER_HINTS_VALID |
  158. FF_BUFFER_HINTS_PRESERVE |
  159. FF_BUFFER_HINTS_REUSABLE;
  160. if (avctx->reget_buffer(avctx, f)) {
  161. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  162. return -1;
  163. }
  164. /* bit 31 means same as previous pic */
  165. f->pict_type = (header & (1<<31))? FF_P_TYPE : FF_I_TYPE;
  166. f->key_frame = f->pict_type == FF_I_TYPE;
  167. if (f->pict_type == FF_I_TYPE) {
  168. buf32=(const uint32_t*)buf;
  169. for(y=0; y<avctx->height/2; y++){
  170. luma1=(uint32_t*)&f->data[0][ y*2*f->linesize[0] ];
  171. luma2=(uint32_t*)&f->data[0][ (y*2+1)*f->linesize[0] ];
  172. cr=(uint32_t*)&f->data[1][ y*f->linesize[1] ];
  173. cb=(uint32_t*)&f->data[2][ y*f->linesize[2] ];
  174. for(x=0; x<avctx->width; x+=8){
  175. *(luma1++) = *(buf32++);
  176. *(luma1++) = *(buf32++);
  177. *(luma2++) = *(buf32++);
  178. *(luma2++) = *(buf32++);
  179. *(cr++) = *(buf32++);
  180. *(cb++) = *(buf32++);
  181. }
  182. }
  183. }
  184. break;
  185. case 1:
  186. /* Fraps v1 is an upside-down BGR24 */
  187. avctx->pix_fmt = PIX_FMT_BGR24;
  188. if ( (buf_size != avctx->width*avctx->height*3+header_size) &&
  189. (buf_size != header_size) ) {
  190. av_log(avctx, AV_LOG_ERROR,
  191. "Invalid frame length %d (should be %d)\n",
  192. buf_size, avctx->width*avctx->height*3+header_size);
  193. return -1;
  194. }
  195. f->reference = 1;
  196. f->buffer_hints = FF_BUFFER_HINTS_VALID |
  197. FF_BUFFER_HINTS_PRESERVE |
  198. FF_BUFFER_HINTS_REUSABLE;
  199. if (avctx->reget_buffer(avctx, f)) {
  200. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  201. return -1;
  202. }
  203. /* bit 31 means same as previous pic */
  204. f->pict_type = (header & (1<<31))? FF_P_TYPE : FF_I_TYPE;
  205. f->key_frame = f->pict_type == FF_I_TYPE;
  206. if (f->pict_type == FF_I_TYPE) {
  207. for(y=0; y<avctx->height; y++)
  208. memcpy(&f->data[0][ (avctx->height-y)*f->linesize[0] ],
  209. &buf[y*avctx->width*3],
  210. f->linesize[0]);
  211. }
  212. break;
  213. case 2:
  214. case 4:
  215. /**
  216. * Fraps v2 is Huffman-coded YUV420 planes
  217. * Fraps v4 is virtually the same
  218. */
  219. avctx->pix_fmt = PIX_FMT_YUV420P;
  220. planes = 3;
  221. f->reference = 1;
  222. f->buffer_hints = FF_BUFFER_HINTS_VALID |
  223. FF_BUFFER_HINTS_PRESERVE |
  224. FF_BUFFER_HINTS_REUSABLE;
  225. if (avctx->reget_buffer(avctx, f)) {
  226. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  227. return -1;
  228. }
  229. /* skip frame */
  230. if(buf_size == 8) {
  231. f->pict_type = FF_P_TYPE;
  232. f->key_frame = 0;
  233. break;
  234. }
  235. f->pict_type = FF_I_TYPE;
  236. f->key_frame = 1;
  237. if ((AV_RL32(buf) != FPS_TAG)||(buf_size < (planes*1024 + 24))) {
  238. av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
  239. return -1;
  240. }
  241. for(i = 0; i < planes; i++) {
  242. offs[i] = AV_RL32(buf + 4 + i * 4);
  243. if(offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
  244. av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
  245. return -1;
  246. }
  247. }
  248. offs[planes] = buf_size;
  249. for(i = 0; i < planes; i++){
  250. is_chroma = !!i;
  251. s->tmpbuf = av_realloc(s->tmpbuf, offs[i + 1] - offs[i] - 1024 + FF_INPUT_BUFFER_PADDING_SIZE);
  252. if(fraps2_decode_plane(s, f->data[i], f->linesize[i], avctx->width >> is_chroma,
  253. avctx->height >> is_chroma, buf + offs[i], offs[i + 1] - offs[i], is_chroma) < 0) {
  254. av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
  255. return -1;
  256. }
  257. }
  258. break;
  259. }
  260. *frame = *f;
  261. *data_size = sizeof(AVFrame);
  262. return buf_size;
  263. }
  264. /**
  265. * closes decoder
  266. * @param avctx codec context
  267. * @return 0 on success or negative if fails
  268. */
  269. static int decode_end(AVCodecContext *avctx)
  270. {
  271. FrapsContext *s = (FrapsContext*)avctx->priv_data;
  272. if (s->frame.data[0])
  273. avctx->release_buffer(avctx, &s->frame);
  274. av_freep(&s->tmpbuf);
  275. return 0;
  276. }
  277. AVCodec fraps_decoder = {
  278. "fraps",
  279. CODEC_TYPE_VIDEO,
  280. CODEC_ID_FRAPS,
  281. sizeof(FrapsContext),
  282. decode_init,
  283. NULL,
  284. decode_end,
  285. decode_frame,
  286. CODEC_CAP_DR1,
  287. };