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.

371 lines
11KB

  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 <limits.h>
  24. #include "libavutil/bswap.h"
  25. #include "libavutil/common.h"
  26. #include "libavutil/intreadwrite.h"
  27. #include "libavutil/lzo.h"
  28. #include "libavutil/imgutils.h"
  29. #include "avcodec.h"
  30. #include "idctdsp.h"
  31. #include "internal.h"
  32. #include "rtjpeg.h"
  33. typedef struct NuvContext {
  34. AVFrame *pic;
  35. int codec_frameheader;
  36. int quality;
  37. int width, height;
  38. unsigned int decomp_size;
  39. unsigned char *decomp_buf;
  40. uint32_t lq[64], cq[64];
  41. RTJpegContext rtj;
  42. } NuvContext;
  43. static const uint8_t fallback_lquant[] = {
  44. 16, 11, 10, 16, 24, 40, 51, 61,
  45. 12, 12, 14, 19, 26, 58, 60, 55,
  46. 14, 13, 16, 24, 40, 57, 69, 56,
  47. 14, 17, 22, 29, 51, 87, 80, 62,
  48. 18, 22, 37, 56, 68, 109, 103, 77,
  49. 24, 35, 55, 64, 81, 104, 113, 92,
  50. 49, 64, 78, 87, 103, 121, 120, 101,
  51. 72, 92, 95, 98, 112, 100, 103, 99
  52. };
  53. static const uint8_t fallback_cquant[] = {
  54. 17, 18, 24, 47, 99, 99, 99, 99,
  55. 18, 21, 26, 66, 99, 99, 99, 99,
  56. 24, 26, 56, 99, 99, 99, 99, 99,
  57. 47, 66, 99, 99, 99, 99, 99, 99,
  58. 99, 99, 99, 99, 99, 99, 99, 99,
  59. 99, 99, 99, 99, 99, 99, 99, 99,
  60. 99, 99, 99, 99, 99, 99, 99, 99,
  61. 99, 99, 99, 99, 99, 99, 99, 99
  62. };
  63. /**
  64. * @brief copy frame data from buffer to AVFrame, handling stride.
  65. * @param f destination AVFrame
  66. * @param src source buffer, does not use any line-stride
  67. * @param width width of the video frame
  68. * @param height height of the video frame
  69. */
  70. static void copy_frame(AVFrame *f, const uint8_t *src, int width, int height)
  71. {
  72. AVPicture pic;
  73. avpicture_fill(&pic, src, AV_PIX_FMT_YUV420P, width, height);
  74. av_picture_copy((AVPicture *)f, &pic, AV_PIX_FMT_YUV420P, width, height);
  75. }
  76. /**
  77. * @brief extract quantization tables from codec data into our context
  78. */
  79. static int get_quant(AVCodecContext *avctx, NuvContext *c, const uint8_t *buf,
  80. int size)
  81. {
  82. int i;
  83. if (size < 2 * 64 * 4) {
  84. av_log(avctx, AV_LOG_ERROR, "insufficient rtjpeg quant data\n");
  85. return AVERROR_INVALIDDATA;
  86. }
  87. for (i = 0; i < 64; i++, buf += 4)
  88. c->lq[i] = AV_RL32(buf);
  89. for (i = 0; i < 64; i++, buf += 4)
  90. c->cq[i] = AV_RL32(buf);
  91. return 0;
  92. }
  93. /**
  94. * @brief set quantization tables from a quality value
  95. */
  96. static void get_quant_quality(NuvContext *c, int quality)
  97. {
  98. int i;
  99. quality = FFMAX(quality, 1);
  100. for (i = 0; i < 64; i++) {
  101. c->lq[i] = (fallback_lquant[i] << 7) / quality;
  102. c->cq[i] = (fallback_cquant[i] << 7) / quality;
  103. }
  104. }
  105. static int codec_reinit(AVCodecContext *avctx, int width, int height,
  106. int quality)
  107. {
  108. NuvContext *c = avctx->priv_data;
  109. int ret;
  110. width = FFALIGN(width, 2);
  111. height = FFALIGN(height, 2);
  112. if (quality >= 0)
  113. get_quant_quality(c, quality);
  114. if (width != c->width || height != c->height) {
  115. // also reserve space for a possible additional header
  116. int buf_size = height * width * 3 / 2
  117. + FFMAX(AV_LZO_OUTPUT_PADDING, AV_INPUT_BUFFER_PADDING_SIZE)
  118. + RTJPEG_HEADER_SIZE;
  119. if (buf_size > INT_MAX/8)
  120. return -1;
  121. if ((ret = ff_set_dimensions(avctx, width, height)) < 0)
  122. return ret;
  123. c->width = width;
  124. c->height = height;
  125. av_fast_malloc(&c->decomp_buf, &c->decomp_size,
  126. buf_size);
  127. if (!c->decomp_buf) {
  128. av_log(avctx, AV_LOG_ERROR,
  129. "Can't allocate decompression buffer.\n");
  130. return AVERROR(ENOMEM);
  131. }
  132. ff_rtjpeg_decode_init(&c->rtj, c->width, c->height, c->lq, c->cq);
  133. av_frame_unref(c->pic);
  134. return 1;
  135. } else if (quality != c->quality)
  136. ff_rtjpeg_decode_init(&c->rtj, c->width, c->height, c->lq, c->cq);
  137. return 0;
  138. }
  139. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  140. AVPacket *avpkt)
  141. {
  142. const uint8_t *buf = avpkt->data;
  143. int buf_size = avpkt->size;
  144. NuvContext *c = avctx->priv_data;
  145. AVFrame *picture = data;
  146. int orig_size = buf_size;
  147. int keyframe, ret;
  148. int size_change = 0;
  149. int minsize = 0;
  150. int result, init_frame = !avctx->frame_number;
  151. enum {
  152. NUV_UNCOMPRESSED = '0',
  153. NUV_RTJPEG = '1',
  154. NUV_RTJPEG_IN_LZO = '2',
  155. NUV_LZO = '3',
  156. NUV_BLACK = 'N',
  157. NUV_COPY_LAST = 'L'
  158. } comptype;
  159. if (buf_size < 12) {
  160. av_log(avctx, AV_LOG_ERROR, "coded frame too small\n");
  161. return AVERROR_INVALIDDATA;
  162. }
  163. // codec data (rtjpeg quant tables)
  164. if (buf[0] == 'D' && buf[1] == 'R') {
  165. int ret;
  166. // skip rest of the frameheader.
  167. buf = &buf[12];
  168. buf_size -= 12;
  169. ret = get_quant(avctx, c, buf, buf_size);
  170. if (ret < 0)
  171. return ret;
  172. ff_rtjpeg_decode_init(&c->rtj, c->width, c->height, c->lq, c->cq);
  173. return orig_size;
  174. }
  175. if (buf_size < 12 || buf[0] != 'V') {
  176. av_log(avctx, AV_LOG_ERROR, "not a nuv video frame\n");
  177. return AVERROR_INVALIDDATA;
  178. }
  179. comptype = buf[1];
  180. switch (comptype) {
  181. case NUV_RTJPEG_IN_LZO:
  182. case NUV_RTJPEG:
  183. keyframe = !buf[2];
  184. if (c->width < 16 || c->height < 16) {
  185. return AVERROR_INVALIDDATA;
  186. }
  187. break;
  188. case NUV_COPY_LAST:
  189. keyframe = 0;
  190. break;
  191. default:
  192. keyframe = 1;
  193. break;
  194. }
  195. switch (comptype) {
  196. case NUV_UNCOMPRESSED:
  197. minsize = c->width * c->height * 3 / 2;
  198. break;
  199. case NUV_RTJPEG:
  200. minsize = c->width/16 * (c->height/16) * 6;
  201. break;
  202. case NUV_BLACK:
  203. case NUV_COPY_LAST:
  204. case NUV_LZO:
  205. case NUV_RTJPEG_IN_LZO:
  206. break;
  207. default:
  208. av_log(avctx, AV_LOG_ERROR, "unknown compression\n");
  209. return AVERROR_INVALIDDATA;
  210. }
  211. if (buf_size < minsize / 4)
  212. return AVERROR_INVALIDDATA;
  213. retry:
  214. // skip rest of the frameheader.
  215. buf = &buf[12];
  216. buf_size -= 12;
  217. if (comptype == NUV_RTJPEG_IN_LZO || comptype == NUV_LZO) {
  218. int outlen = c->decomp_size - FFMAX(AV_INPUT_BUFFER_PADDING_SIZE, AV_LZO_OUTPUT_PADDING);
  219. int inlen = buf_size;
  220. if (av_lzo1x_decode(c->decomp_buf, &outlen, buf, &inlen)) {
  221. av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n");
  222. return AVERROR_INVALIDDATA;
  223. }
  224. buf = c->decomp_buf;
  225. buf_size = c->decomp_size - FFMAX(AV_INPUT_BUFFER_PADDING_SIZE, AV_LZO_OUTPUT_PADDING) - outlen;
  226. memset(c->decomp_buf + buf_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  227. }
  228. if (c->codec_frameheader) {
  229. int w, h, q;
  230. if (buf_size < RTJPEG_HEADER_SIZE) {
  231. av_log(avctx, AV_LOG_ERROR, "Too small NUV video frame\n");
  232. return AVERROR_INVALIDDATA;
  233. }
  234. // There seem to exist two variants of this header: one starts with 'V'
  235. // and 5 bytes unknown, the other matches current MythTV and is 4 bytes size,
  236. // 1 byte header size (== 12), 1 byte version (== 0)
  237. if (buf[0] != 'V' && AV_RL16(&buf[4]) != 0x000c) {
  238. av_log(avctx, AV_LOG_ERROR, "Unknown secondary frame header (wrong codec_tag?)\n");
  239. return AVERROR_INVALIDDATA;
  240. }
  241. w = AV_RL16(&buf[6]);
  242. h = AV_RL16(&buf[8]);
  243. q = buf[10];
  244. if ((result = codec_reinit(avctx, w, h, q)) < 0)
  245. return result;
  246. if (result) {
  247. buf = avpkt->data;
  248. buf_size = avpkt->size;
  249. size_change = 1;
  250. goto retry;
  251. }
  252. buf = &buf[RTJPEG_HEADER_SIZE];
  253. buf_size -= RTJPEG_HEADER_SIZE;
  254. }
  255. if (size_change || keyframe) {
  256. av_frame_unref(c->pic);
  257. init_frame = 1;
  258. }
  259. if ((result = ff_reget_buffer(avctx, c->pic)) < 0)
  260. return result;
  261. if (init_frame) {
  262. memset(c->pic->data[0], 0, avctx->height * c->pic->linesize[0]);
  263. memset(c->pic->data[1], 0x80, avctx->height * c->pic->linesize[1] / 2);
  264. memset(c->pic->data[2], 0x80, avctx->height * c->pic->linesize[2] / 2);
  265. }
  266. c->pic->pict_type = keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
  267. c->pic->key_frame = keyframe;
  268. // decompress/copy/whatever data
  269. switch (comptype) {
  270. case NUV_LZO:
  271. case NUV_UNCOMPRESSED: {
  272. int height = c->height;
  273. if (buf_size < c->width * height * 3 / 2) {
  274. av_log(avctx, AV_LOG_ERROR, "uncompressed frame too short\n");
  275. height = buf_size / c->width / 3 * 2;
  276. }
  277. if(height > 0)
  278. copy_frame(c->pic, buf, c->width, height);
  279. break;
  280. }
  281. case NUV_RTJPEG_IN_LZO:
  282. case NUV_RTJPEG:
  283. ret = ff_rtjpeg_decode_frame_yuv420(&c->rtj, c->pic, buf, buf_size);
  284. if (ret < 0)
  285. return ret;
  286. break;
  287. case NUV_BLACK:
  288. memset(c->pic->data[0], 0, c->width * c->height);
  289. memset(c->pic->data[1], 128, c->width * c->height / 4);
  290. memset(c->pic->data[2], 128, c->width * c->height / 4);
  291. break;
  292. case NUV_COPY_LAST:
  293. /* nothing more to do here */
  294. break;
  295. }
  296. if ((result = av_frame_ref(picture, c->pic)) < 0)
  297. return result;
  298. *got_frame = 1;
  299. return orig_size;
  300. }
  301. static av_cold int decode_init(AVCodecContext *avctx)
  302. {
  303. NuvContext *c = avctx->priv_data;
  304. int ret;
  305. c->pic = av_frame_alloc();
  306. if (!c->pic)
  307. return AVERROR(ENOMEM);
  308. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  309. c->decomp_buf = NULL;
  310. c->quality = -1;
  311. c->width = 0;
  312. c->height = 0;
  313. c->codec_frameheader = avctx->codec_tag == MKTAG('R', 'J', 'P', 'G');
  314. if (avctx->extradata_size)
  315. get_quant(avctx, c, avctx->extradata, avctx->extradata_size);
  316. ff_rtjpeg_init(&c->rtj, avctx);
  317. if ((ret = codec_reinit(avctx, avctx->width, avctx->height, -1)) < 0)
  318. return ret;
  319. return 0;
  320. }
  321. static av_cold int decode_end(AVCodecContext *avctx)
  322. {
  323. NuvContext *c = avctx->priv_data;
  324. av_freep(&c->decomp_buf);
  325. av_frame_free(&c->pic);
  326. return 0;
  327. }
  328. AVCodec ff_nuv_decoder = {
  329. .name = "nuv",
  330. .long_name = NULL_IF_CONFIG_SMALL("NuppelVideo/RTJPEG"),
  331. .type = AVMEDIA_TYPE_VIDEO,
  332. .id = AV_CODEC_ID_NUV,
  333. .priv_data_size = sizeof(NuvContext),
  334. .init = decode_init,
  335. .close = decode_end,
  336. .decode = decode_frame,
  337. .capabilities = AV_CODEC_CAP_DR1,
  338. };