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.

174 lines
5.4KB

  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. AVCodecContext *mjpeg_avctx;
  28. int is_mjpeg;
  29. int interlace; //FIXME use frame.interlaced_frame
  30. int tff;
  31. } AVRnContext;
  32. static av_cold int init(AVCodecContext *avctx)
  33. {
  34. AVRnContext *a = avctx->priv_data;
  35. int ret;
  36. // Support "Resolution 1:1" for Avid AVI Codec
  37. a->is_mjpeg = avctx->extradata_size < 31 || memcmp(&avctx->extradata[28], "1:1", 3);
  38. if(!a->is_mjpeg && avctx->lowres) {
  39. av_log(avctx, AV_LOG_ERROR, "lowres is not possible with rawvideo\n");
  40. return AVERROR(EINVAL);
  41. }
  42. if(a->is_mjpeg) {
  43. AVCodec *codec = avcodec_find_decoder(AV_CODEC_ID_MJPEG);
  44. AVDictionary *thread_opt = NULL;
  45. if (!codec) {
  46. av_log(avctx, AV_LOG_ERROR, "MJPEG codec not found\n");
  47. return AVERROR_DECODER_NOT_FOUND;
  48. }
  49. a->mjpeg_avctx = avcodec_alloc_context3(codec);
  50. av_dict_set(&thread_opt, "threads", "1", 0); // Is this needed ?
  51. a->mjpeg_avctx->refcounted_frames = 1;
  52. a->mjpeg_avctx->flags = avctx->flags;
  53. a->mjpeg_avctx->idct_algo = avctx->idct_algo;
  54. a->mjpeg_avctx->lowres = avctx->lowres;
  55. a->mjpeg_avctx->width = avctx->width;
  56. a->mjpeg_avctx->height = avctx->height;
  57. if ((ret = ff_codec_open2_recursive(a->mjpeg_avctx, codec, &thread_opt)) < 0) {
  58. av_log(avctx, AV_LOG_ERROR, "MJPEG codec failed to open\n");
  59. }
  60. av_dict_free(&thread_opt);
  61. return ret;
  62. }
  63. if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
  64. return ret;
  65. avctx->pix_fmt = AV_PIX_FMT_UYVY422;
  66. if(avctx->extradata_size >= 9 && avctx->extradata[4]+28 < avctx->extradata_size) {
  67. int ndx = avctx->extradata[4] + 4;
  68. a->interlace = !memcmp(avctx->extradata + ndx, "1:1(", 4);
  69. if(a->interlace) {
  70. a->tff = avctx->extradata[ndx + 24] == 1;
  71. }
  72. }
  73. return 0;
  74. }
  75. static av_cold int end(AVCodecContext *avctx)
  76. {
  77. AVRnContext *a = avctx->priv_data;
  78. avcodec_close(a->mjpeg_avctx);
  79. av_freep(&a->mjpeg_avctx);
  80. return 0;
  81. }
  82. static int decode_frame(AVCodecContext *avctx, void *data,
  83. int *got_frame, AVPacket *avpkt)
  84. {
  85. AVRnContext *a = avctx->priv_data;
  86. AVFrame *p = data;
  87. const uint8_t *buf = avpkt->data;
  88. int buf_size = avpkt->size;
  89. int y, ret, true_height;
  90. if(a->is_mjpeg) {
  91. ret = avcodec_decode_video2(a->mjpeg_avctx, data, got_frame, avpkt);
  92. if (ret >= 0 && *got_frame && avctx->width <= p->width && avctx->height <= p->height) {
  93. int shift = p->height - avctx->height;
  94. int subsample_h, subsample_v;
  95. av_pix_fmt_get_chroma_sub_sample(p->format, &subsample_h, &subsample_v);
  96. p->data[0] += p->linesize[0] * shift;
  97. if (p->data[2]) {
  98. p->data[1] += p->linesize[1] * (shift>>subsample_v);
  99. p->data[2] += p->linesize[2] * (shift>>subsample_v);
  100. }
  101. p->width = avctx->width;
  102. p->height = avctx->height;
  103. }
  104. avctx->pix_fmt = a->mjpeg_avctx->pix_fmt;
  105. return ret;
  106. }
  107. true_height = buf_size / (2*avctx->width);
  108. if(buf_size < 2*avctx->width * avctx->height) {
  109. av_log(avctx, AV_LOG_ERROR, "packet too small\n");
  110. return AVERROR_INVALIDDATA;
  111. }
  112. if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
  113. return ret;
  114. p->pict_type= AV_PICTURE_TYPE_I;
  115. p->key_frame= 1;
  116. if(a->interlace) {
  117. buf += (true_height - avctx->height)*avctx->width;
  118. for(y = 0; y < avctx->height-1; y+=2) {
  119. memcpy(p->data[0] + (y+ a->tff)*p->linesize[0], buf , 2*avctx->width);
  120. memcpy(p->data[0] + (y+!a->tff)*p->linesize[0], buf + avctx->width*true_height+4, 2*avctx->width);
  121. buf += 2*avctx->width;
  122. }
  123. } else {
  124. buf += (true_height - avctx->height)*avctx->width*2;
  125. for(y = 0; y < avctx->height; y++) {
  126. memcpy(p->data[0] + y*p->linesize[0], buf, 2*avctx->width);
  127. buf += 2*avctx->width;
  128. }
  129. }
  130. *got_frame = 1;
  131. return buf_size;
  132. }
  133. AVCodec ff_avrn_decoder = {
  134. .name = "avrn",
  135. .long_name = NULL_IF_CONFIG_SMALL("Avid AVI Codec"),
  136. .type = AVMEDIA_TYPE_VIDEO,
  137. .id = AV_CODEC_ID_AVRN,
  138. .priv_data_size = sizeof(AVRnContext),
  139. .init = init,
  140. .close = end,
  141. .decode = decode_frame,
  142. .capabilities = AV_CODEC_CAP_DR1,
  143. .max_lowres = 3,
  144. };