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.

244 lines
7.3KB

  1. /*
  2. * Intel Indeo 2 codec
  3. * Copyright (c) 2005 Konstantin Shishkov
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Intel Indeo 2 decoder.
  24. */
  25. #define BITSTREAM_READER_LE
  26. #include "avcodec.h"
  27. #include "get_bits.h"
  28. #include "indeo2data.h"
  29. #include "libavutil/common.h"
  30. typedef struct Ir2Context{
  31. AVCodecContext *avctx;
  32. AVFrame picture;
  33. GetBitContext gb;
  34. int decode_delta;
  35. } Ir2Context;
  36. #define CODE_VLC_BITS 14
  37. static VLC ir2_vlc;
  38. /* Indeo 2 codes are in range 0x01..0x7F and 0x81..0x90 */
  39. static inline int ir2_get_code(GetBitContext *gb)
  40. {
  41. return get_vlc2(gb, ir2_vlc.table, CODE_VLC_BITS, 1) + 1;
  42. }
  43. static int ir2_decode_plane(Ir2Context *ctx, int width, int height, uint8_t *dst, int stride,
  44. const uint8_t *table)
  45. {
  46. int i;
  47. int j;
  48. int out = 0;
  49. int c;
  50. int t;
  51. if(width&1)
  52. return -1;
  53. /* first line contain absolute values, other lines contain deltas */
  54. while (out < width){
  55. c = ir2_get_code(&ctx->gb);
  56. if(c >= 0x80) { /* we have a run */
  57. c -= 0x7F;
  58. if(out + c*2 > width)
  59. return -1;
  60. for (i = 0; i < c * 2; i++)
  61. dst[out++] = 0x80;
  62. } else { /* copy two values from table */
  63. dst[out++] = table[c * 2];
  64. dst[out++] = table[(c * 2) + 1];
  65. }
  66. }
  67. dst += stride;
  68. for (j = 1; j < height; j++){
  69. out = 0;
  70. while (out < width){
  71. c = ir2_get_code(&ctx->gb);
  72. if(c >= 0x80) { /* we have a skip */
  73. c -= 0x7F;
  74. if(out + c*2 > width)
  75. return -1;
  76. for (i = 0; i < c * 2; i++) {
  77. dst[out] = dst[out - stride];
  78. out++;
  79. }
  80. } else { /* add two deltas from table */
  81. t = dst[out - stride] + (table[c * 2] - 128);
  82. t= av_clip_uint8(t);
  83. dst[out] = t;
  84. out++;
  85. t = dst[out - stride] + (table[(c * 2) + 1] - 128);
  86. t= av_clip_uint8(t);
  87. dst[out] = t;
  88. out++;
  89. }
  90. }
  91. dst += stride;
  92. }
  93. return 0;
  94. }
  95. static int ir2_decode_plane_inter(Ir2Context *ctx, int width, int height, uint8_t *dst, int stride,
  96. const uint8_t *table)
  97. {
  98. int j;
  99. int out = 0;
  100. int c;
  101. int t;
  102. if(width&1)
  103. return -1;
  104. for (j = 0; j < height; j++){
  105. out = 0;
  106. while (out < width){
  107. c = ir2_get_code(&ctx->gb);
  108. if(c >= 0x80) { /* we have a skip */
  109. c -= 0x7F;
  110. out += c * 2;
  111. } else { /* add two deltas from table */
  112. t = dst[out] + (((table[c * 2] - 128)*3) >> 2);
  113. t= av_clip_uint8(t);
  114. dst[out] = t;
  115. out++;
  116. t = dst[out] + (((table[(c * 2) + 1] - 128)*3) >> 2);
  117. t= av_clip_uint8(t);
  118. dst[out] = t;
  119. out++;
  120. }
  121. }
  122. dst += stride;
  123. }
  124. return 0;
  125. }
  126. static int ir2_decode_frame(AVCodecContext *avctx,
  127. void *data, int *data_size,
  128. AVPacket *avpkt)
  129. {
  130. const uint8_t *buf = avpkt->data;
  131. int buf_size = avpkt->size;
  132. Ir2Context * const s = avctx->priv_data;
  133. AVFrame *picture = data;
  134. AVFrame * const p = &s->picture;
  135. int start;
  136. if(p->data[0])
  137. avctx->release_buffer(avctx, p);
  138. p->reference = 1;
  139. p->buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  140. if (avctx->reget_buffer(avctx, p)) {
  141. av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  142. return -1;
  143. }
  144. start = 48; /* hardcoded for now */
  145. if (start >= buf_size) {
  146. av_log(s->avctx, AV_LOG_ERROR, "input buffer size too small (%d)\n", buf_size);
  147. return AVERROR_INVALIDDATA;
  148. }
  149. s->decode_delta = buf[18];
  150. /* decide whether frame uses deltas or not */
  151. #ifndef BITSTREAM_READER_LE
  152. for (i = 0; i < buf_size; i++)
  153. buf[i] = av_reverse[buf[i]];
  154. #endif
  155. init_get_bits(&s->gb, buf + start, (buf_size - start) * 8);
  156. if (s->decode_delta) { /* intraframe */
  157. ir2_decode_plane(s, avctx->width, avctx->height,
  158. s->picture.data[0], s->picture.linesize[0], ir2_luma_table);
  159. /* swapped U and V */
  160. ir2_decode_plane(s, avctx->width >> 2, avctx->height >> 2,
  161. s->picture.data[2], s->picture.linesize[2], ir2_luma_table);
  162. ir2_decode_plane(s, avctx->width >> 2, avctx->height >> 2,
  163. s->picture.data[1], s->picture.linesize[1], ir2_luma_table);
  164. } else { /* interframe */
  165. ir2_decode_plane_inter(s, avctx->width, avctx->height,
  166. s->picture.data[0], s->picture.linesize[0], ir2_luma_table);
  167. /* swapped U and V */
  168. ir2_decode_plane_inter(s, avctx->width >> 2, avctx->height >> 2,
  169. s->picture.data[2], s->picture.linesize[2], ir2_luma_table);
  170. ir2_decode_plane_inter(s, avctx->width >> 2, avctx->height >> 2,
  171. s->picture.data[1], s->picture.linesize[1], ir2_luma_table);
  172. }
  173. *picture = s->picture;
  174. *data_size = sizeof(AVPicture);
  175. return buf_size;
  176. }
  177. static av_cold int ir2_decode_init(AVCodecContext *avctx){
  178. Ir2Context * const ic = avctx->priv_data;
  179. static VLC_TYPE vlc_tables[1 << CODE_VLC_BITS][2];
  180. ic->avctx = avctx;
  181. avctx->pix_fmt= PIX_FMT_YUV410P;
  182. ir2_vlc.table = vlc_tables;
  183. ir2_vlc.table_allocated = 1 << CODE_VLC_BITS;
  184. #ifdef BITSTREAM_READER_LE
  185. init_vlc(&ir2_vlc, CODE_VLC_BITS, IR2_CODES,
  186. &ir2_codes[0][1], 4, 2,
  187. &ir2_codes[0][0], 4, 2, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
  188. #else
  189. init_vlc(&ir2_vlc, CODE_VLC_BITS, IR2_CODES,
  190. &ir2_codes[0][1], 4, 2,
  191. &ir2_codes[0][0], 4, 2, INIT_VLC_USE_NEW_STATIC);
  192. #endif
  193. return 0;
  194. }
  195. static av_cold int ir2_decode_end(AVCodecContext *avctx){
  196. Ir2Context * const ic = avctx->priv_data;
  197. AVFrame *pic = &ic->picture;
  198. if (pic->data[0])
  199. avctx->release_buffer(avctx, pic);
  200. return 0;
  201. }
  202. AVCodec ff_indeo2_decoder = {
  203. .name = "indeo2",
  204. .type = AVMEDIA_TYPE_VIDEO,
  205. .id = CODEC_ID_INDEO2,
  206. .priv_data_size = sizeof(Ir2Context),
  207. .init = ir2_decode_init,
  208. .close = ir2_decode_end,
  209. .decode = ir2_decode_frame,
  210. .capabilities = CODEC_CAP_DR1,
  211. .long_name = NULL_IF_CONFIG_SMALL("Intel Indeo 2"),
  212. };