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.

260 lines
7.9KB

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