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.

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