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.

215 lines
6.8KB

  1. /*
  2. * NuppelVideo decoder
  3. * Copyright (c) 2006 Reimar Doeffinger
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include "common.h"
  22. #include "avcodec.h"
  23. #include "bswap.h"
  24. #include "dsputil.h"
  25. #include "lzo.h"
  26. #include "rtjpeg.h"
  27. typedef struct {
  28. AVFrame pic;
  29. int width, height;
  30. unsigned int decomp_size;
  31. unsigned char* decomp_buf;
  32. uint32_t lq[64], cq[64];
  33. RTJpegContext rtj;
  34. DSPContext dsp;
  35. } NuvContext;
  36. /**
  37. * \brief copy frame data from buffer to AVFrame, handling stride.
  38. * \param f destination AVFrame
  39. * \param src source buffer, does not use any line-stride
  40. * \param width width of the video frame
  41. * \param height height of the video frame
  42. */
  43. static void copy_frame(AVFrame *f, uint8_t *src,
  44. int width, int height) {
  45. AVPicture pic;
  46. avpicture_fill(&pic, src, PIX_FMT_YUV420P, width, height);
  47. img_copy((AVPicture *)f, &pic, PIX_FMT_YUV420P, width, height);
  48. }
  49. /**
  50. * \brief extract quantization tables from codec data into our context
  51. */
  52. static int get_quant(AVCodecContext *avctx, NuvContext *c,
  53. uint8_t *buf, int size) {
  54. int i;
  55. if (size < 2 * 64 * 4) {
  56. av_log(avctx, AV_LOG_ERROR, "insufficient rtjpeg quant data\n");
  57. return -1;
  58. }
  59. for (i = 0; i < 64; i++, buf += 4)
  60. c->lq[i] = LE_32(buf);
  61. for (i = 0; i < 64; i++, buf += 4)
  62. c->cq[i] = LE_32(buf);
  63. return 0;
  64. }
  65. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  66. uint8_t *buf, int buf_size) {
  67. NuvContext *c = (NuvContext *)avctx->priv_data;
  68. AVFrame *picture = data;
  69. int orig_size = buf_size;
  70. enum {NUV_UNCOMPRESSED = '0', NUV_RTJPEG = '1',
  71. NUV_RTJPEG_IN_LZO = '2', NUV_LZO = '3',
  72. NUV_BLACK = 'N', NUV_COPY_LAST = 'L'} comptype;
  73. if (buf_size < 12) {
  74. av_log(avctx, AV_LOG_ERROR, "coded frame too small\n");
  75. return -1;
  76. }
  77. if (c->pic.data[0])
  78. avctx->release_buffer(avctx, &c->pic);
  79. c->pic.reference = 1;
  80. c->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_READABLE |
  81. FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  82. if (avctx->get_buffer(avctx, &c->pic) < 0) {
  83. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  84. return -1;
  85. }
  86. // codec data (rtjpeg quant tables)
  87. if (buf[0] == 'D' && buf[1] == 'R') {
  88. int ret;
  89. // skip rest of the frameheader.
  90. buf = &buf[12];
  91. buf_size -= 12;
  92. ret = get_quant(avctx, c, buf, buf_size);
  93. if (ret < 0)
  94. return ret;
  95. rtjpeg_decode_init(&c->rtj, &c->dsp, c->width, c->height, c->lq, c->cq);
  96. return orig_size;
  97. }
  98. if (buf[0] != 'V' || buf_size < 12) {
  99. av_log(avctx, AV_LOG_ERROR, "not a nuv video frame\n");
  100. return -1;
  101. }
  102. comptype = buf[1];
  103. // skip rest of the frameheader.
  104. buf = &buf[12];
  105. buf_size -= 12;
  106. c->pic.pict_type = FF_I_TYPE;
  107. c->pic.key_frame = 1;
  108. // decompress/copy/whatever data
  109. switch (comptype) {
  110. case NUV_UNCOMPRESSED: {
  111. int height = c->height;
  112. if (buf_size < c->width * height * 3 / 2) {
  113. av_log(avctx, AV_LOG_ERROR, "uncompressed frame too short\n");
  114. height = buf_size / c->width / 3 * 2;
  115. }
  116. copy_frame(&c->pic, buf, c->width, height);
  117. break;
  118. }
  119. case NUV_RTJPEG: {
  120. rtjpeg_decode_frame_yuv420(&c->rtj, &c->pic, buf, buf_size);
  121. break;
  122. }
  123. case NUV_RTJPEG_IN_LZO: {
  124. int outlen = c->decomp_size, inlen = buf_size;
  125. if (lzo1x_decode(c->decomp_buf, &outlen, buf, &inlen))
  126. av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n");
  127. rtjpeg_decode_frame_yuv420(&c->rtj, &c->pic, c->decomp_buf, c->decomp_size);
  128. break;
  129. }
  130. case NUV_LZO: {
  131. int outlen = c->decomp_size, inlen = buf_size;
  132. if (lzo1x_decode(c->decomp_buf, &outlen, buf, &inlen))
  133. av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n");
  134. copy_frame(&c->pic, c->decomp_buf, c->width, c->height);
  135. break;
  136. }
  137. case NUV_BLACK: {
  138. memset(c->pic.data[0], 0, c->width * c->height);
  139. memset(c->pic.data[1], 128, c->width * c->height / 4);
  140. memset(c->pic.data[2], 128, c->width * c->height / 4);
  141. break;
  142. }
  143. case NUV_COPY_LAST: {
  144. c->pic.pict_type = FF_P_TYPE;
  145. c->pic.key_frame = 0;
  146. /* nothing more to do here */
  147. break;
  148. }
  149. default:
  150. av_log(avctx, AV_LOG_ERROR, "unknown compression\n");
  151. return -1;
  152. }
  153. *picture = c->pic;
  154. *data_size = sizeof(AVFrame);
  155. return orig_size;
  156. }
  157. static int decode_init(AVCodecContext *avctx) {
  158. NuvContext *c = (NuvContext *)avctx->priv_data;
  159. avctx->width = (avctx->width + 1) & ~1;
  160. avctx->height = (avctx->height + 1) & ~1;
  161. if (avcodec_check_dimensions(avctx, avctx->height, avctx->width) < 0) {
  162. return 1;
  163. }
  164. avctx->has_b_frames = 0;
  165. avctx->pix_fmt = PIX_FMT_YUV420P;
  166. c->pic.data[0] = NULL;
  167. c->width = avctx->width;
  168. c->height = avctx->height;
  169. c->decomp_size = c->height * c->width * 3 / 2;
  170. c->decomp_buf = av_malloc(c->decomp_size + LZO_OUTPUT_PADDING);
  171. if (!c->decomp_buf) {
  172. av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
  173. return 1;
  174. }
  175. dsputil_init(&c->dsp, avctx);
  176. if (avctx->extradata_size)
  177. get_quant(avctx, c, avctx->extradata, avctx->extradata_size);
  178. rtjpeg_decode_init(&c->rtj, &c->dsp, c->width, c->height, c->lq, c->cq);
  179. return 0;
  180. }
  181. static int decode_end(AVCodecContext *avctx) {
  182. NuvContext *c = (NuvContext *)avctx->priv_data;
  183. av_freep(&c->decomp_buf);
  184. if (c->pic.data[0])
  185. avctx->release_buffer(avctx, &c->pic);
  186. return 0;
  187. }
  188. AVCodec nuv_decoder = {
  189. "nuv",
  190. CODEC_TYPE_VIDEO,
  191. CODEC_ID_NUV,
  192. sizeof(NuvContext),
  193. decode_init,
  194. NULL,
  195. decode_end,
  196. decode_frame,
  197. CODEC_CAP_DR1,
  198. };