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.

144 lines
4.2KB

  1. /*
  2. * AVRn decoder
  3. * Copyright (c) 2012 Michael Niedermayer
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avcodec.h"
  22. #include "internal.h"
  23. #include "mjpeg.h"
  24. #include "mjpegdec.h"
  25. #include "libavutil/imgutils.h"
  26. typedef struct {
  27. MJpegDecodeContext mjpeg_ctx;
  28. AVFrame frame;
  29. int is_mjpeg;
  30. int interlace; //FIXME use frame.interlaced_frame
  31. int tff;
  32. } AVRnContext;
  33. static av_cold int init(AVCodecContext *avctx)
  34. {
  35. AVRnContext *a = avctx->priv_data;
  36. int ret;
  37. // Support "Resolution 1:1" for Avid AVI Codec
  38. a->is_mjpeg = avctx->extradata_size < 31 || memcmp(&avctx->extradata[28], "1:1", 3);
  39. if(!a->is_mjpeg && avctx->lowres) {
  40. av_log(avctx, AV_LOG_ERROR, "lowres is not possible with rawvideo\n");
  41. return AVERROR(EINVAL);
  42. }
  43. if(a->is_mjpeg)
  44. return ff_mjpeg_decode_init(avctx);
  45. if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
  46. return ret;
  47. avcodec_get_frame_defaults(&a->frame);
  48. avctx->pix_fmt = AV_PIX_FMT_UYVY422;
  49. if(avctx->extradata_size >= 9 && avctx->extradata[4]+28 < avctx->extradata_size) {
  50. int ndx = avctx->extradata[4] + 4;
  51. a->interlace = !memcmp(avctx->extradata + ndx, "1:1(", 4);
  52. if(a->interlace) {
  53. a->tff = avctx->extradata[ndx + 24] == 1;
  54. }
  55. }
  56. return 0;
  57. }
  58. static av_cold int end(AVCodecContext *avctx)
  59. {
  60. AVRnContext *a = avctx->priv_data;
  61. AVFrame *p = &a->frame;
  62. if(p->data[0])
  63. avctx->release_buffer(avctx, p);
  64. if(a->is_mjpeg)
  65. ff_mjpeg_decode_end(avctx);
  66. return 0;
  67. }
  68. static int decode_frame(AVCodecContext *avctx, void *data,
  69. int *got_frame, AVPacket *avpkt)
  70. {
  71. AVRnContext *a = avctx->priv_data;
  72. AVFrame *p = &a->frame;
  73. const uint8_t *buf = avpkt->data;
  74. int buf_size = avpkt->size;
  75. int y, ret, true_height;
  76. if(a->is_mjpeg)
  77. return ff_mjpeg_decode_frame(avctx, data, got_frame, avpkt);
  78. true_height = buf_size / (2*avctx->width);
  79. if(p->data[0])
  80. avctx->release_buffer(avctx, p);
  81. if(buf_size < 2*avctx->width * avctx->height) {
  82. av_log(avctx, AV_LOG_ERROR, "packet too small\n");
  83. return AVERROR_INVALIDDATA;
  84. }
  85. if((ret = ff_get_buffer(avctx, p)) < 0){
  86. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  87. return ret;
  88. }
  89. p->pict_type= AV_PICTURE_TYPE_I;
  90. p->key_frame= 1;
  91. if(a->interlace) {
  92. buf += (true_height - avctx->height)*avctx->width;
  93. for(y = 0; y < avctx->height-1; y+=2) {
  94. memcpy(p->data[0] + (y+ a->tff)*p->linesize[0], buf , 2*avctx->width);
  95. memcpy(p->data[0] + (y+!a->tff)*p->linesize[0], buf + avctx->width*true_height+4, 2*avctx->width);
  96. buf += 2*avctx->width;
  97. }
  98. } else {
  99. buf += (true_height - avctx->height)*avctx->width*2;
  100. for(y = 0; y < avctx->height; y++) {
  101. memcpy(p->data[0] + y*p->linesize[0], buf, 2*avctx->width);
  102. buf += 2*avctx->width;
  103. }
  104. }
  105. *(AVFrame*)data = a->frame;
  106. *got_frame = 1;
  107. return buf_size;
  108. }
  109. AVCodec ff_avrn_decoder = {
  110. .name = "avrn",
  111. .type = AVMEDIA_TYPE_VIDEO,
  112. .id = AV_CODEC_ID_AVRN,
  113. .priv_data_size = sizeof(AVRnContext),
  114. .init = init,
  115. .close = end,
  116. .decode = decode_frame,
  117. .long_name = NULL_IF_CONFIG_SMALL("Avid AVI Codec"),
  118. .capabilities = CODEC_CAP_DR1,
  119. .max_lowres = 3,
  120. };