| 1 | // Copyright (c) 2007-2015 Fredrik Mellbin |
| 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 | extern "C" { |
| 22 | #include <libavutil/channel_layout.h> |
| 23 | } |
| 24 | |
| 25 | #include "utils.h" |
| 26 | |
| 27 | #include "indexing.h" |
| 28 | #include "track.h" |
| 29 | |
| 30 | #ifdef _WIN32 |
| 31 | # define WIN32_LEAN_AND_MEAN |
| 32 | # include <windows.h> |
| 33 | #endif // _WIN32 |
| 34 | |
| 35 | #include <algorithm> |
| 36 | #include <sstream> |
| 37 | |
| 38 | FFMS_Exception::FFMS_Exception(int ErrorType, int SubType, const char *Message) : _Message(Message), _ErrorType(ErrorType), _SubType(SubType) {} |
| 39 | FFMS_Exception::FFMS_Exception(int ErrorType, int SubType, const std::string &Message) : _Message(Message), _ErrorType(ErrorType), _SubType(SubType) {} |
| 40 | |
| 41 | int FFMS_Exception::CopyOut(FFMS_ErrorInfo *ErrorInfo) const { |
| 42 | if (ErrorInfo) { |
| 43 | ErrorInfo->ErrorType = _ErrorType; |
| 44 | ErrorInfo->SubType = _SubType; |
| 45 | |
| 46 | if (ErrorInfo->BufferSize > 0) { |
| 47 | memset(ErrorInfo->Buffer, 0, ErrorInfo->BufferSize); |
| 48 | _Message.copy(ErrorInfo->Buffer, ErrorInfo->BufferSize - 1); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | return (_ErrorType << 16) | _SubType; |
| 53 | } |
| 54 | |
| 55 | void ClearErrorInfo(FFMS_ErrorInfo *ErrorInfo) { |
| 56 | if (ErrorInfo) { |
| 57 | ErrorInfo->ErrorType = FFMS_ERROR_SUCCESS; |
| 58 | ErrorInfo->SubType = FFMS_ERROR_SUCCESS; |
| 59 | |
| 60 | if (ErrorInfo->BufferSize > 0) |
| 61 | ErrorInfo->Buffer[0] = 0; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | void FillAP(FFMS_AudioProperties &AP, AVCodecContext *CTX, FFMS_Track &Frames) { |
| 66 | AP.SampleFormat = static_cast<FFMS_SampleFormat>(av_get_packed_sample_fmt(CTX->sample_fmt)); |
| 67 | AP.BitsPerSample = av_get_bytes_per_sample(CTX->sample_fmt) * 8; |
| 68 | AP.Channels = CTX->ch_layout.nb_channels; |
| 69 | |
| 70 | if (CTX->ch_layout.order == AV_CHANNEL_ORDER_NATIVE) { |
| 71 | AP.ChannelLayout = CTX->ch_layout.u.mask; |
| 72 | } else if (CTX->ch_layout.order == AV_CHANNEL_ORDER_UNSPEC) { |
| 73 | AVChannelLayout ch = {}; |
| 74 | av_channel_layout_default(&ch, CTX->ch_layout.nb_channels); |
| 75 | AP.ChannelLayout = ch.u.mask; |
| 76 | } else { |
| 77 | throw FFMS_Exception(FFMS_ERROR_DECODING, FFMS_ERROR_UNSUPPORTED, "Ambisonics and custom channel orders not supported"); |
| 78 | } |
| 79 | |
| 80 | AP.SampleRate = CTX->sample_rate; |
| 81 | if (!Frames.empty()) { |
| 82 | AP.NumSamples = (Frames.back()).SampleStart + (Frames.back()).SampleCount; |
| 83 | AP.FirstTime = ((Frames.front().PTS * Frames.TB.Num) / (double)Frames.TB.Den) / 1000; |
| 84 | AP.LastTime = ((Frames.back().PTS * Frames.TB.Num) / (double)Frames.TB.Den) / 1000; |
| 85 | AP.LastEndTime = (((Frames.back().PTS + Frames.LastDuration) * Frames.TB.Num) / (double)Frames.TB.Den) / 1000; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | void LAVFOpenFile(const char *SourceFile, AVFormatContext *&FormatContext, int Track, const std::map<std::string, std::string> &LAVFOpts) { |
| 90 | AVDictionary *Dict = nullptr; |
| 91 | for (const auto &iter : LAVFOpts) |
| 92 | av_dict_set(&Dict, iter.first.c_str(), iter.second.c_str(), 0); |
| 93 | |
| 94 | if (avformat_open_input(&FormatContext, SourceFile, nullptr, &Dict) != 0) |
| 95 | throw FFMS_Exception(FFMS_ERROR_PARSER, FFMS_ERROR_FILE_READ, |
| 96 | std::string("Couldn't open '") + SourceFile + "'"); |
| 97 | |
| 98 | av_dict_free(&Dict); |
| 99 | |
| 100 | if (avformat_find_stream_info(FormatContext, nullptr) < 0) { |
| 101 | avformat_close_input(&FormatContext); |
| 102 | FormatContext = nullptr; |
| 103 | throw FFMS_Exception(FFMS_ERROR_PARSER, FFMS_ERROR_FILE_READ, |
| 104 | "Couldn't find stream information"); |
| 105 | } |
| 106 | |
| 107 | for (int i = 0; i < (int)FormatContext->nb_streams; i++) |
| 108 | if (i != Track) |
| 109 | FormatContext->streams[i]->discard = AVDISCARD_ALL; |
| 110 | } |
| 111 | |
| 112 | int ResizerNameToSWSResizer(const char *ResizerName) { |
| 113 | if (!ResizerName) |
| 114 | return 0; |
| 115 | std::string s = ResizerName; |
| 116 | std::transform(s.begin(), s.end(), s.begin(), toupper); |
| 117 | if (s == "FAST_BILINEAR") |
| 118 | return SWS_FAST_BILINEAR; |
| 119 | if (s == "BILINEAR") |
| 120 | return SWS_BILINEAR; |
| 121 | if (s == "BICUBIC") |
| 122 | return SWS_BICUBIC; |
| 123 | if (s == "X") |
| 124 | return SWS_X; |
| 125 | if (s == "POINT") |
| 126 | return SWS_POINT; |
| 127 | if (s == "AREA") |
| 128 | return SWS_AREA; |
| 129 | if (s == "BICUBLIN") |
| 130 | return SWS_BICUBLIN; |
| 131 | if (s == "GAUSS") |
| 132 | return SWS_GAUSS; |
| 133 | if (s == "SINC") |
| 134 | return SWS_SINC; |
| 135 | if (s == "LANCZOS") |
| 136 | return SWS_LANCZOS; |
| 137 | if (s == "SPLINE") |
| 138 | return SWS_SPLINE; |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | bool IsSamePath(const char *p1, const char *p2) { |
| 143 | // assume windows is the only OS with a case insensitive filesystem and ignore all path complications |
| 144 | #ifndef _WIN32 |
| 145 | return !strcmp(p1, p2); |
| 146 | #else |
| 147 | return !_stricmp(p1, p2); |
| 148 | #endif |
| 149 | } |
| 150 | |
| 151 | bool IsIOError(int error) { |
| 152 | switch (error) { |
| 153 | case AVERROR(EIO): |
| 154 | case AVERROR(ETIMEDOUT): |
| 155 | case AVERROR(EPROTO): |
| 156 | case AVERROR(EADDRINUSE): |
| 157 | case AVERROR(EADDRNOTAVAIL): |
| 158 | case AVERROR(ENETDOWN): |
| 159 | case AVERROR(ENETUNREACH): |
| 160 | case AVERROR(ENETRESET): |
| 161 | case AVERROR(ECONNABORTED): |
| 162 | case AVERROR(ECONNRESET): |
| 163 | case AVERROR(ECONNREFUSED): |
| 164 | case AVERROR(EHOSTUNREACH): |
| 165 | #ifndef _WIN32 |
| 166 | case AVERROR(ESHUTDOWN): |
| 167 | case AVERROR(EHOSTDOWN): |
| 168 | #endif |
| 169 | return true; |
| 170 | default: |
| 171 | return false; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | std::string AVErrorToString(int ret) { |
| 176 | char error[1024]; |
| 177 | av_strerror(ret, error, 1024); |
| 178 | std::string cerr(error); |
| 179 | return cerr; |
| 180 | } |