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.

279 lines
8.4KB

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