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.

174 lines
5.9KB

  1. /*
  2. * RTJpeg decoding functions
  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 "libavutil/common.h"
  22. #include "get_bits.h"
  23. #include "dsputil.h"
  24. #include "rtjpeg.h"
  25. #define PUT_COEFF(c) \
  26. i = scan[coeff--]; \
  27. block[i] = (c) * quant[i];
  28. //! aligns the bitstream to the give power of two
  29. #define ALIGN(a) \
  30. n = (-get_bits_count(gb)) & (a - 1); \
  31. if (n) {skip_bits(gb, n);}
  32. /**
  33. * \brief read one block from stream
  34. * \param gb 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: GetBitContext 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(GetBitContext *gb, DCTELEM *block, const uint8_t *scan,
  45. const uint32_t *quant) {
  46. int coeff, i, n;
  47. int8_t ac;
  48. uint8_t dc = get_bits(gb, 8);
  49. // block not coded
  50. if (dc == 255)
  51. return 0;
  52. // number of non-zero coefficients
  53. coeff = get_bits(gb, 6);
  54. if (get_bits_count(gb) + (coeff << 1) >= gb->size_in_bits)
  55. return -1;
  56. // normally we would only need to clear the (63 - coeff) last values,
  57. // but since we do not know where they are we just clear the whole block
  58. memset(block, 0, 64 * sizeof(DCTELEM));
  59. // 2 bits per coefficient
  60. while (coeff) {
  61. ac = get_sbits(gb, 2);
  62. if (ac == -2)
  63. break; // continue with more bits
  64. PUT_COEFF(ac);
  65. }
  66. // 4 bits per coefficient
  67. ALIGN(4);
  68. if (get_bits_count(gb) + (coeff << 2) >= gb->size_in_bits)
  69. return -1;
  70. while (coeff) {
  71. ac = get_sbits(gb, 4);
  72. if (ac == -8)
  73. break; // continue with more bits
  74. PUT_COEFF(ac);
  75. }
  76. // 8 bits per coefficient
  77. ALIGN(8);
  78. if (get_bits_count(gb) + (coeff << 3) >= gb->size_in_bits)
  79. return -1;
  80. while (coeff) {
  81. ac = get_sbits(gb, 8);
  82. PUT_COEFF(ac);
  83. }
  84. PUT_COEFF(dc);
  85. return 1;
  86. }
  87. /**
  88. * \brief decode one rtjpeg YUV420 frame
  89. * \param c context, must be initialized via rtjpeg_decode_init
  90. * \param f AVFrame to place decoded frame into. If parts of the frame
  91. * are not coded they are left unchanged, so consider initializing it
  92. * \param buf buffer containing input data
  93. * \param buf_size length of input data in bytes
  94. * \return number of bytes consumed from the input buffer
  95. */
  96. int rtjpeg_decode_frame_yuv420(RTJpegContext *c, AVFrame *f,
  97. const uint8_t *buf, int buf_size) {
  98. GetBitContext gb;
  99. int w = c->w / 16, h = c->h / 16;
  100. int x, y;
  101. uint8_t *y1 = f->data[0], *y2 = f->data[0] + 8 * f->linesize[0];
  102. uint8_t *u = f->data[1], *v = f->data[2];
  103. init_get_bits(&gb, buf, buf_size * 8);
  104. for (y = 0; y < h; y++) {
  105. for (x = 0; x < w; x++) {
  106. DCTELEM *block = c->block;
  107. if (get_block(&gb, block, c->scan, c->lquant) > 0)
  108. c->dsp->idct_put(y1, f->linesize[0], block);
  109. y1 += 8;
  110. if (get_block(&gb, block, c->scan, c->lquant) > 0)
  111. c->dsp->idct_put(y1, f->linesize[0], block);
  112. y1 += 8;
  113. if (get_block(&gb, block, c->scan, c->lquant) > 0)
  114. c->dsp->idct_put(y2, f->linesize[0], block);
  115. y2 += 8;
  116. if (get_block(&gb, block, c->scan, c->lquant) > 0)
  117. c->dsp->idct_put(y2, f->linesize[0], block);
  118. y2 += 8;
  119. if (get_block(&gb, block, c->scan, c->cquant) > 0)
  120. c->dsp->idct_put(u, f->linesize[1], block);
  121. u += 8;
  122. if (get_block(&gb, block, c->scan, c->cquant) > 0)
  123. c->dsp->idct_put(v, f->linesize[2], block);
  124. v += 8;
  125. }
  126. y1 += 2 * 8 * (f->linesize[0] - w);
  127. y2 += 2 * 8 * (f->linesize[0] - w);
  128. u += 8 * (f->linesize[1] - w);
  129. v += 8 * (f->linesize[2] - w);
  130. }
  131. return get_bits_count(&gb) / 8;
  132. }
  133. /**
  134. * \brief initialize an RTJpegContext, may be called multiple times
  135. * \param c context to initialize
  136. * \param dsp specifies the idct to use for decoding
  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 rtjpeg_decode_init(RTJpegContext *c, DSPContext *dsp,
  145. int width, int height,
  146. const uint32_t *lquant, const uint32_t *cquant) {
  147. int i;
  148. c->dsp = dsp;
  149. for (i = 0; i < 64; i++) {
  150. int z = ff_zigzag_direct[i];
  151. int p = c->dsp->idct_permutation[i];
  152. z = ((z << 3) | (z >> 3)) & 63; // rtjpeg uses a transposed variant
  153. // permute the scan and quantization tables for the chosen idct
  154. c->scan[i] = c->dsp->idct_permutation[z];
  155. c->lquant[p] = lquant[i];
  156. c->cquant[p] = cquant[i];
  157. }
  158. c->w = width;
  159. c->h = height;
  160. }