nuv.c 9.0 KB

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