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.

188 lines
5.9KB

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