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.

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