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.

203 lines
6.1KB

  1. /*
  2. * Cirrus Logic AccuPak (CLJR) codec
  3. * Copyright (c) 2003 Alex Beregszaszi
  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. * Cirrus Logic AccuPak codec.
  24. */
  25. #include "avcodec.h"
  26. #include "libavutil/opt.h"
  27. #include "get_bits.h"
  28. #include "put_bits.h"
  29. typedef struct CLJRContext {
  30. AVClass *avclass;
  31. AVCodecContext *avctx;
  32. AVFrame picture;
  33. int dither_type;
  34. } CLJRContext;
  35. static av_cold int common_init(AVCodecContext *avctx)
  36. {
  37. CLJRContext * const a = avctx->priv_data;
  38. avcodec_get_frame_defaults(&a->picture);
  39. avctx->coded_frame = &a->picture;
  40. a->avctx = avctx;
  41. return 0;
  42. }
  43. #if CONFIG_CLJR_DECODER
  44. static int decode_frame(AVCodecContext *avctx,
  45. void *data, int *data_size,
  46. AVPacket *avpkt)
  47. {
  48. const uint8_t *buf = avpkt->data;
  49. int buf_size = avpkt->size;
  50. CLJRContext * const a = avctx->priv_data;
  51. GetBitContext gb;
  52. AVFrame *picture = data;
  53. AVFrame * const p = &a->picture;
  54. int x, y;
  55. if (p->data[0])
  56. avctx->release_buffer(avctx, p);
  57. if (buf_size / avctx->height < avctx->width) {
  58. av_log(avctx, AV_LOG_ERROR,
  59. "Resolution larger than buffer size. Invalid header?\n");
  60. return AVERROR_INVALIDDATA;
  61. }
  62. p->reference = 0;
  63. if (avctx->get_buffer(avctx, p) < 0) {
  64. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  65. return -1;
  66. }
  67. p->pict_type = AV_PICTURE_TYPE_I;
  68. p->key_frame = 1;
  69. init_get_bits(&gb, buf, buf_size * 8);
  70. for (y = 0; y < avctx->height; y++) {
  71. uint8_t *luma = &a->picture.data[0][y * a->picture.linesize[0]];
  72. uint8_t *cb = &a->picture.data[1][y * a->picture.linesize[1]];
  73. uint8_t *cr = &a->picture.data[2][y * a->picture.linesize[2]];
  74. for (x = 0; x < avctx->width; x += 4) {
  75. luma[3] = get_bits(&gb, 5) << 3;
  76. luma[2] = get_bits(&gb, 5) << 3;
  77. luma[1] = get_bits(&gb, 5) << 3;
  78. luma[0] = get_bits(&gb, 5) << 3;
  79. luma += 4;
  80. *(cb++) = get_bits(&gb, 6) << 2;
  81. *(cr++) = get_bits(&gb, 6) << 2;
  82. }
  83. }
  84. *picture = a->picture;
  85. *data_size = sizeof(AVPicture);
  86. return buf_size;
  87. }
  88. static av_cold int decode_init(AVCodecContext *avctx)
  89. {
  90. avctx->pix_fmt = PIX_FMT_YUV411P;
  91. return common_init(avctx);
  92. }
  93. static av_cold int decode_end(AVCodecContext *avctx)
  94. {
  95. CLJRContext *a = avctx->priv_data;
  96. if (a->picture.data[0])
  97. avctx->release_buffer(avctx, &a->picture);
  98. return 0;
  99. }
  100. AVCodec ff_cljr_decoder = {
  101. .name = "cljr",
  102. .type = AVMEDIA_TYPE_VIDEO,
  103. .id = CODEC_ID_CLJR,
  104. .priv_data_size = sizeof(CLJRContext),
  105. .init = decode_init,
  106. .close = decode_end,
  107. .decode = decode_frame,
  108. .capabilities = CODEC_CAP_DR1,
  109. .long_name = NULL_IF_CONFIG_SMALL("Cirrus Logic AccuPak"),
  110. };
  111. #endif
  112. #if CONFIG_CLJR_ENCODER
  113. static int encode_frame(AVCodecContext *avctx, unsigned char *buf,
  114. int buf_size, void *data)
  115. {
  116. CLJRContext *a = avctx->priv_data;
  117. PutBitContext pb;
  118. AVFrame *p = data;
  119. int x, y;
  120. uint32_t dither= avctx->frame_number;
  121. static const uint32_t ordered_dither[2][2] =
  122. {
  123. { 0x10400000, 0x104F0000 },
  124. { 0xCB2A0000, 0xCB250000 },
  125. };
  126. p->pict_type = AV_PICTURE_TYPE_I;
  127. p->key_frame = 1;
  128. init_put_bits(&pb, buf, buf_size / 8);
  129. for (y = 0; y < avctx->height; y++) {
  130. uint8_t *luma = &p->data[0][y * p->linesize[0]];
  131. uint8_t *cb = &p->data[1][y * p->linesize[1]];
  132. uint8_t *cr = &p->data[2][y * p->linesize[2]];
  133. for (x = 0; x < avctx->width; x += 4) {
  134. switch (a->dither_type) {
  135. case 0: dither = 0x492A0000; break;
  136. case 1: dither = dither * 1664525 + 1013904223; break;
  137. case 2: dither = ordered_dither[ y&1 ][ (x>>2)&1 ];break;
  138. }
  139. put_bits(&pb, 5, (luma[3] + (dither>>29) ) >> 3);
  140. put_bits(&pb, 5, (luma[2] + ((dither>>26)&7)) >> 3);
  141. put_bits(&pb, 5, (luma[1] + ((dither>>23)&7)) >> 3);
  142. put_bits(&pb, 5, (luma[0] + ((dither>>20)&7)) >> 3);
  143. luma += 4;
  144. put_bits(&pb, 6, (*(cb++) + ((dither>>18)&3)) >> 2);
  145. put_bits(&pb, 6, (*(cr++) + ((dither>>16)&3)) >> 2);
  146. }
  147. }
  148. flush_put_bits(&pb);
  149. return put_bits_count(&pb) / 8;
  150. }
  151. #define OFFSET(x) offsetof(CLJRContext, x)
  152. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  153. static const AVOption options[] = {
  154. { "dither_type", "Dither type", OFFSET(dither_type), AV_OPT_TYPE_INT, { .dbl=1 }, 0, 2, VE},
  155. { NULL },
  156. };
  157. static const AVClass class = {
  158. .class_name = "cljr encoder",
  159. .item_name = av_default_item_name,
  160. .option = options,
  161. .version = LIBAVUTIL_VERSION_INT,
  162. };
  163. AVCodec ff_cljr_encoder = {
  164. .name = "cljr",
  165. .type = AVMEDIA_TYPE_VIDEO,
  166. .id = CODEC_ID_CLJR,
  167. .priv_data_size = sizeof(CLJRContext),
  168. .init = common_init,
  169. .encode = encode_frame,
  170. .pix_fmts = (const enum PixelFormat[]) { PIX_FMT_YUV411P,
  171. PIX_FMT_NONE },
  172. .long_name = NULL_IF_CONFIG_SMALL("Cirrus Logic AccuPak"),
  173. .priv_class = &class,
  174. };
  175. #endif