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.

275 lines
8.3KB

  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. #include "libavutil/thread.h"
  27. #define BITSTREAM_READER_LE
  28. #include "avcodec.h"
  29. #include "get_bits.h"
  30. #include "indeo2data.h"
  31. #include "internal.h"
  32. #include "mathops.h"
  33. typedef struct Ir2Context{
  34. AVCodecContext *avctx;
  35. AVFrame *picture;
  36. GetBitContext gb;
  37. int decode_delta;
  38. } Ir2Context;
  39. #define CODE_VLC_BITS 14
  40. static VLC ir2_vlc;
  41. /* Indeo 2 codes are in range 0x01..0x7F and 0x81..0x90 */
  42. static inline int ir2_get_code(GetBitContext *gb)
  43. {
  44. return get_vlc2(gb, ir2_vlc.table, CODE_VLC_BITS, 1);
  45. }
  46. static int ir2_decode_plane(Ir2Context *ctx, int width, int height, uint8_t *dst,
  47. int pitch, const uint8_t *table)
  48. {
  49. int i;
  50. int j;
  51. int out = 0;
  52. if ((width & 1) || width * height / (2*(IR2_CODES - 0x7F)) > get_bits_left(&ctx->gb))
  53. return AVERROR_INVALIDDATA;
  54. /* first line contain absolute values, other lines contain deltas */
  55. while (out < width) {
  56. int c = ir2_get_code(&ctx->gb);
  57. if (c >= 0x80) { /* we have a run */
  58. c -= 0x7F;
  59. if (out + c*2 > width)
  60. return AVERROR_INVALIDDATA;
  61. for (i = 0; i < c * 2; i++)
  62. dst[out++] = 0x80;
  63. } else { /* copy two values from table */
  64. if (c <= 0)
  65. return AVERROR_INVALIDDATA;
  66. dst[out++] = table[c * 2];
  67. dst[out++] = table[(c * 2) + 1];
  68. }
  69. }
  70. dst += pitch;
  71. for (j = 1; j < height; j++) {
  72. out = 0;
  73. while (out < width) {
  74. int c;
  75. if (get_bits_left(&ctx->gb) <= 0)
  76. return AVERROR_INVALIDDATA;
  77. c = ir2_get_code(&ctx->gb);
  78. if (c >= 0x80) { /* we have a skip */
  79. c -= 0x7F;
  80. if (out + c*2 > width)
  81. return AVERROR_INVALIDDATA;
  82. for (i = 0; i < c * 2; i++) {
  83. dst[out] = dst[out - pitch];
  84. out++;
  85. }
  86. } else { /* add two deltas from table */
  87. int t;
  88. if (c <= 0)
  89. return AVERROR_INVALIDDATA;
  90. t = dst[out - pitch] + (table[c * 2] - 128);
  91. t = av_clip_uint8(t);
  92. dst[out] = t;
  93. out++;
  94. t = dst[out - pitch] + (table[(c * 2) + 1] - 128);
  95. t = av_clip_uint8(t);
  96. dst[out] = t;
  97. out++;
  98. }
  99. }
  100. dst += pitch;
  101. }
  102. return 0;
  103. }
  104. static int ir2_decode_plane_inter(Ir2Context *ctx, int width, int height, uint8_t *dst,
  105. int pitch, const uint8_t *table)
  106. {
  107. int j;
  108. int out = 0;
  109. int c;
  110. int t;
  111. if (width & 1)
  112. return AVERROR_INVALIDDATA;
  113. for (j = 0; j < height; j++) {
  114. out = 0;
  115. while (out < width) {
  116. if (get_bits_left(&ctx->gb) <= 0)
  117. return AVERROR_INVALIDDATA;
  118. c = ir2_get_code(&ctx->gb);
  119. if (c >= 0x80) { /* we have a skip */
  120. c -= 0x7F;
  121. out += c * 2;
  122. } else { /* add two deltas from table */
  123. if (c <= 0)
  124. return AVERROR_INVALIDDATA;
  125. t = dst[out] + (((table[c * 2] - 128)*3) >> 2);
  126. t = av_clip_uint8(t);
  127. dst[out] = t;
  128. out++;
  129. t = dst[out] + (((table[(c * 2) + 1] - 128)*3) >> 2);
  130. t = av_clip_uint8(t);
  131. dst[out] = t;
  132. out++;
  133. }
  134. }
  135. dst += pitch;
  136. }
  137. return 0;
  138. }
  139. static int ir2_decode_frame(AVCodecContext *avctx,
  140. void *data, int *got_frame,
  141. AVPacket *avpkt)
  142. {
  143. Ir2Context * const s = avctx->priv_data;
  144. const uint8_t *buf = avpkt->data;
  145. int buf_size = avpkt->size;
  146. AVFrame *picture = data;
  147. AVFrame * const p = s->picture;
  148. int start, ret;
  149. int ltab, ctab;
  150. if ((ret = ff_reget_buffer(avctx, p, 0)) < 0)
  151. return ret;
  152. start = 48; /* hardcoded for now */
  153. if (start >= buf_size) {
  154. av_log(s->avctx, AV_LOG_ERROR, "input buffer size too small (%d)\n", buf_size);
  155. return AVERROR_INVALIDDATA;
  156. }
  157. s->decode_delta = buf[18];
  158. /* decide whether frame uses deltas or not */
  159. if ((ret = init_get_bits8(&s->gb, buf + start, buf_size - start)) < 0)
  160. return ret;
  161. ltab = buf[0x22] & 3;
  162. ctab = buf[0x22] >> 2;
  163. if (ctab > 3) {
  164. av_log(avctx, AV_LOG_ERROR, "ctab %d is invalid\n", ctab);
  165. return AVERROR_INVALIDDATA;
  166. }
  167. if (s->decode_delta) { /* intraframe */
  168. if ((ret = ir2_decode_plane(s, avctx->width, avctx->height,
  169. p->data[0], p->linesize[0],
  170. ir2_delta_table[ltab])) < 0)
  171. return ret;
  172. /* swapped U and V */
  173. if ((ret = ir2_decode_plane(s, avctx->width >> 2, avctx->height >> 2,
  174. p->data[2], p->linesize[2],
  175. ir2_delta_table[ctab])) < 0)
  176. return ret;
  177. if ((ret = ir2_decode_plane(s, avctx->width >> 2, avctx->height >> 2,
  178. p->data[1], p->linesize[1],
  179. ir2_delta_table[ctab])) < 0)
  180. return ret;
  181. } else { /* interframe */
  182. if ((ret = ir2_decode_plane_inter(s, avctx->width, avctx->height,
  183. p->data[0], p->linesize[0],
  184. ir2_delta_table[ltab])) < 0)
  185. return ret;
  186. /* swapped U and V */
  187. if ((ret = ir2_decode_plane_inter(s, avctx->width >> 2, avctx->height >> 2,
  188. p->data[2], p->linesize[2],
  189. ir2_delta_table[ctab])) < 0)
  190. return ret;
  191. if ((ret = ir2_decode_plane_inter(s, avctx->width >> 2, avctx->height >> 2,
  192. p->data[1], p->linesize[1],
  193. ir2_delta_table[ctab])) < 0)
  194. return ret;
  195. }
  196. if ((ret = av_frame_ref(picture, p)) < 0)
  197. return ret;
  198. *got_frame = 1;
  199. return buf_size;
  200. }
  201. static av_cold void ir2_init_static(void)
  202. {
  203. INIT_VLC_STATIC_FROM_LENGTHS(&ir2_vlc, CODE_VLC_BITS, IR2_CODES,
  204. &ir2_tab[0][1], 2, &ir2_tab[0][0], 2, 1,
  205. 0, INIT_VLC_OUTPUT_LE, 1 << CODE_VLC_BITS);
  206. }
  207. static av_cold int ir2_decode_init(AVCodecContext *avctx)
  208. {
  209. static AVOnce init_static_once = AV_ONCE_INIT;
  210. Ir2Context * const ic = avctx->priv_data;
  211. ic->avctx = avctx;
  212. avctx->pix_fmt= AV_PIX_FMT_YUV410P;
  213. ic->picture = av_frame_alloc();
  214. if (!ic->picture)
  215. return AVERROR(ENOMEM);
  216. ff_thread_once(&init_static_once, ir2_init_static);
  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 = AV_CODEC_CAP_DR1,
  235. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  236. };