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.

156 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 "libavutil/intreadwrite.h"
  24. static av_cold int avui_decode_init(AVCodecContext *avctx)
  25. {
  26. avctx->pix_fmt = PIX_FMT_YUVA422P;
  27. avctx->coded_frame = avcodec_alloc_frame();
  28. if (!avctx->coded_frame) {
  29. av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
  30. return AVERROR(ENOMEM);
  31. }
  32. return 0;
  33. }
  34. static int avui_decode_frame(AVCodecContext *avctx, void *data,
  35. int *data_size, AVPacket *avpkt)
  36. {
  37. AVFrame *pic = avctx->coded_frame;
  38. const uint8_t *src = avpkt->data, *extradata = avctx->extradata;
  39. const uint8_t *srca;
  40. uint8_t *y, *u, *v, *a;
  41. int transparent, interlaced = 1, skip, opaque_length, i, j, k;
  42. uint32_t extradata_size = avctx->extradata_size;
  43. if (pic->data[0])
  44. avctx->release_buffer(avctx, pic);
  45. while (extradata_size >= 24) {
  46. uint32_t atom_size = AV_RB32(extradata);
  47. if (!memcmp(&extradata[4], "APRGAPRG0001", 12)) {
  48. interlaced = extradata[19] != 1;
  49. break;
  50. }
  51. if (atom_size && atom_size <= extradata_size) {
  52. extradata += atom_size;
  53. extradata_size -= atom_size;
  54. } else {
  55. break;
  56. }
  57. }
  58. if (avctx->height == 486) {
  59. skip = 10;
  60. } else {
  61. skip = 16;
  62. }
  63. opaque_length = 2 * avctx->width * (avctx->height + skip) + 4 * interlaced;
  64. if (avpkt->size < opaque_length) {
  65. av_log(avctx, AV_LOG_ERROR, "Insufficient input data.\n");
  66. return AVERROR(EINVAL);
  67. }
  68. transparent = avctx->bits_per_coded_sample == 32 &&
  69. avpkt->size >= opaque_length * 2 + 4;
  70. srca = src + opaque_length + 5;
  71. pic->reference = 0;
  72. if (avctx->get_buffer(avctx, pic) < 0) {
  73. av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
  74. return AVERROR(ENOMEM);
  75. }
  76. pic->key_frame = 1;
  77. pic->pict_type = AV_PICTURE_TYPE_I;
  78. if (!interlaced) {
  79. src += avctx->width * skip;
  80. srca += avctx->width * skip;
  81. }
  82. for (i = 0; i < interlaced + 1; i++) {
  83. src += avctx->width * skip;
  84. srca += avctx->width * skip;
  85. if (interlaced && avctx->height == 486) {
  86. y = pic->data[0] + (1 - i) * pic->linesize[0];
  87. u = pic->data[1] + (1 - i) * pic->linesize[1];
  88. v = pic->data[2] + (1 - i) * pic->linesize[2];
  89. a = pic->data[3] + (1 - i) * pic->linesize[3];
  90. } else {
  91. y = pic->data[0] + i * pic->linesize[0];
  92. u = pic->data[1] + i * pic->linesize[1];
  93. v = pic->data[2] + i * pic->linesize[2];
  94. a = pic->data[3] + i * pic->linesize[3];
  95. }
  96. for (j = 0; j < avctx->height >> interlaced; j++) {
  97. for (k = 0; k < avctx->width >> 1; k++) {
  98. u[ k ] = *src++;
  99. y[2 * k ] = *src++;
  100. a[2 * k ] = 0xFF - (transparent ? *srca++ : 0);
  101. srca++;
  102. v[ k ] = *src++;
  103. y[2 * k + 1] = *src++;
  104. a[2 * k + 1] = 0xFF - (transparent ? *srca++ : 0);
  105. srca++;
  106. }
  107. y += (interlaced + 1) * pic->linesize[0];
  108. u += (interlaced + 1) * pic->linesize[1];
  109. v += (interlaced + 1) * pic->linesize[2];
  110. a += (interlaced + 1) * pic->linesize[3];
  111. }
  112. src += 4;
  113. srca += 4;
  114. }
  115. *data_size = sizeof(AVFrame);
  116. *(AVFrame *)data = *pic;
  117. return avpkt->size;
  118. }
  119. static av_cold int avui_decode_close(AVCodecContext *avctx)
  120. {
  121. if (avctx->coded_frame->data[0])
  122. avctx->release_buffer(avctx, avctx->coded_frame);
  123. av_freep(&avctx->coded_frame);
  124. return 0;
  125. }
  126. AVCodec ff_avui_decoder = {
  127. .name = "avui",
  128. .type = AVMEDIA_TYPE_VIDEO,
  129. .id = AV_CODEC_ID_AVUI,
  130. .init = avui_decode_init,
  131. .decode = avui_decode_frame,
  132. .close = avui_decode_close,
  133. .capabilities = CODEC_CAP_DR1,
  134. .long_name = NULL_IF_CONFIG_SMALL("AVID Meridien"),
  135. };