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.1KB

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