avrndec.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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;
  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_free_context(&a->mjpeg_avctx);
  79. return 0;
  80. }
  81. static int decode_frame(AVCodecContext *avctx, void *data,
  82. int *got_frame, AVPacket *avpkt)
  83. {
  84. AVRnContext *a = avctx->priv_data;
  85. AVFrame *p = data;
  86. const uint8_t *buf = avpkt->data;
  87. int buf_size = avpkt->size;
  88. int y, ret, true_height;
  89. if(a->is_mjpeg) {
  90. ret = avcodec_decode_video2(a->mjpeg_avctx, data, got_frame, avpkt);
  91. if (ret >= 0 && *got_frame && avctx->width <= p->width && avctx->height <= p->height) {
  92. int shift = p->height - avctx->height;
  93. int subsample_h, subsample_v;
  94. av_pix_fmt_get_chroma_sub_sample(p->format, &subsample_h, &subsample_v);
  95. p->data[0] += p->linesize[0] * shift;
  96. if (p->data[2]) {
  97. p->data[1] += p->linesize[1] * (shift>>subsample_v);
  98. p->data[2] += p->linesize[2] * (shift>>subsample_v);
  99. }
  100. p->width = avctx->width;
  101. p->height = avctx->height;
  102. }
  103. avctx->pix_fmt = a->mjpeg_avctx->pix_fmt;
  104. return ret;
  105. }
  106. true_height = buf_size / (2*avctx->width);
  107. if(buf_size < 2*avctx->width * avctx->height) {
  108. av_log(avctx, AV_LOG_ERROR, "packet too small\n");
  109. return AVERROR_INVALIDDATA;
  110. }
  111. if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
  112. return ret;
  113. p->pict_type= AV_PICTURE_TYPE_I;
  114. p->key_frame= 1;
  115. if(a->interlace) {
  116. buf += (true_height - avctx->height)*avctx->width;
  117. for(y = 0; y < avctx->height-1; y+=2) {
  118. memcpy(p->data[0] + (y+ a->tff)*p->linesize[0], buf , 2*avctx->width);
  119. memcpy(p->data[0] + (y+!a->tff)*p->linesize[0], buf + avctx->width*true_height+4, 2*avctx->width);
  120. buf += 2*avctx->width;
  121. }
  122. } else {
  123. buf += (true_height - avctx->height)*avctx->width*2;
  124. for(y = 0; y < avctx->height; y++) {
  125. memcpy(p->data[0] + y*p->linesize[0], buf, 2*avctx->width);
  126. buf += 2*avctx->width;
  127. }
  128. }
  129. *got_frame = 1;
  130. return buf_size;
  131. }
  132. AVCodec ff_avrn_decoder = {
  133. .name = "avrn",
  134. .long_name = NULL_IF_CONFIG_SMALL("Avid AVI Codec"),
  135. .type = AVMEDIA_TYPE_VIDEO,
  136. .id = AV_CODEC_ID_AVRN,
  137. .priv_data_size = sizeof(AVRnContext),
  138. .init = init,
  139. .close = end,
  140. .decode = decode_frame,
  141. .max_lowres = 3,
  142. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  143. };