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.

399 lines
12KB

  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 = AV_RL32(src);
  122. s->nodes[i].n0 = -2;
  123. if(s->nodes[i].count < 0) {
  124. av_log(s->avctx, AV_LOG_ERROR, "Symbol count < 0\n");
  125. return -1;
  126. }
  127. src += 4;
  128. sum += s->nodes[i].count;
  129. }
  130. size -= 1024;
  131. if(sum >> 31) {
  132. av_log(s->avctx, AV_LOG_ERROR, "Too high symbol frequencies. Tree construction is not possible\n");
  133. return -1;
  134. }
  135. qsort(s->nodes, 256, sizeof(Node), huff_cmp);
  136. cur_node = 256;
  137. for(i = 0; i < 511; i += 2){
  138. s->nodes[cur_node].sym = HNODE;
  139. s->nodes[cur_node].count = s->nodes[i].count + s->nodes[i+1].count;
  140. s->nodes[cur_node].n0 = i;
  141. for(j = cur_node; j > 0; j--){
  142. if(s->nodes[j].count >= s->nodes[j - 1].count) break;
  143. FFSWAP(Node, s->nodes[j], s->nodes[j - 1]);
  144. }
  145. cur_node++;
  146. }
  147. if(build_huff_tree(&vlc, s->nodes, recode) < 0){
  148. av_log(s->avctx, AV_LOG_ERROR, "Error building tree\n");
  149. return -1;
  150. }
  151. /* we have built Huffman table and are ready to decode plane */
  152. /* convert bits so they may be used by standard bitreader */
  153. s->dsp.bswap_buf(s->tmpbuf, src, size >> 2);
  154. init_get_bits(&gb, s->tmpbuf, size * 8);
  155. for(j = 0; j < h; j++){
  156. for(i = 0; i < w; i++){
  157. dst[i] = recode[get_vlc2(&gb, vlc.table, 9, 3)];
  158. /* lines are stored as deltas between previous lines
  159. * and we need to add 0x80 to the first lines of chroma planes
  160. */
  161. if(j) dst[i] += dst[i - stride];
  162. else if(Uoff) dst[i] += 0x80;
  163. }
  164. dst += stride;
  165. }
  166. free_vlc(&vlc);
  167. return 0;
  168. }
  169. /**
  170. * decode a frame
  171. * @param avctx codec context
  172. * @param data output AVFrame
  173. * @param data_size size of output data or 0 if no picture is returned
  174. * @param buf input data frame
  175. * @param buf_size size of input data frame
  176. * @return number of consumed bytes on success or negative if decode fails
  177. */
  178. static int decode_frame(AVCodecContext *avctx,
  179. void *data, int *data_size,
  180. uint8_t *buf, int buf_size)
  181. {
  182. FrapsContext * const s = avctx->priv_data;
  183. AVFrame *frame = data;
  184. AVFrame * const f = (AVFrame*)&s->frame;
  185. uint32_t header;
  186. unsigned int version,header_size;
  187. unsigned int x, y;
  188. uint32_t *buf32;
  189. uint32_t *luma1,*luma2,*cb,*cr;
  190. uint32_t offs[4];
  191. int i, is_chroma, planes;
  192. header = AV_RL32(buf);
  193. version = header & 0xff;
  194. header_size = (header & (1<<30))? 8 : 4; /* bit 30 means pad to 8 bytes */
  195. if (version > 2 && version != 4) {
  196. av_log(avctx, AV_LOG_ERROR,
  197. "This file is encoded with Fraps version %d. " \
  198. "This codec can only decode version 0, 1, 2 and 4.\n", version);
  199. return -1;
  200. }
  201. buf+=4;
  202. if (header_size == 8)
  203. buf+=4;
  204. switch(version) {
  205. case 0:
  206. default:
  207. /* Fraps v0 is a reordered YUV420 */
  208. avctx->pix_fmt = PIX_FMT_YUV420P;
  209. if ( (buf_size != avctx->width*avctx->height*3/2+header_size) &&
  210. (buf_size != header_size) ) {
  211. av_log(avctx, AV_LOG_ERROR,
  212. "Invalid frame length %d (should be %d)\n",
  213. buf_size, avctx->width*avctx->height*3/2+header_size);
  214. return -1;
  215. }
  216. if (( (avctx->width % 8) != 0) || ( (avctx->height % 2) != 0 )) {
  217. av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n",
  218. avctx->width, avctx->height);
  219. return -1;
  220. }
  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. /* bit 31 means same as previous pic */
  230. f->pict_type = (header & (1<<31))? FF_P_TYPE : FF_I_TYPE;
  231. f->key_frame = f->pict_type == FF_I_TYPE;
  232. if (f->pict_type == FF_I_TYPE) {
  233. buf32=(uint32_t*)buf;
  234. for(y=0; y<avctx->height/2; y++){
  235. luma1=(uint32_t*)&f->data[0][ y*2*f->linesize[0] ];
  236. luma2=(uint32_t*)&f->data[0][ (y*2+1)*f->linesize[0] ];
  237. cr=(uint32_t*)&f->data[1][ y*f->linesize[1] ];
  238. cb=(uint32_t*)&f->data[2][ y*f->linesize[2] ];
  239. for(x=0; x<avctx->width; x+=8){
  240. *(luma1++) = *(buf32++);
  241. *(luma1++) = *(buf32++);
  242. *(luma2++) = *(buf32++);
  243. *(luma2++) = *(buf32++);
  244. *(cr++) = *(buf32++);
  245. *(cb++) = *(buf32++);
  246. }
  247. }
  248. }
  249. break;
  250. case 1:
  251. /* Fraps v1 is an upside-down BGR24 */
  252. avctx->pix_fmt = PIX_FMT_BGR24;
  253. if ( (buf_size != avctx->width*avctx->height*3+header_size) &&
  254. (buf_size != header_size) ) {
  255. av_log(avctx, AV_LOG_ERROR,
  256. "Invalid frame length %d (should be %d)\n",
  257. buf_size, avctx->width*avctx->height*3+header_size);
  258. return -1;
  259. }
  260. f->reference = 1;
  261. f->buffer_hints = FF_BUFFER_HINTS_VALID |
  262. FF_BUFFER_HINTS_PRESERVE |
  263. FF_BUFFER_HINTS_REUSABLE;
  264. if (avctx->reget_buffer(avctx, f)) {
  265. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  266. return -1;
  267. }
  268. /* bit 31 means same as previous pic */
  269. f->pict_type = (header & (1<<31))? FF_P_TYPE : FF_I_TYPE;
  270. f->key_frame = f->pict_type == FF_I_TYPE;
  271. if (f->pict_type == FF_I_TYPE) {
  272. for(y=0; y<avctx->height; y++)
  273. memcpy(&f->data[0][ (avctx->height-y)*f->linesize[0] ],
  274. &buf[y*avctx->width*3],
  275. f->linesize[0]);
  276. }
  277. break;
  278. case 2:
  279. case 4:
  280. /**
  281. * Fraps v2 is Huffman-coded YUV420 planes
  282. * Fraps v4 is virtually the same
  283. */
  284. avctx->pix_fmt = PIX_FMT_YUV420P;
  285. planes = 3;
  286. f->reference = 1;
  287. f->buffer_hints = FF_BUFFER_HINTS_VALID |
  288. FF_BUFFER_HINTS_PRESERVE |
  289. FF_BUFFER_HINTS_REUSABLE;
  290. if (avctx->reget_buffer(avctx, f)) {
  291. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  292. return -1;
  293. }
  294. /* skip frame */
  295. if(buf_size == 8) {
  296. f->pict_type = FF_P_TYPE;
  297. f->key_frame = 0;
  298. break;
  299. }
  300. f->pict_type = FF_I_TYPE;
  301. f->key_frame = 1;
  302. if ((AV_RL32(buf) != FPS_TAG)||(buf_size < (planes*1024 + 24))) {
  303. av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
  304. return -1;
  305. }
  306. for(i = 0; i < planes; i++) {
  307. offs[i] = AV_RL32(buf + 4 + i * 4);
  308. if(offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
  309. av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
  310. return -1;
  311. }
  312. }
  313. offs[planes] = buf_size;
  314. for(i = 0; i < planes; i++){
  315. is_chroma = !!i;
  316. s->tmpbuf = av_realloc(s->tmpbuf, offs[i + 1] - offs[i] - 1024 + FF_INPUT_BUFFER_PADDING_SIZE);
  317. if(fraps2_decode_plane(s, f->data[i], f->linesize[i], avctx->width >> is_chroma,
  318. avctx->height >> is_chroma, buf + offs[i], offs[i + 1] - offs[i], is_chroma) < 0) {
  319. av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
  320. return -1;
  321. }
  322. }
  323. break;
  324. }
  325. *frame = *f;
  326. *data_size = sizeof(AVFrame);
  327. return buf_size;
  328. }
  329. /**
  330. * closes decoder
  331. * @param avctx codec context
  332. * @return 0 on success or negative if fails
  333. */
  334. static int decode_end(AVCodecContext *avctx)
  335. {
  336. FrapsContext *s = (FrapsContext*)avctx->priv_data;
  337. if (s->frame.data[0])
  338. avctx->release_buffer(avctx, &s->frame);
  339. av_freep(&s->tmpbuf);
  340. return 0;
  341. }
  342. AVCodec fraps_decoder = {
  343. "fraps",
  344. CODEC_TYPE_VIDEO,
  345. CODEC_ID_FRAPS,
  346. sizeof(FrapsContext),
  347. decode_init,
  348. NULL,
  349. decode_end,
  350. decode_frame,
  351. CODEC_CAP_DR1,
  352. };