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.

217 lines
6.2KB

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