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.

165 lines
5.5KB

  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 "common.h"
  22. #include "bitstream.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. *
  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, DCTELEM *block, uint8_t *scan,
  44. 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. // normally we would only need to clear the (63 - coeff) last values,
  54. // but since we do not know where they are we just clear the whole block
  55. memset(block, 0, 64 * sizeof(DCTELEM));
  56. // 2 bits per coefficient
  57. while (coeff) {
  58. ac = get_sbits(gb, 2);
  59. if (ac == -2)
  60. break; // continue with more bits
  61. PUT_COEFF(ac);
  62. }
  63. // 4 bits per coefficient
  64. ALIGN(4);
  65. while (coeff) {
  66. ac = get_sbits(gb, 4);
  67. if (ac == -8)
  68. break; // continue with more bits
  69. PUT_COEFF(ac);
  70. }
  71. // 8 bits per coefficient
  72. ALIGN(8);
  73. while (coeff) {
  74. ac = get_sbits(gb, 8);
  75. PUT_COEFF(ac);
  76. }
  77. PUT_COEFF(dc);
  78. return 1;
  79. }
  80. /**
  81. * \brief decode one rtjpeg YUV420 frame
  82. * \param c context, must be initialized via rtjpeg_decode_init
  83. * \param f AVFrame to place decoded frame into. If parts of the frame
  84. * are not coded they are left unchanged, so consider initializing it
  85. * \param buf buffer containing input data
  86. * \param buf_size length of input data in bytes
  87. * \return number of bytes consumed from the input buffer
  88. */
  89. int rtjpeg_decode_frame_yuv420(RTJpegContext *c, AVFrame *f,
  90. uint8_t *buf, int buf_size) {
  91. GetBitContext gb;
  92. int w = c->w / 16, h = c->h / 16;
  93. int x, y;
  94. void *y1 = f->data[0], *y2 = f->data[0] + 8 * f->linesize[0];
  95. void *u = f->data[1], *v = f->data[2];
  96. init_get_bits(&gb, buf, buf_size * 8);
  97. for (y = 0; y < h; y++) {
  98. for (x = 0; x < w; x++) {
  99. if (get_block(&gb, c->block, c->scan, c->lquant))
  100. c->dsp->idct_put(y1, f->linesize[0], c->block);
  101. y1 += 8;
  102. if (get_block(&gb, c->block, c->scan, c->lquant))
  103. c->dsp->idct_put(y1, f->linesize[0], c->block);
  104. y1 += 8;
  105. if (get_block(&gb, c->block, c->scan, c->lquant))
  106. c->dsp->idct_put(y2, f->linesize[0], c->block);
  107. y2 += 8;
  108. if (get_block(&gb, c->block, c->scan, c->lquant))
  109. c->dsp->idct_put(y2, f->linesize[0], c->block);
  110. y2 += 8;
  111. if (get_block(&gb, c->block, c->scan, c->cquant))
  112. c->dsp->idct_put(u, f->linesize[1], c->block);
  113. u += 8;
  114. if (get_block(&gb, c->block, c->scan, c->cquant))
  115. c->dsp->idct_put(v, f->linesize[2], c->block);
  116. v += 8;
  117. }
  118. y1 += 2 * 8 * (f->linesize[0] - w);
  119. y2 += 2 * 8 * (f->linesize[0] - w);
  120. u += 8 * (f->linesize[1] - w);
  121. v += 8 * (f->linesize[2] - w);
  122. }
  123. return get_bits_count(&gb) / 8;
  124. }
  125. /**
  126. * \brief initialize an RTJpegContext, may be called multiple times
  127. * \param c context to initialize
  128. * \param dsp specifies the idct to use for decoding
  129. * \param width width of image, will be rounded down to the nearest multiple
  130. * of 16 for decoding
  131. * \param height height of image, will be rounded down to the nearest multiple
  132. * of 16 for decoding
  133. * \param lquant luma quantization table to use
  134. * \param cquant chroma quantization table to use
  135. */
  136. void rtjpeg_decode_init(RTJpegContext *c, DSPContext *dsp,
  137. int width, int height,
  138. uint32_t *lquant, uint32_t *cquant) {
  139. int i;
  140. c->dsp = dsp;
  141. for (i = 0; i < 64; i++) {
  142. int z = ff_zigzag_direct[i];
  143. int p = c->dsp->idct_permutation[i];
  144. z = ((z << 3) | (z >> 3)) & 63; // rtjpeg uses a transposed variant
  145. // permute the scan and quantization tables for the chosen idct
  146. c->scan[i] = c->dsp->idct_permutation[z];
  147. c->lquant[p] = lquant[i];
  148. c->cquant[p] = cquant[i];
  149. }
  150. c->w = width;
  151. c->h = height;
  152. }