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.

169 lines
4.9KB

  1. /*
  2. * ZeroCodec Decoder
  3. *
  4. * Copyright (c) 2012, Derek Buitenhuis
  5. *
  6. * Permission to use, copy, modify, and/or distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #include <zlib.h>
  19. #include "avcodec.h"
  20. #include "libavutil/common.h"
  21. typedef struct {
  22. AVFrame previous_frame;
  23. z_stream zstream;
  24. int size;
  25. } ZeroCodecContext;
  26. static int zerocodec_decode_frame(AVCodecContext *avctx, void *data,
  27. int *data_size, AVPacket *avpkt)
  28. {
  29. ZeroCodecContext *zc = avctx->priv_data;
  30. AVFrame *pic = avctx->coded_frame;
  31. AVFrame *prev_pic = &zc->previous_frame;
  32. z_stream *zstream = &zc->zstream;
  33. uint8_t *prev = prev_pic->data[0];
  34. uint8_t *dst;
  35. int i, j, zret;
  36. pic->reference = 3;
  37. if (avpkt->flags & AV_PKT_FLAG_KEY) {
  38. pic->key_frame = 1;
  39. pic->pict_type = AV_PICTURE_TYPE_I;
  40. } else {
  41. if (!prev) {
  42. av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
  43. return AVERROR_INVALIDDATA;
  44. }
  45. pic->key_frame = 0;
  46. pic->pict_type = AV_PICTURE_TYPE_P;
  47. }
  48. zret = inflateReset(zstream);
  49. if (zret != Z_OK) {
  50. av_log(avctx, AV_LOG_ERROR, "Could not reset inflate: %d.\n", zret);
  51. return AVERROR_INVALIDDATA;
  52. }
  53. if (avctx->get_buffer(avctx, pic) < 0) {
  54. av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
  55. return AVERROR(ENOMEM);
  56. }
  57. zstream->next_in = avpkt->data;
  58. zstream->avail_in = avpkt->size;
  59. dst = pic->data[0];
  60. /**
  61. * ZeroCodec has very simple interframe compression. If a value
  62. * is the same as the previous frame, set it to 0.
  63. */
  64. for (i = 0; i < avctx->height; i++) {
  65. zstream->next_out = dst;
  66. zstream->avail_out = avctx->width << 1;
  67. zret = inflate(zstream, Z_SYNC_FLUSH);
  68. if (zret != Z_OK && zret != Z_STREAM_END) {
  69. avctx->release_buffer(avctx, pic);
  70. av_log(avctx, AV_LOG_ERROR,
  71. "Inflate failed with return code: %d.\n", zret);
  72. return AVERROR_INVALIDDATA;
  73. }
  74. if (!(avpkt->flags & AV_PKT_FLAG_KEY))
  75. for (j = 0; j < avctx->width << 1; j++)
  76. dst[j] += prev[j] & -!dst[j];
  77. prev += prev_pic->linesize[0];
  78. dst += pic->linesize[0];
  79. }
  80. /* Release the previous buffer if need be */
  81. if (prev_pic->data[0])
  82. avctx->release_buffer(avctx, prev_pic);
  83. *data_size = sizeof(AVFrame);
  84. *(AVFrame *)data = *pic;
  85. /* Store the previous frame for use later.
  86. * FFSWAP ensures that e.g. pic->data is NULLed. */
  87. FFSWAP(AVFrame, *pic, *prev_pic);
  88. return avpkt->size;
  89. }
  90. static av_cold int zerocodec_decode_close(AVCodecContext *avctx)
  91. {
  92. ZeroCodecContext *zc = avctx->priv_data;
  93. AVFrame *prev_pic = &zc->previous_frame;
  94. inflateEnd(&zc->zstream);
  95. /* Release last frame */
  96. if (prev_pic->data[0])
  97. avctx->release_buffer(avctx, prev_pic);
  98. av_freep(&avctx->coded_frame);
  99. return 0;
  100. }
  101. static av_cold int zerocodec_decode_init(AVCodecContext *avctx)
  102. {
  103. ZeroCodecContext *zc = avctx->priv_data;
  104. z_stream *zstream = &zc->zstream;
  105. int zret;
  106. avctx->pix_fmt = PIX_FMT_UYVY422;
  107. avctx->bits_per_raw_sample = 8;
  108. zc->size = avpicture_get_size(avctx->pix_fmt,
  109. avctx->width, avctx->height);
  110. zstream->zalloc = Z_NULL;
  111. zstream->zfree = Z_NULL;
  112. zstream->opaque = Z_NULL;
  113. zret = inflateInit(zstream);
  114. if (zret != Z_OK) {
  115. av_log(avctx, AV_LOG_ERROR, "Could not initialize inflate: %d.\n", zret);
  116. return AVERROR(ENOMEM);
  117. }
  118. avctx->coded_frame = avcodec_alloc_frame();
  119. if (!avctx->coded_frame) {
  120. av_log(avctx, AV_LOG_ERROR, "Could not allocate frame buffer.\n");
  121. zerocodec_decode_close(avctx);
  122. return AVERROR(ENOMEM);
  123. }
  124. return 0;
  125. }
  126. AVCodec ff_zerocodec_decoder = {
  127. .type = AVMEDIA_TYPE_VIDEO,
  128. .name = "zerocodec",
  129. .id = AV_CODEC_ID_ZEROCODEC,
  130. .priv_data_size = sizeof(ZeroCodecContext),
  131. .init = zerocodec_decode_init,
  132. .decode = zerocodec_decode_frame,
  133. .close = zerocodec_decode_close,
  134. .capabilities = CODEC_CAP_DR1,
  135. .long_name = NULL_IF_CONFIG_SMALL("ZeroCodec Lossless Video"),
  136. };