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.

395 lines
11KB

  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. /**
  24. * @file fraps.c
  25. * Lossless Fraps 'FPS1' decoder
  26. * @author Roine Gustafsson <roine at users sf net>
  27. * @author Konstantin Shishkov
  28. *
  29. * Codec algorithm for version 0 is taken from Transcode <www.transcoding.org>
  30. *
  31. * Version 2 files support by Konstantin Shishkov
  32. */
  33. #include "avcodec.h"
  34. #include "bitstream.h"
  35. #include "dsputil.h"
  36. #define FPS_TAG MKTAG('F', 'P', 'S', 'x')
  37. /* symbol for Huffman tree node */
  38. #define HNODE -1
  39. /**
  40. * Huffman node
  41. * FIXME one day this should belong to one general framework
  42. */
  43. typedef struct Node{
  44. int16_t sym;
  45. int16_t n0;
  46. int count;
  47. }Node;
  48. /**
  49. * local variable storage
  50. */
  51. typedef struct FrapsContext{
  52. AVCodecContext *avctx;
  53. AVFrame frame;
  54. Node nodes[512];
  55. uint8_t *tmpbuf;
  56. DSPContext dsp;
  57. } FrapsContext;
  58. /**
  59. * initializes decoder
  60. * @param avctx codec context
  61. * @return 0 on success or negative if fails
  62. */
  63. static int decode_init(AVCodecContext *avctx)
  64. {
  65. FrapsContext * const s = avctx->priv_data;
  66. avctx->coded_frame = (AVFrame*)&s->frame;
  67. avctx->has_b_frames = 0;
  68. avctx->pix_fmt= PIX_FMT_NONE; /* set in decode_frame */
  69. s->avctx = avctx;
  70. s->frame.data[0] = NULL;
  71. s->tmpbuf = NULL;
  72. dsputil_init(&s->dsp, avctx);
  73. return 0;
  74. }
  75. /**
  76. * Comparator - our nodes should ascend by count
  77. * but with preserved symbol order
  78. */
  79. static int huff_cmp(const Node *a, const Node *b){
  80. return (a->count - b->count)*256 + a->sym - b->sym;
  81. }
  82. static void get_tree_codes(uint32_t *bits, int16_t *lens, uint8_t *xlat, Node *nodes, int node, uint32_t pfx, int pl, int *pos)
  83. {
  84. int s;
  85. s = nodes[node].sym;
  86. if(s != HNODE || !nodes[node].count){
  87. bits[*pos] = pfx;
  88. lens[*pos] = pl;
  89. xlat[*pos] = s;
  90. (*pos)++;
  91. }else{
  92. pfx <<= 1;
  93. pl++;
  94. get_tree_codes(bits, lens, xlat, nodes, nodes[node].n0, pfx, pl, pos);
  95. pfx |= 1;
  96. get_tree_codes(bits, lens, xlat, nodes, nodes[node].n0+1, pfx, pl, pos);
  97. }
  98. }
  99. static int build_huff_tree(VLC *vlc, Node *nodes, uint8_t *xlat)
  100. {
  101. uint32_t bits[256];
  102. int16_t lens[256];
  103. int pos = 0;
  104. get_tree_codes(bits, lens, xlat, nodes, 510, 0, 0, &pos);
  105. return init_vlc(vlc, 9, pos, lens, 2, 2, bits, 4, 4, 0);
  106. }
  107. /**
  108. * decode Fraps v2 packed plane
  109. */
  110. static int fraps2_decode_plane(FrapsContext *s, uint8_t *dst, int stride, int w,
  111. int h, uint8_t *src, int size, int Uoff)
  112. {
  113. int i, j;
  114. int cur_node;
  115. GetBitContext gb;
  116. VLC vlc;
  117. int64_t sum = 0;
  118. uint8_t recode[256];
  119. for(i = 0; i < 256; i++){
  120. s->nodes[i].sym = i;
  121. s->nodes[i].count = LE_32(src);
  122. s->nodes[i].n0 = -2;
  123. src += 4;
  124. sum += s->nodes[i].count;
  125. }
  126. size -= 1024;
  127. if(sum >> 31) {
  128. av_log(s->avctx, AV_LOG_ERROR, "Too high symbol frequencies. Tree construction is not possible\n");
  129. return -1;
  130. }
  131. qsort(s->nodes, 256, sizeof(Node), huff_cmp);
  132. cur_node = 256;
  133. for(i = 0; i < 511; i += 2){
  134. s->nodes[cur_node].sym = HNODE;
  135. s->nodes[cur_node].count = s->nodes[i].count + s->nodes[i+1].count;
  136. s->nodes[cur_node].n0 = i;
  137. for(j = cur_node; j > 0; j--){
  138. if(s->nodes[j].count >= s->nodes[j - 1].count) break;
  139. FFSWAP(Node, s->nodes[j], s->nodes[j - 1]);
  140. }
  141. cur_node++;
  142. }
  143. if(build_huff_tree(&vlc, s->nodes, recode) < 0){
  144. av_log(s->avctx, AV_LOG_ERROR, "Error building tree\n");
  145. return -1;
  146. }
  147. /* we have built Huffman table and are ready to decode plane */
  148. /* convert bits so they may be used by standard bitreader */
  149. s->dsp.bswap_buf(s->tmpbuf, src, size >> 2);
  150. init_get_bits(&gb, s->tmpbuf, size * 8);
  151. for(j = 0; j < h; j++){
  152. for(i = 0; i < w; i++){
  153. dst[i] = recode[get_vlc2(&gb, vlc.table, 9, 3)];
  154. /* lines are stored as deltas between previous lines
  155. * and we need to add 0x80 to the first lines of chroma planes
  156. */
  157. if(j) dst[i] += dst[i - stride];
  158. else if(Uoff) dst[i] += 0x80;
  159. }
  160. dst += stride;
  161. }
  162. free_vlc(&vlc);
  163. return 0;
  164. }
  165. /**
  166. * decode a frame
  167. * @param avctx codec context
  168. * @param data output AVFrame
  169. * @param data_size size of output data or 0 if no picture is returned
  170. * @param buf input data frame
  171. * @param buf_size size of input data frame
  172. * @return number of consumed bytes on success or negative if decode fails
  173. */
  174. static int decode_frame(AVCodecContext *avctx,
  175. void *data, int *data_size,
  176. uint8_t *buf, int buf_size)
  177. {
  178. FrapsContext * const s = avctx->priv_data;
  179. AVFrame *frame = data;
  180. AVFrame * const f = (AVFrame*)&s->frame;
  181. uint32_t header;
  182. unsigned int version,header_size;
  183. unsigned int x, y;
  184. uint32_t *buf32;
  185. uint32_t *luma1,*luma2,*cb,*cr;
  186. uint32_t offs[4];
  187. int i, is_chroma, planes;
  188. header = LE_32(buf);
  189. version = header & 0xff;
  190. header_size = (header & (1<<30))? 8 : 4; /* bit 30 means pad to 8 bytes */
  191. if (version > 2 && version != 4) {
  192. av_log(avctx, AV_LOG_ERROR,
  193. "This file is encoded with Fraps version %d. " \
  194. "This codec can only decode version 0, 1, 2 and 4.\n", version);
  195. return -1;
  196. }
  197. buf+=4;
  198. if (header_size == 8)
  199. buf+=4;
  200. switch(version) {
  201. case 0:
  202. default:
  203. /* Fraps v0 is a reordered YUV420 */
  204. avctx->pix_fmt = PIX_FMT_YUV420P;
  205. if ( (buf_size != avctx->width*avctx->height*3/2+header_size) &&
  206. (buf_size != header_size) ) {
  207. av_log(avctx, AV_LOG_ERROR,
  208. "Invalid frame length %d (should be %d)\n",
  209. buf_size, avctx->width*avctx->height*3/2+header_size);
  210. return -1;
  211. }
  212. if (( (avctx->width % 8) != 0) || ( (avctx->height % 2) != 0 )) {
  213. av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n",
  214. avctx->width, avctx->height);
  215. return -1;
  216. }
  217. f->reference = 1;
  218. f->buffer_hints = FF_BUFFER_HINTS_VALID |
  219. FF_BUFFER_HINTS_PRESERVE |
  220. FF_BUFFER_HINTS_REUSABLE;
  221. if (avctx->reget_buffer(avctx, f)) {
  222. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  223. return -1;
  224. }
  225. /* bit 31 means same as previous pic */
  226. f->pict_type = (header & (1<<31))? FF_P_TYPE : FF_I_TYPE;
  227. f->key_frame = f->pict_type == FF_I_TYPE;
  228. if (f->pict_type == FF_I_TYPE) {
  229. buf32=(uint32_t*)buf;
  230. for(y=0; y<avctx->height/2; y++){
  231. luma1=(uint32_t*)&f->data[0][ y*2*f->linesize[0] ];
  232. luma2=(uint32_t*)&f->data[0][ (y*2+1)*f->linesize[0] ];
  233. cr=(uint32_t*)&f->data[1][ y*f->linesize[1] ];
  234. cb=(uint32_t*)&f->data[2][ y*f->linesize[2] ];
  235. for(x=0; x<avctx->width; x+=8){
  236. *(luma1++) = *(buf32++);
  237. *(luma1++) = *(buf32++);
  238. *(luma2++) = *(buf32++);
  239. *(luma2++) = *(buf32++);
  240. *(cr++) = *(buf32++);
  241. *(cb++) = *(buf32++);
  242. }
  243. }
  244. }
  245. break;
  246. case 1:
  247. /* Fraps v1 is an upside-down BGR24 */
  248. avctx->pix_fmt = PIX_FMT_BGR24;
  249. if ( (buf_size != avctx->width*avctx->height*3+header_size) &&
  250. (buf_size != header_size) ) {
  251. av_log(avctx, AV_LOG_ERROR,
  252. "Invalid frame length %d (should be %d)\n",
  253. buf_size, avctx->width*avctx->height*3+header_size);
  254. return -1;
  255. }
  256. f->reference = 1;
  257. f->buffer_hints = FF_BUFFER_HINTS_VALID |
  258. FF_BUFFER_HINTS_PRESERVE |
  259. FF_BUFFER_HINTS_REUSABLE;
  260. if (avctx->reget_buffer(avctx, f)) {
  261. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  262. return -1;
  263. }
  264. /* bit 31 means same as previous pic */
  265. f->pict_type = (header & (1<<31))? FF_P_TYPE : FF_I_TYPE;
  266. f->key_frame = f->pict_type == FF_I_TYPE;
  267. if (f->pict_type == FF_I_TYPE) {
  268. for(y=0; y<avctx->height; y++)
  269. memcpy(&f->data[0][ (avctx->height-y)*f->linesize[0] ],
  270. &buf[y*avctx->width*3],
  271. f->linesize[0]);
  272. }
  273. break;
  274. case 2:
  275. case 4:
  276. /**
  277. * Fraps v2 is Huffman-coded YUV420 planes
  278. * Fraps v4 is virtually the same
  279. */
  280. avctx->pix_fmt = PIX_FMT_YUV420P;
  281. planes = 3;
  282. f->reference = 1;
  283. f->buffer_hints = FF_BUFFER_HINTS_VALID |
  284. FF_BUFFER_HINTS_PRESERVE |
  285. FF_BUFFER_HINTS_REUSABLE;
  286. if (avctx->reget_buffer(avctx, f)) {
  287. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  288. return -1;
  289. }
  290. /* skip frame */
  291. if(buf_size == 8) {
  292. f->pict_type = FF_P_TYPE;
  293. f->key_frame = 0;
  294. break;
  295. }
  296. f->pict_type = FF_I_TYPE;
  297. f->key_frame = 1;
  298. if ((LE_32(buf) != FPS_TAG)||(buf_size < (planes*1024 + 24))) {
  299. av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
  300. return -1;
  301. }
  302. for(i = 0; i < planes; i++) {
  303. offs[i] = LE_32(buf + 4 + i * 4);
  304. if(offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
  305. av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
  306. return -1;
  307. }
  308. }
  309. offs[planes] = buf_size;
  310. for(i = 0; i < planes; i++){
  311. is_chroma = !!i;
  312. s->tmpbuf = av_realloc(s->tmpbuf, offs[i + 1] - offs[i] - 1024 + FF_INPUT_BUFFER_PADDING_SIZE);
  313. if(fraps2_decode_plane(s, f->data[i], f->linesize[i], avctx->width >> is_chroma,
  314. avctx->height >> is_chroma, buf + offs[i], offs[i + 1] - offs[i], is_chroma) < 0) {
  315. av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
  316. return -1;
  317. }
  318. }
  319. break;
  320. }
  321. *frame = *f;
  322. *data_size = sizeof(AVFrame);
  323. return buf_size;
  324. }
  325. /**
  326. * closes decoder
  327. * @param avctx codec context
  328. * @return 0 on success or negative if fails
  329. */
  330. static int decode_end(AVCodecContext *avctx)
  331. {
  332. FrapsContext *s = (FrapsContext*)avctx->priv_data;
  333. if (s->frame.data[0])
  334. avctx->release_buffer(avctx, &s->frame);
  335. av_freep(&s->tmpbuf);
  336. return 0;
  337. }
  338. AVCodec fraps_decoder = {
  339. "fraps",
  340. CODEC_TYPE_VIDEO,
  341. CODEC_ID_FRAPS,
  342. sizeof(FrapsContext),
  343. decode_init,
  344. NULL,
  345. decode_end,
  346. decode_frame,
  347. CODEC_CAP_DR1,
  348. };