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.

221 lines
6.4KB

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