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.

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