FfmpegVideoSampler.cpp 2.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "FfmpegVideoSampler.h"
#include "FfmpegUtil.h"

using namespace std;

FfmpegVideoSampler::FfmpegVideoSampler(
    const VideoFormat& in,
    const VideoFormat& out,
    int swsFlags)
    : inFormat_(in), outFormat_(out), swsFlags_(swsFlags) {}

FfmpegVideoSampler::~FfmpegVideoSampler() {
  if (scaleContext_) {
    sws_freeContext(scaleContext_);
    scaleContext_ = nullptr;
  }
}

int FfmpegVideoSampler::init() {
  VLOG(1) << "Input format: width " << inFormat_.width << ", height "
          << inFormat_.height << ", format " << inFormat_.format
          << ", minDimension " << inFormat_.minDimension;
  VLOG(1) << "Scale format: width " << outFormat_.width << ", height "
          << outFormat_.height << ", format " << outFormat_.format
          << ", minDimension " << outFormat_.minDimension;

  scaleContext_ = sws_getContext(
      inFormat_.width,
      inFormat_.height,
      (AVPixelFormat)inFormat_.format,
      outFormat_.width,
      outFormat_.height,
      static_cast<AVPixelFormat>(outFormat_.format),
      swsFlags_,
      nullptr,
      nullptr,
      nullptr);
  if (scaleContext_) {
    return 0;
  } else {
    return -1;
  }
}

int32_t FfmpegVideoSampler::getImageBytes() const {
  return av_image_get_buffer_size(
      (AVPixelFormat)outFormat_.format, outFormat_.width, outFormat_.height, 1);
}

// https://ffmpeg.org/doxygen/3.4/scaling_video_8c-example.html#a10
unique_ptr<DecodedFrame> FfmpegVideoSampler::sample(const AVFrame* frame) {
  if (!frame) {
    return nullptr; // no flush for videos
  }
  // scaled and cropped image
  auto outImageSize = getImageBytes();
  AvDataPtr frameData(static_cast<uint8_t*>(av_malloc(outImageSize)));

  uint8_t* scalePlanes[4] = {nullptr};
  int scaleLines[4] = {0};

  int result;
  if ((result = av_image_fill_arrays(
           scalePlanes,
           scaleLines,
           frameData.get(),
           static_cast<AVPixelFormat>(outFormat_.format),
           outFormat_.width,
           outFormat_.height,
           1)) < 0) {
    LOG(ERROR) << "av_image_fill_arrays failed, err: "
               << ffmpeg_util::getErrorDesc(result);
    return nullptr;
  }

  if ((result = sws_scale(
           scaleContext_,
           frame->data,
           frame->linesize,
           0,
           inFormat_.height,
           scalePlanes,
           scaleLines)) < 0) {
    LOG(ERROR) << "sws_scale failed, err: "
               << ffmpeg_util::getErrorDesc(result);
    return nullptr;
  }

  return make_unique<DecodedFrame>(std::move(frameData), outImageSize, 0);
}