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.

206 lines
6.2KB

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