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.

272 lines
8.2KB

  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
  23. * Intel Indeo 2 decoder.
  24. */
  25. #include "libavutil/attributes.h"
  26. #define BITSTREAM_READER_LE
  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 pitch, const uint8_t *table)
  47. {
  48. int i;
  49. int j;
  50. int out = 0;
  51. if (width & 1)
  52. return AVERROR_INVALIDDATA;
  53. /* first line contain absolute values, other lines contain deltas */
  54. while (out < width) {
  55. int 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 AVERROR_INVALIDDATA;
  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 += pitch;
  68. for (j = 1; j < height; j++) {
  69. out = 0;
  70. if (get_bits_left(&ctx->gb) <= 0)
  71. return AVERROR_INVALIDDATA;
  72. while (out < width) {
  73. int 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 - pitch];
  80. out++;
  81. }
  82. } else { /* add two deltas from table */
  83. int t = dst[out - pitch] + (table[c * 2] - 128);
  84. t = av_clip_uint8(t);
  85. dst[out] = t;
  86. out++;
  87. t = dst[out - pitch] + (table[(c * 2) + 1] - 128);
  88. t = av_clip_uint8(t);
  89. dst[out] = t;
  90. out++;
  91. }
  92. }
  93. dst += pitch;
  94. }
  95. return 0;
  96. }
  97. static int ir2_decode_plane_inter(Ir2Context *ctx, int width, int height, uint8_t *dst,
  98. int pitch, 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. if (get_bits_left(&ctx->gb) <= 0)
  109. return AVERROR_INVALIDDATA;
  110. while (out < width) {
  111. c = ir2_get_code(&ctx->gb);
  112. if (c >= 0x80) { /* we have a skip */
  113. c -= 0x7F;
  114. out += c * 2;
  115. } else { /* add two deltas from table */
  116. t = dst[out] + (((table[c * 2] - 128)*3) >> 2);
  117. t = av_clip_uint8(t);
  118. dst[out] = t;
  119. out++;
  120. t = dst[out] + (((table[(c * 2) + 1] - 128)*3) >> 2);
  121. t = av_clip_uint8(t);
  122. dst[out] = t;
  123. out++;
  124. }
  125. }
  126. dst += pitch;
  127. }
  128. return 0;
  129. }
  130. static int ir2_decode_frame(AVCodecContext *avctx,
  131. void *data, int *got_frame,
  132. AVPacket *avpkt)
  133. {
  134. Ir2Context * const s = avctx->priv_data;
  135. const uint8_t *buf = avpkt->data;
  136. int buf_size = avpkt->size;
  137. AVFrame *picture = data;
  138. AVFrame * const p = s->picture;
  139. int start, ret;
  140. int ltab, ctab;
  141. if ((ret = ff_reget_buffer(avctx, p)) < 0)
  142. return ret;
  143. start = 48; /* hardcoded for now */
  144. if (start >= buf_size) {
  145. av_log(s->avctx, AV_LOG_ERROR, "input buffer size too small (%d)\n", buf_size);
  146. return AVERROR_INVALIDDATA;
  147. }
  148. s->decode_delta = buf[18];
  149. /* decide whether frame uses deltas or not */
  150. #ifndef BITSTREAM_READER_LE
  151. for (i = 0; i < buf_size; i++)
  152. buf[i] = ff_reverse[buf[i]];
  153. #endif
  154. if ((ret = init_get_bits8(&s->gb, buf + start, buf_size - start)) < 0)
  155. return ret;
  156. ltab = buf[0x22] & 3;
  157. ctab = buf[0x22] >> 2;
  158. if (ctab > 3) {
  159. av_log(avctx, AV_LOG_ERROR, "ctab %d is invalid\n", ctab);
  160. return AVERROR_INVALIDDATA;
  161. }
  162. if (s->decode_delta) { /* intraframe */
  163. if ((ret = ir2_decode_plane(s, avctx->width, avctx->height,
  164. p->data[0], p->linesize[0],
  165. ir2_delta_table[ltab])) < 0)
  166. return ret;
  167. /* swapped U and V */
  168. if ((ret = ir2_decode_plane(s, avctx->width >> 2, avctx->height >> 2,
  169. p->data[2], p->linesize[2],
  170. ir2_delta_table[ctab])) < 0)
  171. return ret;
  172. if ((ret = ir2_decode_plane(s, avctx->width >> 2, avctx->height >> 2,
  173. p->data[1], p->linesize[1],
  174. ir2_delta_table[ctab])) < 0)
  175. return ret;
  176. } else { /* interframe */
  177. if ((ret = ir2_decode_plane_inter(s, avctx->width, avctx->height,
  178. p->data[0], p->linesize[0],
  179. ir2_delta_table[ltab])) < 0)
  180. return ret;
  181. /* swapped U and V */
  182. if ((ret = ir2_decode_plane_inter(s, avctx->width >> 2, avctx->height >> 2,
  183. p->data[2], p->linesize[2],
  184. ir2_delta_table[ctab])) < 0)
  185. return ret;
  186. if ((ret = ir2_decode_plane_inter(s, avctx->width >> 2, avctx->height >> 2,
  187. p->data[1], p->linesize[1],
  188. ir2_delta_table[ctab])) < 0)
  189. return ret;
  190. }
  191. if ((ret = av_frame_ref(picture, p)) < 0)
  192. return ret;
  193. *got_frame = 1;
  194. return buf_size;
  195. }
  196. static av_cold int ir2_decode_init(AVCodecContext *avctx)
  197. {
  198. Ir2Context * const ic = avctx->priv_data;
  199. static VLC_TYPE vlc_tables[1 << CODE_VLC_BITS][2];
  200. ic->avctx = avctx;
  201. avctx->pix_fmt= AV_PIX_FMT_YUV410P;
  202. ic->picture = av_frame_alloc();
  203. if (!ic->picture)
  204. return AVERROR(ENOMEM);
  205. ir2_vlc.table = vlc_tables;
  206. ir2_vlc.table_allocated = 1 << CODE_VLC_BITS;
  207. #ifdef BITSTREAM_READER_LE
  208. init_vlc(&ir2_vlc, CODE_VLC_BITS, IR2_CODES,
  209. &ir2_codes[0][1], 4, 2,
  210. &ir2_codes[0][0], 4, 2, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
  211. #else
  212. init_vlc(&ir2_vlc, CODE_VLC_BITS, IR2_CODES,
  213. &ir2_codes[0][1], 4, 2,
  214. &ir2_codes[0][0], 4, 2, INIT_VLC_USE_NEW_STATIC);
  215. #endif
  216. return 0;
  217. }
  218. static av_cold int ir2_decode_end(AVCodecContext *avctx)
  219. {
  220. Ir2Context * const ic = avctx->priv_data;
  221. av_frame_free(&ic->picture);
  222. return 0;
  223. }
  224. AVCodec ff_indeo2_decoder = {
  225. .name = "indeo2",
  226. .long_name = NULL_IF_CONFIG_SMALL("Intel Indeo 2"),
  227. .type = AVMEDIA_TYPE_VIDEO,
  228. .id = AV_CODEC_ID_INDEO2,
  229. .priv_data_size = sizeof(Ir2Context),
  230. .init = ir2_decode_init,
  231. .close = ir2_decode_end,
  232. .decode = ir2_decode_frame,
  233. .capabilities = AV_CODEC_CAP_DR1,
  234. };