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.

184 lines
5.8KB

  1. /*
  2. * RTJpeg decoding functions
  3. * Copyright (c) 2006 Reimar Doeffinger
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/common.h"
  22. #include "get_bits.h"
  23. #include "rtjpeg.h"
  24. #define PUT_COEFF(c) \
  25. i = scan[coeff--]; \
  26. block[i] = (c) * quant[i];
  27. /// aligns the bitstream to the given power of two
  28. #define ALIGN(a) \
  29. n = (-get_bits_count(gb)) & (a - 1); \
  30. if (n) {skip_bits(gb, n);}
  31. /**
  32. * @brief read one block from stream
  33. * @param gb contains stream data
  34. * @param block where data is written to
  35. * @param scan array containing the mapping stream address -> block position
  36. * @param quant quantization factors
  37. * @return 0 means the block is not coded, < 0 means an error occurred.
  38. *
  39. * Note: GetBitContext is used to make the code simpler, since all data is
  40. * aligned this could be done faster in a different way, e.g. as it is done
  41. * in MPlayer libmpcodecs/native/rtjpegn.c.
  42. */
  43. static inline int get_block(GetBitContext *gb, int16_t *block, const uint8_t *scan,
  44. const uint32_t *quant) {
  45. int coeff, i, n;
  46. int8_t ac;
  47. uint8_t dc = get_bits(gb, 8);
  48. // block not coded
  49. if (dc == 255)
  50. return 0;
  51. // number of non-zero coefficients
  52. coeff = get_bits(gb, 6);
  53. if (get_bits_left(gb) < (coeff << 1))
  54. return AVERROR_INVALIDDATA;
  55. // normally we would only need to clear the (63 - coeff) last values,
  56. // but since we do not know where they are we just clear the whole block
  57. memset(block, 0, 64 * sizeof(int16_t));
  58. // 2 bits per coefficient
  59. while (coeff) {
  60. ac = get_sbits(gb, 2);
  61. if (ac == -2)
  62. break; // continue with more bits
  63. PUT_COEFF(ac);
  64. }
  65. // 4 bits per coefficient
  66. ALIGN(4);
  67. if (get_bits_left(gb) < (coeff << 2))
  68. return AVERROR_INVALIDDATA;
  69. while (coeff) {
  70. ac = get_sbits(gb, 4);
  71. if (ac == -8)
  72. break; // continue with more bits
  73. PUT_COEFF(ac);
  74. }
  75. // 8 bits per coefficient
  76. ALIGN(8);
  77. if (get_bits_left(gb) < (coeff << 3))
  78. return AVERROR_INVALIDDATA;
  79. while (coeff) {
  80. ac = get_sbits(gb, 8);
  81. PUT_COEFF(ac);
  82. }
  83. PUT_COEFF(dc);
  84. return 1;
  85. }
  86. /**
  87. * @brief decode one rtjpeg YUV420 frame
  88. * @param c context, must be initialized via ff_rtjpeg_decode_init
  89. * @param f AVFrame to place decoded frame into. If parts of the frame
  90. * are not coded they are left unchanged, so consider initializing it
  91. * @param buf buffer containing input data
  92. * @param buf_size length of input data in bytes
  93. * @return number of bytes consumed from the input buffer
  94. */
  95. int ff_rtjpeg_decode_frame_yuv420(RTJpegContext *c, AVFrame *f,
  96. const uint8_t *buf, int buf_size) {
  97. GetBitContext gb;
  98. int w = c->w / 16, h = c->h / 16;
  99. int x, y, ret;
  100. uint8_t *y1 = f->data[0], *y2 = f->data[0] + 8 * f->linesize[0];
  101. uint8_t *u = f->data[1], *v = f->data[2];
  102. if ((ret = init_get_bits8(&gb, buf, buf_size)) < 0)
  103. return ret;
  104. for (y = 0; y < h; y++) {
  105. for (x = 0; x < w; x++) {
  106. #define BLOCK(quant, dst, stride) do { \
  107. int res = get_block(&gb, block, c->scan, quant); \
  108. if (res < 0) \
  109. return res; \
  110. if (res > 0) \
  111. c->idsp.idct_put(dst, stride, block); \
  112. } while (0)
  113. int16_t *block = c->block;
  114. BLOCK(c->lquant, y1, f->linesize[0]);
  115. y1 += 8;
  116. BLOCK(c->lquant, y1, f->linesize[0]);
  117. y1 += 8;
  118. BLOCK(c->lquant, y2, f->linesize[0]);
  119. y2 += 8;
  120. BLOCK(c->lquant, y2, f->linesize[0]);
  121. y2 += 8;
  122. BLOCK(c->cquant, u, f->linesize[1]);
  123. u += 8;
  124. BLOCK(c->cquant, v, f->linesize[2]);
  125. v += 8;
  126. }
  127. y1 += 2 * 8 * (f->linesize[0] - w);
  128. y2 += 2 * 8 * (f->linesize[0] - w);
  129. u += 8 * (f->linesize[1] - w);
  130. v += 8 * (f->linesize[2] - w);
  131. }
  132. return get_bits_count(&gb) / 8;
  133. }
  134. /**
  135. * @brief initialize an RTJpegContext, may be called multiple times
  136. * @param c context to initialize
  137. * @param width width of image, will be rounded down to the nearest multiple
  138. * of 16 for decoding
  139. * @param height height of image, will be rounded down to the nearest multiple
  140. * of 16 for decoding
  141. * @param lquant luma quantization table to use
  142. * @param cquant chroma quantization table to use
  143. */
  144. void ff_rtjpeg_decode_init(RTJpegContext *c, int width, int height,
  145. const uint32_t *lquant, const uint32_t *cquant) {
  146. int i;
  147. for (i = 0; i < 64; i++) {
  148. int p = c->idsp.idct_permutation[i];
  149. c->lquant[p] = lquant[i];
  150. c->cquant[p] = cquant[i];
  151. }
  152. c->w = width;
  153. c->h = height;
  154. }
  155. void ff_rtjpeg_init(RTJpegContext *c, AVCodecContext *avctx)
  156. {
  157. int i;
  158. ff_idctdsp_init(&c->idsp, avctx);
  159. for (i = 0; i < 64; i++) {
  160. int z = ff_zigzag_direct[i];
  161. z = ((z << 3) | (z >> 3)) & 63; // rtjpeg uses a transposed variant
  162. // permute the scan and quantization tables for the chosen idct
  163. c->scan[i] = c->idsp.idct_permutation[z];
  164. }
  165. }