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.

266 lines
7.9KB

  1. /*
  2. * Intel Indeo 2 codec
  3. * Copyright (c) 2005 Konstantin Shishkov
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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 "bitstream.h"
  29. #include "indeo2data.h"
  30. #include "internal.h"
  31. #include "mathops.h"
  32. #include "vlc.h"
  33. typedef struct Ir2Context{
  34. AVCodecContext *avctx;
  35. AVFrame *picture;
  36. BitstreamContext bc;
  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(BitstreamContext *bc)
  43. {
  44. return bitstream_read_vlc(bc, ir2_vlc.table, CODE_VLC_BITS, 1) + 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. int c;
  53. int t;
  54. if (width & 1)
  55. return AVERROR_INVALIDDATA;
  56. /* first line contain absolute values, other lines contain deltas */
  57. while (out < width) {
  58. c = ir2_get_code(&ctx->bc);
  59. if (c >= 0x80) { /* we have a run */
  60. c -= 0x7F;
  61. if (out + c*2 > width)
  62. return AVERROR_INVALIDDATA;
  63. for (i = 0; i < c * 2; i++)
  64. dst[out++] = 0x80;
  65. } else { /* copy two values from table */
  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. c = ir2_get_code(&ctx->bc);
  75. if (c >= 0x80) { /* we have a skip */
  76. c -= 0x7F;
  77. if (out + c*2 > width)
  78. return AVERROR_INVALIDDATA;
  79. for (i = 0; i < c * 2; i++) {
  80. dst[out] = dst[out - pitch];
  81. out++;
  82. }
  83. } else { /* add two deltas from table */
  84. t = dst[out - pitch] + (table[c * 2] - 128);
  85. t = av_clip_uint8(t);
  86. dst[out] = t;
  87. out++;
  88. t = dst[out - pitch] + (table[(c * 2) + 1] - 128);
  89. t = av_clip_uint8(t);
  90. dst[out] = t;
  91. out++;
  92. }
  93. }
  94. dst += pitch;
  95. }
  96. return 0;
  97. }
  98. static int ir2_decode_plane_inter(Ir2Context *ctx, int width, int height, uint8_t *dst,
  99. int pitch, const uint8_t *table)
  100. {
  101. int j;
  102. int out = 0;
  103. int c;
  104. int t;
  105. if (width & 1)
  106. return AVERROR_INVALIDDATA;
  107. for (j = 0; j < height; j++) {
  108. out = 0;
  109. while (out < width) {
  110. c = ir2_get_code(&ctx->bc);
  111. if (c >= 0x80) { /* we have a skip */
  112. c -= 0x7F;
  113. out += c * 2;
  114. } else { /* add two deltas from table */
  115. t = dst[out] + (((table[c * 2] - 128)*3) >> 2);
  116. t = av_clip_uint8(t);
  117. dst[out] = t;
  118. out++;
  119. t = dst[out] + (((table[(c * 2) + 1] - 128)*3) >> 2);
  120. t = av_clip_uint8(t);
  121. dst[out] = t;
  122. out++;
  123. }
  124. }
  125. dst += pitch;
  126. }
  127. return 0;
  128. }
  129. static int ir2_decode_frame(AVCodecContext *avctx,
  130. void *data, int *got_frame,
  131. AVPacket *avpkt)
  132. {
  133. Ir2Context * const s = avctx->priv_data;
  134. const uint8_t *buf = avpkt->data;
  135. int buf_size = avpkt->size;
  136. AVFrame *picture = data;
  137. AVFrame * const p = s->picture;
  138. int start, ret;
  139. int ltab, ctab;
  140. if ((ret = ff_reget_buffer(avctx, p)) < 0) {
  141. av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  142. return ret;
  143. }
  144. start = 48; /* hardcoded for now */
  145. if (start >= buf_size) {
  146. av_log(s->avctx, AV_LOG_ERROR, "input buffer size too small (%d)\n", buf_size);
  147. return AVERROR_INVALIDDATA;
  148. }
  149. s->decode_delta = buf[18];
  150. /* decide whether frame uses deltas or not */
  151. #ifndef BITSTREAM_READER_LE
  152. for (i = 0; i < buf_size; i++)
  153. buf[i] = ff_reverse[buf[i]];
  154. #endif
  155. bitstream_init8(&s->bc, buf + start, buf_size - start);
  156. ltab = buf[0x22] & 3;
  157. ctab = buf[0x22] >> 2;
  158. if (s->decode_delta) { /* intraframe */
  159. if ((ret = ir2_decode_plane(s, avctx->width, avctx->height,
  160. p->data[0], p->linesize[0],
  161. ir2_delta_table[ltab])) < 0)
  162. return ret;
  163. /* swapped U and V */
  164. if ((ret = ir2_decode_plane(s, avctx->width >> 2, avctx->height >> 2,
  165. p->data[2], p->linesize[2],
  166. ir2_delta_table[ctab])) < 0)
  167. return ret;
  168. if ((ret = ir2_decode_plane(s, avctx->width >> 2, avctx->height >> 2,
  169. p->data[1], p->linesize[1],
  170. ir2_delta_table[ctab])) < 0)
  171. return ret;
  172. } else { /* interframe */
  173. if ((ret = ir2_decode_plane_inter(s, avctx->width, avctx->height,
  174. p->data[0], p->linesize[0],
  175. ir2_delta_table[ltab])) < 0)
  176. return ret;
  177. /* swapped U and V */
  178. if ((ret = ir2_decode_plane_inter(s, avctx->width >> 2, avctx->height >> 2,
  179. p->data[2], p->linesize[2],
  180. ir2_delta_table[ctab])) < 0)
  181. return ret;
  182. if ((ret = ir2_decode_plane_inter(s, avctx->width >> 2, avctx->height >> 2,
  183. p->data[1], p->linesize[1],
  184. ir2_delta_table[ctab])) < 0)
  185. return ret;
  186. }
  187. if ((ret = av_frame_ref(picture, p)) < 0)
  188. return ret;
  189. *got_frame = 1;
  190. return buf_size;
  191. }
  192. static av_cold int ir2_decode_init(AVCodecContext *avctx)
  193. {
  194. Ir2Context * const ic = avctx->priv_data;
  195. static VLC_TYPE vlc_tables[1 << CODE_VLC_BITS][2];
  196. ic->avctx = avctx;
  197. avctx->pix_fmt= AV_PIX_FMT_YUV410P;
  198. ic->picture = av_frame_alloc();
  199. if (!ic->picture)
  200. return AVERROR(ENOMEM);
  201. ir2_vlc.table = vlc_tables;
  202. ir2_vlc.table_allocated = 1 << CODE_VLC_BITS;
  203. #ifdef BITSTREAM_READER_LE
  204. init_vlc(&ir2_vlc, CODE_VLC_BITS, IR2_CODES,
  205. &ir2_codes[0][1], 4, 2,
  206. &ir2_codes[0][0], 4, 2, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
  207. #else
  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);
  211. #endif
  212. return 0;
  213. }
  214. static av_cold int ir2_decode_end(AVCodecContext *avctx)
  215. {
  216. Ir2Context * const ic = avctx->priv_data;
  217. av_frame_free(&ic->picture);
  218. return 0;
  219. }
  220. AVCodec ff_indeo2_decoder = {
  221. .name = "indeo2",
  222. .long_name = NULL_IF_CONFIG_SMALL("Intel Indeo 2"),
  223. .type = AVMEDIA_TYPE_VIDEO,
  224. .id = AV_CODEC_ID_INDEO2,
  225. .priv_data_size = sizeof(Ir2Context),
  226. .init = ir2_decode_init,
  227. .close = ir2_decode_end,
  228. .decode = ir2_decode_frame,
  229. .capabilities = AV_CODEC_CAP_DR1,
  230. };