rtjpeg.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 "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, 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. // 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. const uint8_t *buf, int buf_size) {
  91. DECLARE_ALIGNED_16(DCTELEM, block[64]);
  92. GetBitContext gb;
  93. int w = c->w / 16, h = c->h / 16;
  94. int x, y;
  95. uint8_t *y1 = f->data[0], *y2 = f->data[0] + 8 * f->linesize[0];
  96. uint8_t *u = f->data[1], *v = f->data[2];
  97. init_get_bits(&gb, buf, buf_size * 8);
  98. for (y = 0; y < h; y++) {
  99. for (x = 0; x < w; x++) {
  100. if (get_block(&gb, block, c->scan, c->lquant))
  101. c->dsp->idct_put(y1, f->linesize[0], block);
  102. y1 += 8;
  103. if (get_block(&gb, block, c->scan, c->lquant))
  104. c->dsp->idct_put(y1, f->linesize[0], block);
  105. y1 += 8;
  106. if (get_block(&gb, block, c->scan, c->lquant))
  107. c->dsp->idct_put(y2, f->linesize[0], block);
  108. y2 += 8;
  109. if (get_block(&gb, block, c->scan, c->lquant))
  110. c->dsp->idct_put(y2, f->linesize[0], block);
  111. y2 += 8;
  112. if (get_block(&gb, block, c->scan, c->cquant))
  113. c->dsp->idct_put(u, f->linesize[1], block);
  114. u += 8;
  115. if (get_block(&gb, block, c->scan, c->cquant))
  116. c->dsp->idct_put(v, f->linesize[2], block);
  117. v += 8;
  118. }
  119. y1 += 2 * 8 * (f->linesize[0] - w);
  120. y2 += 2 * 8 * (f->linesize[0] - w);
  121. u += 8 * (f->linesize[1] - w);
  122. v += 8 * (f->linesize[2] - w);
  123. }
  124. return get_bits_count(&gb) / 8;
  125. }
  126. /**
  127. * \brief initialize an RTJpegContext, may be called multiple times
  128. * \param c context to initialize
  129. * \param dsp specifies the idct to use for decoding
  130. * \param width width of image, will be rounded down to the nearest multiple
  131. * of 16 for decoding
  132. * \param height height of image, will be rounded down to the nearest multiple
  133. * of 16 for decoding
  134. * \param lquant luma quantization table to use
  135. * \param cquant chroma quantization table to use
  136. */
  137. void rtjpeg_decode_init(RTJpegContext *c, DSPContext *dsp,
  138. int width, int height,
  139. const uint32_t *lquant, const uint32_t *cquant) {
  140. int i;
  141. c->dsp = dsp;
  142. for (i = 0; i < 64; i++) {
  143. int z = ff_zigzag_direct[i];
  144. int p = c->dsp->idct_permutation[i];
  145. z = ((z << 3) | (z >> 3)) & 63; // rtjpeg uses a transposed variant
  146. // permute the scan and quantization tables for the chosen idct
  147. c->scan[i] = c->dsp->idct_permutation[z];
  148. c->lquant[p] = lquant[i];
  149. c->cquant[p] = cquant[i];
  150. }
  151. c->w = width;
  152. c->h = height;
  153. }