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.

256 lines
7.9KB

  1. /*
  2. * NuppelVideo decoder
  3. * Copyright (c) 2006 Reimar Doeffinger
  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. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include "avcodec.h"
  24. #include "bswap.h"
  25. #include "dsputil.h"
  26. #include "lzo.h"
  27. #include "rtjpeg.h"
  28. typedef struct {
  29. AVFrame pic;
  30. int codec_frameheader;
  31. int width, height;
  32. unsigned int decomp_size;
  33. unsigned char* decomp_buf;
  34. uint32_t lq[64], cq[64];
  35. RTJpegContext rtj;
  36. DSPContext dsp;
  37. } NuvContext;
  38. static const uint8_t fallback_lquant[] = {
  39. 16, 11, 10, 16, 24, 40, 51, 61,
  40. 12, 12, 14, 19, 26, 58, 60, 55,
  41. 14, 13, 16, 24, 40, 57, 69, 56,
  42. 14, 17, 22, 29, 51, 87, 80, 62,
  43. 18, 22, 37, 56, 68, 109, 103, 77,
  44. 24, 35, 55, 64, 81, 104, 113, 92,
  45. 49, 64, 78, 87, 103, 121, 120, 101,
  46. 72, 92, 95, 98, 112, 100, 103, 99
  47. };
  48. static const uint8_t fallback_cquant[] = {
  49. 17, 18, 24, 47, 99, 99, 99, 99,
  50. 18, 21, 26, 66, 99, 99, 99, 99,
  51. 24, 26, 56, 99, 99, 99, 99, 99,
  52. 47, 66, 99, 99, 99, 99, 99, 99,
  53. 99, 99, 99, 99, 99, 99, 99, 99,
  54. 99, 99, 99, 99, 99, 99, 99, 99,
  55. 99, 99, 99, 99, 99, 99, 99, 99,
  56. 99, 99, 99, 99, 99, 99, 99, 99
  57. };
  58. /**
  59. * \brief copy frame data from buffer to AVFrame, handling stride.
  60. * \param f destination AVFrame
  61. * \param src source buffer, does not use any line-stride
  62. * \param width width of the video frame
  63. * \param height height of the video frame
  64. */
  65. static void copy_frame(AVFrame *f, uint8_t *src,
  66. int width, int height) {
  67. AVPicture pic;
  68. avpicture_fill(&pic, src, PIX_FMT_YUV420P, width, height);
  69. av_picture_copy((AVPicture *)f, &pic, PIX_FMT_YUV420P, width, height);
  70. }
  71. /**
  72. * \brief extract quantization tables from codec data into our context
  73. */
  74. static int get_quant(AVCodecContext *avctx, NuvContext *c,
  75. uint8_t *buf, int size) {
  76. int i;
  77. if (size < 2 * 64 * 4) {
  78. av_log(avctx, AV_LOG_ERROR, "insufficient rtjpeg quant data\n");
  79. return -1;
  80. }
  81. for (i = 0; i < 64; i++, buf += 4)
  82. c->lq[i] = AV_RL32(buf);
  83. for (i = 0; i < 64; i++, buf += 4)
  84. c->cq[i] = AV_RL32(buf);
  85. return 0;
  86. }
  87. /**
  88. * \brief set quantization tables from a quality value
  89. */
  90. static void get_quant_quality(NuvContext *c, int quality) {
  91. int i;
  92. quality = FFMAX(quality, 1);
  93. for (i = 0; i < 64; i++) {
  94. c->lq[i] = (fallback_lquant[i] << 7) / quality;
  95. c->cq[i] = (fallback_cquant[i] << 7) / quality;
  96. }
  97. }
  98. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  99. uint8_t *buf, int buf_size) {
  100. NuvContext *c = avctx->priv_data;
  101. AVFrame *picture = data;
  102. int orig_size = buf_size;
  103. enum {NUV_UNCOMPRESSED = '0', NUV_RTJPEG = '1',
  104. NUV_RTJPEG_IN_LZO = '2', NUV_LZO = '3',
  105. NUV_BLACK = 'N', NUV_COPY_LAST = 'L'} comptype;
  106. if (buf_size < 12) {
  107. av_log(avctx, AV_LOG_ERROR, "coded frame too small\n");
  108. return -1;
  109. }
  110. if (c->pic.data[0])
  111. avctx->release_buffer(avctx, &c->pic);
  112. c->pic.reference = 1;
  113. c->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_READABLE |
  114. FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  115. if (avctx->get_buffer(avctx, &c->pic) < 0) {
  116. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  117. return -1;
  118. }
  119. // codec data (rtjpeg quant tables)
  120. if (buf[0] == 'D' && buf[1] == 'R') {
  121. int ret;
  122. // skip rest of the frameheader.
  123. buf = &buf[12];
  124. buf_size -= 12;
  125. ret = get_quant(avctx, c, buf, buf_size);
  126. if (ret < 0)
  127. return ret;
  128. rtjpeg_decode_init(&c->rtj, &c->dsp, c->width, c->height, c->lq, c->cq);
  129. return orig_size;
  130. }
  131. if (buf[0] != 'V' || buf_size < 12) {
  132. av_log(avctx, AV_LOG_ERROR, "not a nuv video frame\n");
  133. return -1;
  134. }
  135. comptype = buf[1];
  136. // skip rest of the frameheader.
  137. buf = &buf[12];
  138. buf_size -= 12;
  139. if (comptype == NUV_RTJPEG_IN_LZO || comptype == NUV_LZO) {
  140. int outlen = c->decomp_size, inlen = buf_size;
  141. if (lzo1x_decode(c->decomp_buf, &outlen, buf, &inlen))
  142. av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n");
  143. buf = c->decomp_buf;
  144. buf_size = c->decomp_size;
  145. }
  146. if (c->codec_frameheader) {
  147. if (buf_size < 12) {
  148. av_log(avctx, AV_LOG_ERROR, "invalid nuv video frame\n");
  149. return -1;
  150. }
  151. get_quant_quality(c, buf[10]);
  152. rtjpeg_decode_init(&c->rtj, &c->dsp, c->width, c->height, c->lq, c->cq);
  153. buf = &buf[12];
  154. buf_size -= 12;
  155. }
  156. c->pic.pict_type = FF_I_TYPE;
  157. c->pic.key_frame = 1;
  158. // decompress/copy/whatever data
  159. switch (comptype) {
  160. case NUV_LZO:
  161. case NUV_UNCOMPRESSED: {
  162. int height = c->height;
  163. if (buf_size < c->width * height * 3 / 2) {
  164. av_log(avctx, AV_LOG_ERROR, "uncompressed frame too short\n");
  165. height = buf_size / c->width / 3 * 2;
  166. }
  167. copy_frame(&c->pic, buf, c->width, height);
  168. break;
  169. }
  170. case NUV_RTJPEG_IN_LZO:
  171. case NUV_RTJPEG: {
  172. rtjpeg_decode_frame_yuv420(&c->rtj, &c->pic, buf, buf_size);
  173. break;
  174. }
  175. case NUV_BLACK: {
  176. memset(c->pic.data[0], 0, c->width * c->height);
  177. memset(c->pic.data[1], 128, c->width * c->height / 4);
  178. memset(c->pic.data[2], 128, c->width * c->height / 4);
  179. break;
  180. }
  181. case NUV_COPY_LAST: {
  182. c->pic.pict_type = FF_P_TYPE;
  183. c->pic.key_frame = 0;
  184. /* nothing more to do here */
  185. break;
  186. }
  187. default:
  188. av_log(avctx, AV_LOG_ERROR, "unknown compression\n");
  189. return -1;
  190. }
  191. *picture = c->pic;
  192. *data_size = sizeof(AVFrame);
  193. return orig_size;
  194. }
  195. static int decode_init(AVCodecContext *avctx) {
  196. NuvContext *c = avctx->priv_data;
  197. avctx->width = (avctx->width + 1) & ~1;
  198. avctx->height = (avctx->height + 1) & ~1;
  199. if (avcodec_check_dimensions(avctx, avctx->height, avctx->width) < 0) {
  200. return 1;
  201. }
  202. avctx->pix_fmt = PIX_FMT_YUV420P;
  203. c->pic.data[0] = NULL;
  204. c->codec_frameheader = avctx->codec_tag == MKTAG('R', 'J', 'P', 'G');
  205. c->width = avctx->width;
  206. c->height = avctx->height;
  207. c->decomp_size = c->height * c->width * 3 / 2;
  208. c->decomp_buf = av_malloc(c->decomp_size + LZO_OUTPUT_PADDING);
  209. if (!c->decomp_buf) {
  210. av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
  211. return 1;
  212. }
  213. dsputil_init(&c->dsp, avctx);
  214. if (avctx->extradata_size)
  215. get_quant(avctx, c, avctx->extradata, avctx->extradata_size);
  216. rtjpeg_decode_init(&c->rtj, &c->dsp, c->width, c->height, c->lq, c->cq);
  217. return 0;
  218. }
  219. static int decode_end(AVCodecContext *avctx) {
  220. NuvContext *c = avctx->priv_data;
  221. av_freep(&c->decomp_buf);
  222. if (c->pic.data[0])
  223. avctx->release_buffer(avctx, &c->pic);
  224. return 0;
  225. }
  226. AVCodec nuv_decoder = {
  227. "nuv",
  228. CODEC_TYPE_VIDEO,
  229. CODEC_ID_NUV,
  230. sizeof(NuvContext),
  231. decode_init,
  232. NULL,
  233. decode_end,
  234. decode_frame,
  235. CODEC_CAP_DR1,
  236. };