subtitle_stream.cpp 2.54 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2004-present Facebook. All Rights Reserved.

#include "subtitle_stream.h"
#include <c10/util/Logging.h>
#include <limits>
#include "util.h"

namespace ffmpeg {

namespace {

bool operator==(const SubtitleFormat&, const AVCodecContext&) {
  return true;
}

SubtitleFormat& toSubtitleFormat(SubtitleFormat& x, const AVCodecContext&) {
  return x;
}
} // namespace

SubtitleStream::SubtitleStream(
    AVFormatContext* inputCtx,
    int index,
    bool convertPtsToWallTime,
    const SubtitleFormat& format)
    : Stream(
          inputCtx,
          MediaFormat::makeMediaFormat(format, index),
          convertPtsToWallTime) {
  memset(&sub_, 0, sizeof(sub_));
}

void SubtitleStream::releaseSubtitle() {
  if (sub_.release) {
    avsubtitle_free(&sub_);
    memset(&sub_, 0, sizeof(sub_));
  }
}

SubtitleStream::~SubtitleStream() {
  releaseSubtitle();
  sampler_.shutdown();
}

int SubtitleStream::initFormat() {
  if (!codecCtx_->subtitle_header) {
    LOG(ERROR) << "No subtitle header found";
  } else {
    LOG(INFO) << "Subtitle header found!";
  }
  return 0;
}

int SubtitleStream::analyzePacket(const AVPacket* packet, int* gotFramePtr) {
  // clean-up
  releaseSubtitle();
  // check flush packet
  AVPacket avPacket;
  av_init_packet(&avPacket);
  avPacket.data = nullptr;

  auto pkt = packet ? *packet : avPacket;
  int result = avcodec_decode_subtitle2(codecCtx_, &sub_, gotFramePtr, &pkt);

  if (result < 0) {
    VLOG(1) << "avcodec_decode_subtitle2 failed, err: "
            << Util::generateErrorDesc(result);
  } else if (result == 0) {
    result = packet ? packet->size : 0; // discard the rest of the package
  }

  sub_.release = *gotFramePtr;
  return result;
}

int SubtitleStream::estimateBytes(bool flush) {
  if (!(sampler_.getInputFormat().subtitle == *codecCtx_)) {
    // - reinit sampler
    SamplerParameters params;
    params.type = MediaType::TYPE_SUBTITLE;
    toSubtitleFormat(params.in.subtitle, *codecCtx_);
    if (flush || !sampler_.init(params)) {
      return -1;
    }

    VLOG(1) << "Set input subtitle sampler format";
  }
  return sampler_.getSamplesBytes(&sub_);
}

int SubtitleStream::copyFrameBytes(ByteStorage* out, bool flush) {
  return sampler_.sample(flush ? nullptr : &sub_, out);
}

void SubtitleStream::setHeader(DecoderHeader* header) {
  header->seqno = numGenerator_++;

  header->pts = sub_.pts; // already in us

  if (convertPtsToWallTime_) {
    keeper_.adjust(header->pts);
  }

  header->keyFrame = 0;
  header->fps = std::numeric_limits<double>::quiet_NaN();
  header->format = format_;
}
} // namespace ffmpeg