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.

251 lines
7.4KB

  1. /*
  2. * Fraps FPS1 decoder
  3. * Copyright (c) 2005 Roine Gustafsson
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. /**
  23. * @file fraps.c
  24. * Lossless Fraps 'FPS1' decoder
  25. * @author Roine Gustafsson <roine at users sf net>
  26. *
  27. * Only decodes version 0 and 1 files.
  28. * Codec algorithm for version 0 is taken from Transcode <www.transcoding.org>
  29. *
  30. * Version 2 files, which are the most commonly found Fraps files, cannot be
  31. * decoded yet.
  32. */
  33. #include "avcodec.h"
  34. #define FPS_TAG MKTAG('F', 'P', 'S', 'x')
  35. /**
  36. * local variable storage
  37. */
  38. typedef struct FrapsContext{
  39. AVCodecContext *avctx;
  40. AVFrame frame;
  41. } FrapsContext;
  42. /**
  43. * initializes decoder
  44. * @param avctx codec context
  45. * @return 0 on success or negative if fails
  46. */
  47. static int decode_init(AVCodecContext *avctx)
  48. {
  49. FrapsContext * const s = avctx->priv_data;
  50. avctx->coded_frame = (AVFrame*)&s->frame;
  51. avctx->has_b_frames = 0;
  52. avctx->pix_fmt= PIX_FMT_NONE; /* set in decode_frame */
  53. s->avctx = avctx;
  54. s->frame.data[0] = NULL;
  55. return 0;
  56. }
  57. /**
  58. * decode a frame
  59. * @param avctx codec context
  60. * @param data output AVFrame
  61. * @param data_size size of output data or 0 if no picture is returned
  62. * @param buf input data frame
  63. * @param buf_size size of input data frame
  64. * @return number of consumed bytes on success or negative if decode fails
  65. */
  66. static int decode_frame(AVCodecContext *avctx,
  67. void *data, int *data_size,
  68. uint8_t *buf, int buf_size)
  69. {
  70. FrapsContext * const s = avctx->priv_data;
  71. AVFrame *frame = data;
  72. AVFrame * const f = (AVFrame*)&s->frame;
  73. uint32_t header;
  74. unsigned int version,header_size;
  75. unsigned int x, y;
  76. uint32_t *buf32;
  77. uint32_t *luma1,*luma2,*cb,*cr;
  78. header = LE_32(buf);
  79. version = header & 0xff;
  80. header_size = (header & (1<<30))? 8 : 4; /* bit 30 means pad to 8 bytes */
  81. if (version > 1) {
  82. av_log(avctx, AV_LOG_ERROR,
  83. "This file is encoded with Fraps version %d. " \
  84. "This codec can only decode version 0 and 1.\n", version);
  85. return -1;
  86. }
  87. buf+=4;
  88. if (header_size == 8)
  89. buf+=4;
  90. switch(version) {
  91. case 0:
  92. default:
  93. /* Fraps v0 is a reordered YUV420 */
  94. avctx->pix_fmt = PIX_FMT_YUV420P;
  95. if ( (buf_size != avctx->width*avctx->height*3/2+header_size) &&
  96. (buf_size != header_size) ) {
  97. av_log(avctx, AV_LOG_ERROR,
  98. "Invalid frame length %d (should be %d)\n",
  99. buf_size, avctx->width*avctx->height*3/2+header_size);
  100. return -1;
  101. }
  102. if (( (avctx->width % 8) != 0) || ( (avctx->height % 2) != 0 )) {
  103. av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n",
  104. avctx->width, avctx->height);
  105. return -1;
  106. }
  107. f->reference = 1;
  108. f->buffer_hints = FF_BUFFER_HINTS_VALID |
  109. FF_BUFFER_HINTS_PRESERVE |
  110. FF_BUFFER_HINTS_REUSABLE;
  111. if (avctx->reget_buffer(avctx, f)) {
  112. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  113. return -1;
  114. }
  115. /* bit 31 means same as previous pic */
  116. f->pict_type = (header & (1<<31))? FF_P_TYPE : FF_I_TYPE;
  117. f->key_frame = f->pict_type == FF_I_TYPE;
  118. if (f->pict_type == FF_I_TYPE) {
  119. buf32=(uint32_t*)buf;
  120. for(y=0; y<avctx->height/2; y++){
  121. luma1=(uint32_t*)&f->data[0][ y*2*f->linesize[0] ];
  122. luma2=(uint32_t*)&f->data[0][ (y*2+1)*f->linesize[0] ];
  123. cr=(uint32_t*)&f->data[1][ y*f->linesize[1] ];
  124. cb=(uint32_t*)&f->data[2][ y*f->linesize[2] ];
  125. for(x=0; x<avctx->width; x+=8){
  126. *(luma1++) = *(buf32++);
  127. *(luma1++) = *(buf32++);
  128. *(luma2++) = *(buf32++);
  129. *(luma2++) = *(buf32++);
  130. *(cr++) = *(buf32++);
  131. *(cb++) = *(buf32++);
  132. }
  133. }
  134. }
  135. break;
  136. case 1:
  137. /* Fraps v1 is an upside-down BGR24 */
  138. avctx->pix_fmt = PIX_FMT_BGR24;
  139. if ( (buf_size != avctx->width*avctx->height*3+header_size) &&
  140. (buf_size != header_size) ) {
  141. av_log(avctx, AV_LOG_ERROR,
  142. "Invalid frame length %d (should be %d)\n",
  143. buf_size, avctx->width*avctx->height*3+header_size);
  144. return -1;
  145. }
  146. f->reference = 1;
  147. f->buffer_hints = FF_BUFFER_HINTS_VALID |
  148. FF_BUFFER_HINTS_PRESERVE |
  149. FF_BUFFER_HINTS_REUSABLE;
  150. if (avctx->reget_buffer(avctx, f)) {
  151. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  152. return -1;
  153. }
  154. /* bit 31 means same as previous pic */
  155. f->pict_type = (header & (1<<31))? FF_P_TYPE : FF_I_TYPE;
  156. f->key_frame = f->pict_type == FF_I_TYPE;
  157. if (f->pict_type == FF_I_TYPE) {
  158. for(y=0; y<avctx->height; y++)
  159. memcpy(&f->data[0][ (avctx->height-y)*f->linesize[0] ],
  160. &buf[y*avctx->width*3],
  161. f->linesize[0]);
  162. }
  163. break;
  164. case 2:
  165. /**
  166. * Fraps v2 sub-header description. All numbers are little-endian:
  167. * (this is all guesswork)
  168. *
  169. * 0: DWORD 'FPSx'
  170. * 4: DWORD 0x00000010 unknown, perhaps flags
  171. * 8: DWORD off_2 offset to plane 2
  172. * 12: DWORD off_3 offset to plane 3
  173. * 16: 256xDWORD freqtbl_1 frequency table for plane 1
  174. * 1040: plane_1
  175. * ...
  176. * off_2: 256xDWORD freqtbl_2 frequency table for plane 2
  177. * plane_2
  178. * ...
  179. * off_3: 256xDWORD freqtbl_3 frequency table for plane 3
  180. * plane_3
  181. */
  182. if ((BE_32(buf) != FPS_TAG)||(buf_size < (3*1024 + 8))) {
  183. av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
  184. return -1;
  185. }
  186. /* NOT FINISHED */
  187. break;
  188. }
  189. *frame = *f;
  190. *data_size = sizeof(AVFrame);
  191. return buf_size;
  192. }
  193. /**
  194. * closes decoder
  195. * @param avctx codec context
  196. * @return 0 on success or negative if fails
  197. */
  198. static int decode_end(AVCodecContext *avctx)
  199. {
  200. FrapsContext *s = (FrapsContext*)avctx->priv_data;
  201. if (s->frame.data[0])
  202. avctx->release_buffer(avctx, &s->frame);
  203. return 0;
  204. }
  205. AVCodec fraps_decoder = {
  206. "fraps",
  207. CODEC_TYPE_VIDEO,
  208. CODEC_ID_FRAPS,
  209. sizeof(FrapsContext),
  210. decode_init,
  211. NULL,
  212. decode_end,
  213. decode_frame,
  214. CODEC_CAP_DR1,
  215. };