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.

163 lines
5.5KB

  1. /*
  2. * RTJpeg decoding functions
  3. * Copyright (c) 2006 Reimar Doeffinger
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "common.h"
  20. #include "bitstream.h"
  21. #include "dsputil.h"
  22. #include "rtjpeg.h"
  23. #define PUT_COEFF(c) \
  24. i = scan[coeff--]; \
  25. block[i] = (c) * quant[i];
  26. //! aligns the bitstream to the give power of two
  27. #define ALIGN(a) \
  28. n = (-get_bits_count(gb)) & (a - 1); \
  29. if (n) {skip_bits(gb, n);}
  30. /**
  31. * \brief read one block from stream
  32. * \param gb contains stream data
  33. * \param block where data is written to
  34. * \param scan array containing the mapping stream address -> block position
  35. * \param quant quantization factors
  36. *
  37. * Note: GetBitContext is used to make the code simpler, since all data is
  38. * aligned this could be done faster in a different way, e.g. as it is done
  39. * in MPlayer libmpcodecs/native/RTjpegN.c
  40. */
  41. static inline int get_block(GetBitContext *gb, DCTELEM *block, uint8_t *scan,
  42. uint32_t *quant) {
  43. int coeff, i, n;
  44. int8_t ac;
  45. uint8_t dc = get_bits(gb, 8);
  46. // block not coded
  47. if (dc == 255)
  48. return 0;
  49. // number of non-zero coefficients
  50. coeff = get_bits(gb, 6);
  51. // normally we would only need to clear the (63 - coeff) last values,
  52. // but since we do not know where they are we just clear the whole block
  53. memset(block, 0, 64 * sizeof(DCTELEM));
  54. // 2 bits per coefficient
  55. while (coeff) {
  56. ac = get_sbits(gb, 2);
  57. if (ac == -2)
  58. break; // continue with more bits
  59. PUT_COEFF(ac);
  60. }
  61. // 4 bits per coefficient
  62. ALIGN(4);
  63. while (coeff) {
  64. ac = get_sbits(gb, 4);
  65. if (ac == -8)
  66. break; // continue with more bits
  67. PUT_COEFF(ac);
  68. }
  69. // 8 bits per coefficient
  70. ALIGN(8);
  71. while (coeff) {
  72. ac = get_sbits(gb, 8);
  73. PUT_COEFF(ac);
  74. }
  75. PUT_COEFF(dc);
  76. return 1;
  77. }
  78. /**
  79. * \brief decode one rtjpeg YUV420 frame
  80. * \param c context, must be initialized via rtjpeg_decode_init
  81. * \param f AVFrame to place decoded frame into. If parts of the frame
  82. * are not coded they are left unchanged, so consider initializing it
  83. * \param buf buffer containing input data
  84. * \param buf_size length of input data in bytes
  85. * \return number of bytes consumed from the input buffer
  86. */
  87. int rtjpeg_decode_frame_yuv420(RTJpegContext *c, AVFrame *f,
  88. uint8_t *buf, int buf_size) {
  89. GetBitContext gb;
  90. int w = c->w / 16, h = c->h / 16;
  91. int x, y;
  92. void *y1 = f->data[0], *y2 = f->data[0] + 8 * f->linesize[0];
  93. void *u = f->data[1], *v = f->data[2];
  94. init_get_bits(&gb, buf, buf_size * 8);
  95. for (y = 0; y < h; y++) {
  96. for (x = 0; x < w; x++) {
  97. if (get_block(&gb, c->block, c->scan, c->lquant))
  98. c->dsp->idct_put(y1, f->linesize[0], c->block);
  99. y1 += 8;
  100. if (get_block(&gb, c->block, c->scan, c->lquant))
  101. c->dsp->idct_put(y1, f->linesize[0], c->block);
  102. y1 += 8;
  103. if (get_block(&gb, c->block, c->scan, c->lquant))
  104. c->dsp->idct_put(y2, f->linesize[0], c->block);
  105. y2 += 8;
  106. if (get_block(&gb, c->block, c->scan, c->lquant))
  107. c->dsp->idct_put(y2, f->linesize[0], c->block);
  108. y2 += 8;
  109. if (get_block(&gb, c->block, c->scan, c->cquant))
  110. c->dsp->idct_put(u, f->linesize[1], c->block);
  111. u += 8;
  112. if (get_block(&gb, c->block, c->scan, c->cquant))
  113. c->dsp->idct_put(v, f->linesize[2], c->block);
  114. v += 8;
  115. }
  116. y1 += 2 * 8 * (f->linesize[0] - w);
  117. y2 += 2 * 8 * (f->linesize[0] - w);
  118. u += 8 * (f->linesize[1] - w);
  119. v += 8 * (f->linesize[2] - w);
  120. }
  121. return get_bits_count(&gb) / 8;
  122. }
  123. /**
  124. * \brief initialize an RTJpegContext, may be called multiple times
  125. * \param c context to initialize
  126. * \param dsp specifies the idct to use for decoding
  127. * \param width width of image, will be rounded down to the nearest multiple
  128. * of 16 for decoding
  129. * \param height height of image, will be rounded down to the nearest multiple
  130. * of 16 for decoding
  131. * \param lquant luma quantization table to use
  132. * \param cquant chroma quantization table to use
  133. */
  134. void rtjpeg_decode_init(RTJpegContext *c, DSPContext *dsp,
  135. int width, int height,
  136. uint32_t *lquant, uint32_t *cquant) {
  137. int i;
  138. c->dsp = dsp;
  139. for (i = 0; i < 64; i++) {
  140. int z = ff_zigzag_direct[i];
  141. int p = c->dsp->idct_permutation[i];
  142. z = ((z << 3) | (z >> 3)) & 63; // rtjpeg uses a transposed variant
  143. // permute the scan and quantization tables for the chosen idct
  144. c->scan[i] = c->dsp->idct_permutation[z];
  145. c->lquant[p] = lquant[i];
  146. c->cquant[p] = cquant[i];
  147. }
  148. c->w = width;
  149. c->h = height;
  150. }