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.

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