indeo2.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 "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. start = 48; /* hardcoded for now */
  142. if (start >= buf_size) {
  143. av_log(s->avctx, AV_LOG_ERROR, "input buffer size too small (%d)\n", buf_size);
  144. return AVERROR_INVALIDDATA;
  145. }
  146. s->decode_delta = buf[18];
  147. /* decide whether frame uses deltas or not */
  148. #ifndef ALT_BITSTREAM_READER_LE
  149. for (i = 0; i < buf_size; i++)
  150. buf[i] = ff_reverse[buf[i]];
  151. #endif
  152. init_get_bits(&s->gb, buf + start, (buf_size - start) * 8);
  153. if (s->decode_delta) { /* intraframe */
  154. ir2_decode_plane(s, avctx->width, avctx->height,
  155. s->picture.data[0], s->picture.linesize[0], ir2_luma_table);
  156. /* swapped U and V */
  157. ir2_decode_plane(s, avctx->width >> 2, avctx->height >> 2,
  158. s->picture.data[2], s->picture.linesize[2], ir2_luma_table);
  159. ir2_decode_plane(s, avctx->width >> 2, avctx->height >> 2,
  160. s->picture.data[1], s->picture.linesize[1], ir2_luma_table);
  161. } else { /* interframe */
  162. ir2_decode_plane_inter(s, avctx->width, avctx->height,
  163. s->picture.data[0], s->picture.linesize[0], ir2_luma_table);
  164. /* swapped U and V */
  165. ir2_decode_plane_inter(s, avctx->width >> 2, avctx->height >> 2,
  166. s->picture.data[2], s->picture.linesize[2], ir2_luma_table);
  167. ir2_decode_plane_inter(s, avctx->width >> 2, avctx->height >> 2,
  168. s->picture.data[1], s->picture.linesize[1], ir2_luma_table);
  169. }
  170. *picture= *(AVFrame*)&s->picture;
  171. *data_size = sizeof(AVPicture);
  172. return buf_size;
  173. }
  174. static av_cold int ir2_decode_init(AVCodecContext *avctx){
  175. Ir2Context * const ic = avctx->priv_data;
  176. ic->avctx = avctx;
  177. avctx->pix_fmt= PIX_FMT_YUV410P;
  178. if (!ir2_vlc.table)
  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_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_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. };