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.

228 lines
6.8KB

  1. /*
  2. * Intel Indeo 2 codec
  3. * Copyright (c) 2005 Konstantin Shishkov
  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. /**
  22. * @file libavcodec/indeo2.c
  23. * Intel Indeo 2 decoder.
  24. */
  25. #define ALT_BITSTREAM_READER_LE
  26. #include "avcodec.h"
  27. #include "get_bits.h"
  28. #include "indeo2data.h"
  29. typedef struct Ir2Context{
  30. AVCodecContext *avctx;
  31. AVFrame picture;
  32. GetBitContext gb;
  33. int decode_delta;
  34. } Ir2Context;
  35. #define CODE_VLC_BITS 14
  36. static VLC ir2_vlc;
  37. /* Indeo 2 codes are in range 0x01..0x7F and 0x81..0x90 */
  38. static inline int ir2_get_code(GetBitContext *gb)
  39. {
  40. return get_vlc2(gb, ir2_vlc.table, CODE_VLC_BITS, 1) + 1;
  41. }
  42. static int ir2_decode_plane(Ir2Context *ctx, int width, int height, uint8_t *dst, int stride,
  43. const uint8_t *table)
  44. {
  45. int i;
  46. int j;
  47. int out = 0;
  48. int c;
  49. int t;
  50. if(width&1)
  51. return -1;
  52. /* first line contain absolute values, other lines contain deltas */
  53. while (out < width){
  54. c = ir2_get_code(&ctx->gb);
  55. if(c >= 0x80) { /* we have a run */
  56. c -= 0x7F;
  57. if(out + c*2 > width)
  58. return -1;
  59. for (i = 0; i < c * 2; i++)
  60. dst[out++] = 0x80;
  61. } else { /* copy two values from table */
  62. dst[out++] = table[c * 2];
  63. dst[out++] = table[(c * 2) + 1];
  64. }
  65. }
  66. dst += stride;
  67. for (j = 1; j < height; j++){
  68. out = 0;
  69. while (out < width){
  70. c = ir2_get_code(&ctx->gb);
  71. if(c >= 0x80) { /* we have a skip */
  72. c -= 0x7F;
  73. if(out + c*2 > width)
  74. return -1;
  75. for (i = 0; i < c * 2; i++) {
  76. dst[out] = dst[out - stride];
  77. out++;
  78. }
  79. } else { /* add two deltas from table */
  80. t = dst[out - stride] + (table[c * 2] - 128);
  81. t= av_clip_uint8(t);
  82. dst[out] = t;
  83. out++;
  84. t = dst[out - stride] + (table[(c * 2) + 1] - 128);
  85. t= av_clip_uint8(t);
  86. dst[out] = t;
  87. out++;
  88. }
  89. }
  90. dst += stride;
  91. }
  92. return 0;
  93. }
  94. static int ir2_decode_plane_inter(Ir2Context *ctx, int width, int height, uint8_t *dst, int stride,
  95. const uint8_t *table)
  96. {
  97. int j;
  98. int out = 0;
  99. int c;
  100. int t;
  101. if(width&1)
  102. return -1;
  103. for (j = 0; j < height; j++){
  104. out = 0;
  105. while (out < width){
  106. c = ir2_get_code(&ctx->gb);
  107. if(c >= 0x80) { /* we have a skip */
  108. c -= 0x7F;
  109. out += c * 2;
  110. } else { /* add two deltas from table */
  111. t = dst[out] + (((table[c * 2] - 128)*3) >> 2);
  112. t= av_clip_uint8(t);
  113. dst[out] = t;
  114. out++;
  115. t = dst[out] + (((table[(c * 2) + 1] - 128)*3) >> 2);
  116. t= av_clip_uint8(t);
  117. dst[out] = t;
  118. out++;
  119. }
  120. }
  121. dst += stride;
  122. }
  123. return 0;
  124. }
  125. static int ir2_decode_frame(AVCodecContext *avctx,
  126. void *data, int *data_size,
  127. AVPacket *avpkt)
  128. {
  129. const uint8_t *buf = avpkt->data;
  130. int buf_size = avpkt->size;
  131. Ir2Context * const s = avctx->priv_data;
  132. AVFrame *picture = data;
  133. AVFrame * const p= (AVFrame*)&s->picture;
  134. int start;
  135. if(p->data[0])
  136. avctx->release_buffer(avctx, p);
  137. p->reference = 1;
  138. p->buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  139. if (avctx->reget_buffer(avctx, p)) {
  140. av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  141. return -1;
  142. }
  143. s->decode_delta = buf[18];
  144. /* decide whether frame uses deltas or not */
  145. #ifndef ALT_BITSTREAM_READER_LE
  146. for (i = 0; i < buf_size; i++)
  147. buf[i] = ff_reverse[buf[i]];
  148. #endif
  149. start = 48; /* hardcoded for now */
  150. init_get_bits(&s->gb, buf + start, buf_size - start);
  151. if (s->decode_delta) { /* intraframe */
  152. ir2_decode_plane(s, avctx->width, avctx->height,
  153. s->picture.data[0], s->picture.linesize[0], ir2_luma_table);
  154. /* swapped U and V */
  155. ir2_decode_plane(s, avctx->width >> 2, avctx->height >> 2,
  156. s->picture.data[2], s->picture.linesize[2], ir2_luma_table);
  157. ir2_decode_plane(s, avctx->width >> 2, avctx->height >> 2,
  158. s->picture.data[1], s->picture.linesize[1], ir2_luma_table);
  159. } else { /* interframe */
  160. ir2_decode_plane_inter(s, avctx->width, avctx->height,
  161. s->picture.data[0], s->picture.linesize[0], ir2_luma_table);
  162. /* swapped U and V */
  163. ir2_decode_plane_inter(s, avctx->width >> 2, avctx->height >> 2,
  164. s->picture.data[2], s->picture.linesize[2], ir2_luma_table);
  165. ir2_decode_plane_inter(s, avctx->width >> 2, avctx->height >> 2,
  166. s->picture.data[1], s->picture.linesize[1], ir2_luma_table);
  167. }
  168. *picture= *(AVFrame*)&s->picture;
  169. *data_size = sizeof(AVPicture);
  170. return buf_size;
  171. }
  172. static av_cold int ir2_decode_init(AVCodecContext *avctx){
  173. Ir2Context * const ic = avctx->priv_data;
  174. static VLC_TYPE vlc_tables[1 << CODE_VLC_BITS][2];
  175. ic->avctx = avctx;
  176. avctx->pix_fmt= PIX_FMT_YUV410P;
  177. ir2_vlc.table = vlc_tables;
  178. ir2_vlc.table_allocated = 1 << CODE_VLC_BITS;
  179. #ifdef ALT_BITSTREAM_READER_LE
  180. init_vlc(&ir2_vlc, CODE_VLC_BITS, IR2_CODES,
  181. &ir2_codes[0][1], 4, 2,
  182. &ir2_codes[0][0], 4, 2, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
  183. #else
  184. init_vlc(&ir2_vlc, CODE_VLC_BITS, IR2_CODES,
  185. &ir2_codes[0][1], 4, 2,
  186. &ir2_codes[0][0], 4, 2, INIT_VLC_USE_NEW_STATIC);
  187. #endif
  188. return 0;
  189. }
  190. AVCodec indeo2_decoder = {
  191. "indeo2",
  192. CODEC_TYPE_VIDEO,
  193. CODEC_ID_INDEO2,
  194. sizeof(Ir2Context),
  195. ir2_decode_init,
  196. NULL,
  197. NULL,
  198. ir2_decode_frame,
  199. CODEC_CAP_DR1,
  200. .long_name = NULL_IF_CONFIG_SMALL("Intel Indeo 2"),
  201. };