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.

224 lines
6.6KB

  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 indeo2.c
  23. * Intel Indeo 2 decoder.
  24. */
  25. #define ALT_BITSTREAM_READER_LE
  26. #include "avcodec.h"
  27. #include "bitstream.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. const uint8_t *buf, int buf_size)
  128. {
  129. Ir2Context * const s = avctx->priv_data;
  130. AVFrame *picture = data;
  131. AVFrame * const p= (AVFrame*)&s->picture;
  132. int start;
  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 av_cold 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. #ifdef ALT_BITSTREAM_READER_LE
  176. init_vlc(&ir2_vlc, CODE_VLC_BITS, IR2_CODES,
  177. &ir2_codes[0][1], 4, 2,
  178. &ir2_codes[0][0], 4, 2, INIT_VLC_USE_STATIC | INIT_VLC_LE);
  179. #else
  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_STATIC);
  183. #endif
  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. .long_name = NULL_IF_CONFIG_SMALL("Intel Indeo 2"),
  197. };