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
5.0KB

  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 "internal.h"
  21. #include "libavutil/common.h"
  22. typedef struct {
  23. AVFrame previous_frame;
  24. z_stream zstream;
  25. int size;
  26. } ZeroCodecContext;
  27. static int zerocodec_decode_frame(AVCodecContext *avctx, void *data,
  28. int *got_frame, AVPacket *avpkt)
  29. {
  30. ZeroCodecContext *zc = avctx->priv_data;
  31. AVFrame *pic = avctx->coded_frame;
  32. AVFrame *prev_pic = &zc->previous_frame;
  33. z_stream *zstream = &zc->zstream;
  34. uint8_t *prev = prev_pic->data[0];
  35. uint8_t *dst;
  36. int i, j, zret;
  37. pic->reference = 3;
  38. if (avpkt->flags & AV_PKT_FLAG_KEY) {
  39. pic->key_frame = 1;
  40. pic->pict_type = AV_PICTURE_TYPE_I;
  41. } else {
  42. if (!prev) {
  43. av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
  44. return AVERROR_INVALIDDATA;
  45. }
  46. prev += (avctx->height - 1) * prev_pic->linesize[0];
  47. pic->key_frame = 0;
  48. pic->pict_type = AV_PICTURE_TYPE_P;
  49. }
  50. zret = inflateReset(zstream);
  51. if (zret != Z_OK) {
  52. av_log(avctx, AV_LOG_ERROR, "Could not reset inflate: %d.\n", zret);
  53. return AVERROR_INVALIDDATA;
  54. }
  55. if (ff_get_buffer(avctx, pic) < 0) {
  56. av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
  57. return AVERROR(ENOMEM);
  58. }
  59. zstream->next_in = avpkt->data;
  60. zstream->avail_in = avpkt->size;
  61. dst = pic->data[0] + (avctx->height - 1) * pic->linesize[0];
  62. /**
  63. * ZeroCodec has very simple interframe compression. If a value
  64. * is the same as the previous frame, set it to 0.
  65. */
  66. for (i = 0; i < avctx->height; i++) {
  67. zstream->next_out = dst;
  68. zstream->avail_out = avctx->width << 1;
  69. zret = inflate(zstream, Z_SYNC_FLUSH);
  70. if (zret != Z_OK && zret != Z_STREAM_END) {
  71. avctx->release_buffer(avctx, pic);
  72. av_log(avctx, AV_LOG_ERROR,
  73. "Inflate failed with return code: %d.\n", zret);
  74. return AVERROR_INVALIDDATA;
  75. }
  76. if (!(avpkt->flags & AV_PKT_FLAG_KEY))
  77. for (j = 0; j < avctx->width << 1; j++)
  78. dst[j] += prev[j] & -!dst[j];
  79. prev -= prev_pic->linesize[0];
  80. dst -= pic->linesize[0];
  81. }
  82. /* Release the previous buffer if need be */
  83. if (prev_pic->data[0])
  84. avctx->release_buffer(avctx, prev_pic);
  85. *got_frame = 1;
  86. *(AVFrame *)data = *pic;
  87. /* Store the previous frame for use later.
  88. * FFSWAP ensures that e.g. pic->data is NULLed. */
  89. FFSWAP(AVFrame, *pic, *prev_pic);
  90. return avpkt->size;
  91. }
  92. static av_cold int zerocodec_decode_close(AVCodecContext *avctx)
  93. {
  94. ZeroCodecContext *zc = avctx->priv_data;
  95. AVFrame *prev_pic = &zc->previous_frame;
  96. inflateEnd(&zc->zstream);
  97. /* Release last frame */
  98. if (prev_pic->data[0])
  99. avctx->release_buffer(avctx, prev_pic);
  100. av_freep(&avctx->coded_frame);
  101. return 0;
  102. }
  103. static av_cold int zerocodec_decode_init(AVCodecContext *avctx)
  104. {
  105. ZeroCodecContext *zc = avctx->priv_data;
  106. z_stream *zstream = &zc->zstream;
  107. int zret;
  108. avctx->pix_fmt = AV_PIX_FMT_UYVY422;
  109. avctx->bits_per_raw_sample = 8;
  110. zc->size = avpicture_get_size(avctx->pix_fmt,
  111. avctx->width, avctx->height);
  112. zstream->zalloc = Z_NULL;
  113. zstream->zfree = Z_NULL;
  114. zstream->opaque = Z_NULL;
  115. zret = inflateInit(zstream);
  116. if (zret != Z_OK) {
  117. av_log(avctx, AV_LOG_ERROR, "Could not initialize inflate: %d.\n", zret);
  118. return AVERROR(ENOMEM);
  119. }
  120. avctx->coded_frame = avcodec_alloc_frame();
  121. if (!avctx->coded_frame) {
  122. av_log(avctx, AV_LOG_ERROR, "Could not allocate frame buffer.\n");
  123. zerocodec_decode_close(avctx);
  124. return AVERROR(ENOMEM);
  125. }
  126. return 0;
  127. }
  128. AVCodec ff_zerocodec_decoder = {
  129. .type = AVMEDIA_TYPE_VIDEO,
  130. .name = "zerocodec",
  131. .id = AV_CODEC_ID_ZEROCODEC,
  132. .priv_data_size = sizeof(ZeroCodecContext),
  133. .init = zerocodec_decode_init,
  134. .decode = zerocodec_decode_frame,
  135. .close = zerocodec_decode_close,
  136. .capabilities = CODEC_CAP_DR1,
  137. .long_name = NULL_IF_CONFIG_SMALL("ZeroCodec Lossless Video"),
  138. };