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