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.

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