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.

173 lines
4.8KB

  1. /*
  2. * Cirrus Logic AccuPak (CLJR) codec
  3. * Copyright (c) 2003 Alex Beregszaszi
  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. * Cirrus Logic AccuPak codec.
  24. */
  25. #include "avcodec.h"
  26. #include "get_bits.h"
  27. #include "put_bits.h"
  28. typedef struct CLJRContext{
  29. AVCodecContext *avctx;
  30. AVFrame picture;
  31. } CLJRContext;
  32. static av_cold int common_init(AVCodecContext *avctx)
  33. {
  34. CLJRContext * const a = avctx->priv_data;
  35. avctx->coded_frame = &a->picture;
  36. a->avctx = avctx;
  37. return 0;
  38. }
  39. #if CONFIG_CLJR_DECODER
  40. static int decode_frame(AVCodecContext *avctx,
  41. void *data, int *data_size,
  42. AVPacket *avpkt)
  43. {
  44. const uint8_t *buf = avpkt->data;
  45. int buf_size = avpkt->size;
  46. CLJRContext * const a = avctx->priv_data;
  47. GetBitContext gb;
  48. AVFrame *picture = data;
  49. AVFrame * const p = &a->picture;
  50. int x, y;
  51. if(p->data[0])
  52. avctx->release_buffer(avctx, p);
  53. if(buf_size/avctx->height < avctx->width) {
  54. av_log(avctx, AV_LOG_ERROR, "Resolution larger than buffer size. Invalid header?\n");
  55. return -1;
  56. }
  57. p->reference= 0;
  58. if(avctx->get_buffer(avctx, p) < 0){
  59. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  60. return -1;
  61. }
  62. p->pict_type= AV_PICTURE_TYPE_I;
  63. p->key_frame= 1;
  64. init_get_bits(&gb, buf, buf_size * 8);
  65. for(y=0; y<avctx->height; y++){
  66. uint8_t *luma= &a->picture.data[0][ y*a->picture.linesize[0] ];
  67. uint8_t *cb= &a->picture.data[1][ y*a->picture.linesize[1] ];
  68. uint8_t *cr= &a->picture.data[2][ y*a->picture.linesize[2] ];
  69. for(x=0; x<avctx->width; x+=4){
  70. luma[3] = get_bits(&gb, 5) << 3;
  71. luma[2] = get_bits(&gb, 5) << 3;
  72. luma[1] = get_bits(&gb, 5) << 3;
  73. luma[0] = get_bits(&gb, 5) << 3;
  74. luma+= 4;
  75. *(cb++) = get_bits(&gb, 6) << 2;
  76. *(cr++) = get_bits(&gb, 6) << 2;
  77. }
  78. }
  79. *picture = a->picture;
  80. *data_size = sizeof(AVPicture);
  81. emms_c();
  82. return buf_size;
  83. }
  84. static av_cold int decode_init(AVCodecContext *avctx)
  85. {
  86. avctx->pix_fmt = PIX_FMT_YUV411P;
  87. return common_init(avctx);
  88. }
  89. static av_cold int decode_end(AVCodecContext *avctx)
  90. {
  91. CLJRContext *a = avctx->priv_data;
  92. if (a->picture.data[0])
  93. avctx->release_buffer(avctx, &a->picture);
  94. return 0;
  95. }
  96. AVCodec ff_cljr_decoder = {
  97. .name = "cljr",
  98. .type = AVMEDIA_TYPE_VIDEO,
  99. .id = CODEC_ID_CLJR,
  100. .priv_data_size = sizeof(CLJRContext),
  101. .init = decode_init,
  102. .close = decode_end,
  103. .decode = decode_frame,
  104. .capabilities = CODEC_CAP_DR1,
  105. .long_name = NULL_IF_CONFIG_SMALL("Cirrus Logic AccuPak"),
  106. };
  107. #endif
  108. #if CONFIG_CLJR_ENCODER
  109. static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  110. PutBitContext pb;
  111. AVFrame *p = data;
  112. int x, y;
  113. p->pict_type= AV_PICTURE_TYPE_I;
  114. p->key_frame= 1;
  115. init_put_bits(&pb, buf, buf_size / 8);
  116. for (y = 0; y < avctx->height; y++) {
  117. uint8_t *luma = &p->data[0][y * p->linesize[0]];
  118. uint8_t *cb = &p->data[1][y * p->linesize[1]];
  119. uint8_t *cr = &p->data[2][y * p->linesize[2]];
  120. for (x = 0; x < avctx->width; x += 4) {
  121. put_bits(&pb, 5, luma[3] >> 3);
  122. put_bits(&pb, 5, luma[2] >> 3);
  123. put_bits(&pb, 5, luma[1] >> 3);
  124. put_bits(&pb, 5, luma[0] >> 3);
  125. luma += 4;
  126. put_bits(&pb, 6, *(cb++) >> 2);
  127. put_bits(&pb, 6, *(cr++) >> 2);
  128. }
  129. }
  130. flush_put_bits(&pb);
  131. emms_c();
  132. return put_bits_count(&pb) / 8;
  133. }
  134. AVCodec ff_cljr_encoder = {
  135. .name = "cljr",
  136. .type = AVMEDIA_TYPE_VIDEO,
  137. .id = CODEC_ID_CLJR,
  138. .priv_data_size = sizeof(CLJRContext),
  139. .init = common_init,
  140. .encode = encode_frame,
  141. .pix_fmts = (const enum PixelFormat[]) { PIX_FMT_YUV411P,
  142. PIX_FMT_NONE },
  143. .long_name = NULL_IF_CONFIG_SMALL("Cirrus Logic AccuPak"),
  144. };
  145. #endif