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.

157 lines
4.8KB

  1. /*
  2. * AVID Meridien decoder
  3. *
  4. * Copyright (c) 2012 Carl Eugen Hoyos
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "avcodec.h"
  23. #include "internal.h"
  24. #include "libavutil/intreadwrite.h"
  25. static av_cold int avui_decode_init(AVCodecContext *avctx)
  26. {
  27. avctx->pix_fmt = AV_PIX_FMT_YUVA422P;
  28. avctx->coded_frame = avcodec_alloc_frame();
  29. if (!avctx->coded_frame) {
  30. av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
  31. return AVERROR(ENOMEM);
  32. }
  33. return 0;
  34. }
  35. static int avui_decode_frame(AVCodecContext *avctx, void *data,
  36. int *got_frame, AVPacket *avpkt)
  37. {
  38. AVFrame *pic = avctx->coded_frame;
  39. const uint8_t *src = avpkt->data, *extradata = avctx->extradata;
  40. const uint8_t *srca;
  41. uint8_t *y, *u, *v, *a;
  42. int transparent, interlaced = 1, skip, opaque_length, i, j, k;
  43. uint32_t extradata_size = avctx->extradata_size;
  44. if (pic->data[0])
  45. avctx->release_buffer(avctx, pic);
  46. while (extradata_size >= 24) {
  47. uint32_t atom_size = AV_RB32(extradata);
  48. if (!memcmp(&extradata[4], "APRGAPRG0001", 12)) {
  49. interlaced = extradata[19] != 1;
  50. break;
  51. }
  52. if (atom_size && atom_size <= extradata_size) {
  53. extradata += atom_size;
  54. extradata_size -= atom_size;
  55. } else {
  56. break;
  57. }
  58. }
  59. if (avctx->height == 486) {
  60. skip = 10;
  61. } else {
  62. skip = 16;
  63. }
  64. opaque_length = 2 * avctx->width * (avctx->height + skip) + 4 * interlaced;
  65. if (avpkt->size < opaque_length) {
  66. av_log(avctx, AV_LOG_ERROR, "Insufficient input data.\n");
  67. return AVERROR(EINVAL);
  68. }
  69. transparent = avctx->bits_per_coded_sample == 32 &&
  70. avpkt->size >= opaque_length * 2 + 4;
  71. srca = src + opaque_length + 5;
  72. pic->reference = 0;
  73. if (ff_get_buffer(avctx, pic) < 0) {
  74. av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
  75. return AVERROR(ENOMEM);
  76. }
  77. pic->key_frame = 1;
  78. pic->pict_type = AV_PICTURE_TYPE_I;
  79. if (!interlaced) {
  80. src += avctx->width * skip;
  81. srca += avctx->width * skip;
  82. }
  83. for (i = 0; i < interlaced + 1; i++) {
  84. src += avctx->width * skip;
  85. srca += avctx->width * skip;
  86. if (interlaced && avctx->height == 486) {
  87. y = pic->data[0] + (1 - i) * pic->linesize[0];
  88. u = pic->data[1] + (1 - i) * pic->linesize[1];
  89. v = pic->data[2] + (1 - i) * pic->linesize[2];
  90. a = pic->data[3] + (1 - i) * pic->linesize[3];
  91. } else {
  92. y = pic->data[0] + i * pic->linesize[0];
  93. u = pic->data[1] + i * pic->linesize[1];
  94. v = pic->data[2] + i * pic->linesize[2];
  95. a = pic->data[3] + i * pic->linesize[3];
  96. }
  97. for (j = 0; j < avctx->height >> interlaced; j++) {
  98. for (k = 0; k < avctx->width >> 1; k++) {
  99. u[ k ] = *src++;
  100. y[2 * k ] = *src++;
  101. a[2 * k ] = 0xFF - (transparent ? *srca++ : 0);
  102. srca++;
  103. v[ k ] = *src++;
  104. y[2 * k + 1] = *src++;
  105. a[2 * k + 1] = 0xFF - (transparent ? *srca++ : 0);
  106. srca++;
  107. }
  108. y += (interlaced + 1) * pic->linesize[0];
  109. u += (interlaced + 1) * pic->linesize[1];
  110. v += (interlaced + 1) * pic->linesize[2];
  111. a += (interlaced + 1) * pic->linesize[3];
  112. }
  113. src += 4;
  114. srca += 4;
  115. }
  116. *got_frame = 1;
  117. *(AVFrame *)data = *pic;
  118. return avpkt->size;
  119. }
  120. static av_cold int avui_decode_close(AVCodecContext *avctx)
  121. {
  122. if (avctx->coded_frame->data[0])
  123. avctx->release_buffer(avctx, avctx->coded_frame);
  124. av_freep(&avctx->coded_frame);
  125. return 0;
  126. }
  127. AVCodec ff_avui_decoder = {
  128. .name = "avui",
  129. .type = AVMEDIA_TYPE_VIDEO,
  130. .id = AV_CODEC_ID_AVUI,
  131. .init = avui_decode_init,
  132. .decode = avui_decode_frame,
  133. .close = avui_decode_close,
  134. .capabilities = CODEC_CAP_DR1,
  135. .long_name = NULL_IF_CONFIG_SMALL("Avid Meridien Uncompressed"),
  136. };