| 1 | // Copyright (c) 2007-2015 The FFmpegSource Project |
| 2 | // |
| 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | // of this software and associated documentation files (the "Software"), to deal |
| 5 | // in the Software without restriction, including without limitation the rights |
| 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | // copies of the Software, and to permit persons to whom the Software is |
| 8 | // furnished to do so, subject to the following conditions: |
| 9 | // |
| 10 | // The above copyright notice and this permission notice shall be included in |
| 11 | // all copies or substantial portions of the Software. |
| 12 | // |
| 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | // THE SOFTWARE. |
| 20 | |
| 21 | |
| 22 | #include "videoutils.h" |
| 23 | |
| 24 | #include <algorithm> |
| 25 | #include <cmath> |
| 26 | |
| 27 | /* if you have this, we'll assume you have a new enough libavutil too */ |
| 28 | extern "C" { |
| 29 | #include <libavutil/opt.h> |
| 30 | } |
| 31 | |
| 32 | SwsContext *GetSwsContext(int SrcW, int SrcH, AVPixelFormat SrcFormat, int SrcColorSpace, int SrcColorRange, int DstW, int DstH, AVPixelFormat DstFormat, int DstColorSpace, int DstColorRange, int64_t Flags) { |
| 33 | Flags |= SWS_FULL_CHR_H_INT | SWS_FULL_CHR_H_INP | SWS_ACCURATE_RND; |
| 34 | SwsContext *Context = sws_alloc_context(); |
| 35 | if (!Context) return nullptr; |
| 36 | |
| 37 | // 0 = limited range, 1 = full range |
| 38 | int SrcRange = SrcColorRange == AVCOL_RANGE_JPEG; |
| 39 | if (SrcColorRange == AVCOL_RANGE_UNSPECIFIED) |
| 40 | SrcRange = (GuessCSType(SrcFormat) != cYUV); |
| 41 | int DstRange = DstColorRange == AVCOL_RANGE_JPEG; |
| 42 | if (DstColorRange == AVCOL_RANGE_UNSPECIFIED) |
| 43 | DstRange = (GuessCSType(DstFormat) != cYUV); |
| 44 | |
| 45 | av_opt_set_int(Context, "sws_flags", Flags, 0); |
| 46 | av_opt_set_int(Context, "srcw", SrcW, 0); |
| 47 | av_opt_set_int(Context, "srch", SrcH, 0); |
| 48 | av_opt_set_int(Context, "dstw", DstW, 0); |
| 49 | av_opt_set_int(Context, "dsth", DstH, 0); |
| 50 | av_opt_set_int(Context, "src_range", SrcRange, 0); |
| 51 | av_opt_set_int(Context, "dst_range", DstRange, 0); |
| 52 | av_opt_set_int(Context, "src_format", SrcFormat, 0); |
| 53 | av_opt_set_int(Context, "dst_format", DstFormat, 0); |
| 54 | |
| 55 | sws_setColorspaceDetails(Context, |
| 56 | sws_getCoefficients(SrcColorSpace), SrcRange, |
| 57 | sws_getCoefficients(DstColorSpace), DstRange, |
| 58 | 0, 1 << 16, 1 << 16); |
| 59 | |
| 60 | if (sws_init_context(Context, nullptr, nullptr) < 0) { |
| 61 | sws_freeContext(Context); |
| 62 | return nullptr; |
| 63 | } |
| 64 | |
| 65 | return Context; |
| 66 | } |
| 67 | |
| 68 | /*************************** |
| 69 | ** |
| 70 | ** Two functions for making FFMS pretend it's not quite as VFR-based as it really is. |
| 71 | ** |
| 72 | ***************************/ |
| 73 | |
| 74 | |
| 75 | // attempt to correct framerate to a common fraction if close to one |
| 76 | void CorrectRationalFramerate(int *Num, int *Den) { |
| 77 | // Make sure fps is a normalized rational number |
| 78 | av_reduce(Den, Num, *Den, *Num, INT_MAX); |
| 79 | |
| 80 | const double fps = static_cast<double>(*Num) / *Den; |
| 81 | const int fpsList[] = { 24, 25, 30, 48, 50, 60, 100, 120 }; |
| 82 | |
| 83 | for (size_t i = 0; i < sizeof(fpsList) / sizeof(fpsList[0]); i++) { |
| 84 | const double delta = (fpsList[i] - static_cast<double>(fpsList[i]) / 1.001) / 2.0; |
| 85 | if (fabs(fps - fpsList[i]) < delta) { |
| 86 | *Num = fpsList[i]; |
| 87 | *Den = 1; |
| 88 | break; |
| 89 | } else if ((fpsList[i] % 25) && (fabs(fps - static_cast<double>(fpsList[i]) / 1.001) < delta)) { |
| 90 | *Num = fpsList[i] * 1000; |
| 91 | *Den = 1001; |
| 92 | break; |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // correct the timebase if it is invalid |
| 98 | void CorrectTimebase(FFMS_VideoProperties *VP, FFMS_TrackTimeBase *TTimebase) { |
| 99 | double Timebase = (double)TTimebase->Num / TTimebase->Den; |
| 100 | double FPS = (double)VP->FPSNumerator / VP->FPSDenominator; |
| 101 | if ((1000 / Timebase) / FPS < 1) { |
| 102 | TTimebase->Den = VP->FPSNumerator; |
| 103 | TTimebase->Num = (int64_t)VP->FPSDenominator * 1000; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /*************************** |
| 108 | ** |
| 109 | ** Since avcodec_find_best_pix_fmt() is broken, we have our own implementation of it here. |
| 110 | ** |
| 111 | ***************************/ |
| 112 | |
| 113 | BCSType GuessCSType(AVPixelFormat p) { |
| 114 | // guessing the colorspace type from the name is kinda hackish but libav doesn't export this kind of metadata |
| 115 | // special case since pal8 is assumed to be rgb32 |
| 116 | if (p == AV_PIX_FMT_PAL8) |
| 117 | return cRGB; |
| 118 | if (av_pix_fmt_desc_get(p)->flags & AV_PIX_FMT_FLAG_HWACCEL) |
| 119 | return cUNUSABLE; |
| 120 | if (av_pix_fmt_desc_get(p)->flags & AV_PIX_FMT_FLAG_PAL) |
| 121 | return cUNUSABLE; |
| 122 | if (av_pix_fmt_desc_get(p)->flags & AV_PIX_FMT_FLAG_RGB) |
| 123 | return cRGB; |
| 124 | if (av_pix_fmt_desc_get(p)->nb_components <= 2) |
| 125 | return cGRAY; |
| 126 | if (av_pix_fmt_desc_get(p)->nb_components >= 3) |
| 127 | return cYUV; |
| 128 | return cUNUSABLE; // should never come here |
| 129 | } |
| 130 | |
| 131 | struct LossAttributes { |
| 132 | AVPixelFormat Format; |
| 133 | int ChromaUndersampling; |
| 134 | int ChromaOversampling; |
| 135 | int DepthDifference; |
| 136 | int CSLoss; // 0 = no difference, 1 = unused, 2 = full conversion required, 3 = alpha loss, 4 = full conversion plus alpha loss, 5 = complete color loss |
| 137 | int CSGain; // alpha plane added, gray converted to yuv/rgb |
| 138 | }; |
| 139 | |
| 140 | static int GetPseudoDepth(const AVPixFmtDescriptor &Desc) { |
| 141 | // Comparing the pseudo depth makes sure that rgb565-ish formats get selected over rgb555-ish ones |
| 142 | int depth = 0; |
| 143 | for (int i = 0; i < Desc.nb_components; i++) |
| 144 | depth = FFMAX(depth, Desc.comp[i].depth); |
| 145 | return depth; |
| 146 | } |
| 147 | |
| 148 | static LossAttributes CalculateLoss(AVPixelFormat Dst, AVPixelFormat Src) { |
| 149 | const AVPixFmtDescriptor &SrcDesc = *av_pix_fmt_desc_get(Src); |
| 150 | const AVPixFmtDescriptor &DstDesc = *av_pix_fmt_desc_get(Dst); |
| 151 | BCSType SrcCS = GuessCSType(Src); |
| 152 | BCSType DstCS = GuessCSType(Dst); |
| 153 | |
| 154 | LossAttributes Loss; |
| 155 | Loss.Format = Dst; |
| 156 | Loss.DepthDifference = GetPseudoDepth(DstDesc) - GetPseudoDepth(SrcDesc); |
| 157 | Loss.ChromaOversampling = FFMAX(0, SrcDesc.log2_chroma_h - DstDesc.log2_chroma_h) + FFMAX(0, SrcDesc.log2_chroma_w - DstDesc.log2_chroma_w); |
| 158 | Loss.ChromaUndersampling = FFMAX(0, DstDesc.log2_chroma_h - SrcDesc.log2_chroma_h) + FFMAX(0, DstDesc.log2_chroma_w - SrcDesc.log2_chroma_w); |
| 159 | Loss.CSGain = 0; |
| 160 | |
| 161 | if (SrcCS == DstCS) { |
| 162 | Loss.CSLoss = 0; |
| 163 | } else if (SrcCS == cGRAY) { |
| 164 | Loss.CSGain = 1; |
| 165 | Loss.ChromaOversampling = 0; |
| 166 | Loss.ChromaUndersampling = 0; |
| 167 | Loss.CSLoss = 0; |
| 168 | } else if (DstCS == cGRAY) { |
| 169 | Loss.ChromaOversampling = 0; |
| 170 | Loss.ChromaUndersampling = 0; |
| 171 | Loss.CSLoss = 5; |
| 172 | } else { |
| 173 | Loss.CSLoss = 2; |
| 174 | } |
| 175 | |
| 176 | if (Loss.CSLoss < 3 && (DstDesc.nb_components == SrcDesc.nb_components - 1)) { |
| 177 | if (Loss.CSLoss == 2) |
| 178 | Loss.CSLoss = 4; |
| 179 | else |
| 180 | Loss.CSLoss = 3; |
| 181 | } |
| 182 | |
| 183 | // Added alpha plane |
| 184 | if (Loss.CSLoss == 0 && (SrcDesc.nb_components == DstDesc.nb_components - 1)) { |
| 185 | Loss.CSGain = 1; |
| 186 | } |
| 187 | |
| 188 | return Loss; |
| 189 | } |
| 190 | |
| 191 | AVPixelFormat FindBestPixelFormat(const std::vector<AVPixelFormat> &Dsts, AVPixelFormat Src) { |
| 192 | // some trivial special cases to make sure there's as little conversion as possible |
| 193 | if (Dsts.empty()) |
| 194 | return AV_PIX_FMT_NONE; |
| 195 | if (Dsts.size() == 1) |
| 196 | return Dsts[0]; |
| 197 | |
| 198 | // is the input in the output? |
| 199 | auto i = std::find(Dsts.begin(), Dsts.end(), Src); |
| 200 | if (i != Dsts.end()) |
| 201 | return Src; |
| 202 | |
| 203 | // If it's an evil paletted format pretend it's normal RGB when calculating loss |
| 204 | if (Src == AV_PIX_FMT_PAL8) |
| 205 | Src = AV_PIX_FMT_RGB32; |
| 206 | |
| 207 | std::vector<AVPixelFormat> OutputDsts; |
| 208 | for (auto DstFmt = Dsts.begin(); DstFmt != Dsts.end(); ++DstFmt) { |
| 209 | // Skip outputing to formwats we can't convert to, this avoids swscale init failures later in the code |
| 210 | if (sws_isSupportedOutput(*DstFmt)) |
| 211 | OutputDsts.push_back(*DstFmt); |
| 212 | } |
| 213 | |
| 214 | if (OutputDsts.empty()) |
| 215 | return AV_PIX_FMT_NONE; |
| 216 | |
| 217 | i = OutputDsts.begin(); |
| 218 | LossAttributes Loss = CalculateLoss(*i++, Src); |
| 219 | for (; i != OutputDsts.end(); ++i) { |
| 220 | LossAttributes CLoss = CalculateLoss(*i, Src); |
| 221 | if (Loss.CSLoss >= 3 && CLoss.CSLoss < Loss.CSLoss) { // favor the same color format output |
| 222 | Loss = CLoss; |
| 223 | } else if (Loss.DepthDifference >= 0 && CLoss.DepthDifference >= 0) { // focus on chroma undersamling and conversion loss if the target depth has been achieved |
| 224 | if ((CLoss.ChromaUndersampling < Loss.ChromaUndersampling) |
| 225 | || (CLoss.ChromaUndersampling == Loss.ChromaUndersampling && CLoss.CSLoss < Loss.CSLoss) |
| 226 | || (CLoss.ChromaUndersampling == Loss.ChromaUndersampling && CLoss.CSLoss == Loss.CSLoss && CLoss.DepthDifference < Loss.DepthDifference) |
| 227 | || (CLoss.ChromaUndersampling == Loss.ChromaUndersampling && CLoss.CSLoss == Loss.CSLoss |
| 228 | && CLoss.DepthDifference == Loss.DepthDifference && CLoss.ChromaOversampling < Loss.ChromaOversampling)) |
| 229 | Loss = CLoss; |
| 230 | } else { // put priority on reaching the same depth as the input |
| 231 | if ((CLoss.DepthDifference > Loss.DepthDifference) |
| 232 | || (CLoss.DepthDifference == Loss.DepthDifference && CLoss.ChromaUndersampling < Loss.ChromaUndersampling) |
| 233 | || (CLoss.DepthDifference == Loss.DepthDifference && CLoss.ChromaUndersampling == Loss.ChromaUndersampling && CLoss.CSLoss < Loss.CSLoss) |
| 234 | || (CLoss.DepthDifference == Loss.DepthDifference && CLoss.ChromaUndersampling == Loss.ChromaUndersampling |
| 235 | && CLoss.CSLoss == Loss.CSLoss && CLoss.ChromaOversampling < Loss.ChromaOversampling)) |
| 236 | Loss = CLoss; |
| 237 | else if (CLoss.DepthDifference == Loss.DepthDifference && CLoss.ChromaUndersampling == Loss.ChromaUndersampling |
| 238 | && CLoss.CSLoss == Loss.CSLoss && CLoss.ChromaOversampling == Loss.ChromaOversampling && CLoss.CSGain < Loss.CSGain) |
| 239 | Loss = CLoss; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | return Loss.Format; |
| 244 | } |
| 245 | |
| 246 | void ParseVP8(const uint8_t Buf, bool *Invisible, int *PictType) { |
| 247 | *PictType = (Buf & 0x01) ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I; |
| 248 | *Invisible = (*Invisible || !(Buf & 0x10)); |
| 249 | } |
| 250 | |
| 251 | void ParseVP9(const uint8_t Buf, int *PictType) |
| 252 | { |
| 253 | int profile = ((Buf & 0x20) >> 5) | ((Buf & 0x10) >> 3); |
| 254 | int shift = (profile == 3); |
| 255 | |
| 256 | if (Buf & (0x8 >> shift)) |
| 257 | *PictType = AV_PICTURE_TYPE_P; |
| 258 | else |
| 259 | *PictType = (Buf & (0x4 >> shift)) ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I; |
| 260 | } |