| 1 | // Copyright (c) 2007-2017 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 | #include "ffms.h" |
| 22 | |
| 23 | #include "audiosource.h" |
| 24 | #include "indexing.h" |
| 25 | #include "videosource.h" |
| 26 | #include "videoutils.h" |
| 27 | |
| 28 | extern "C" { |
| 29 | #include <libavutil/avutil.h> |
| 30 | #include <libavutil/pixdesc.h> |
| 31 | } |
| 32 | |
| 33 | #include <mutex> |
| 34 | #include <sstream> |
| 35 | #include <iomanip> |
| 36 | |
| 37 | #ifdef FFMS_WIN_DEBUG |
| 38 | # include <windows.h> |
| 39 | #endif |
| 40 | |
| 41 | static std::once_flag FFmpegOnce; |
| 42 | |
| 43 | #ifdef FFMS_WIN_DEBUG |
| 44 | |
| 45 | static void av_log_windebug_callback(void* ptr, int level, const char* fmt, va_list vl) { |
| 46 | if (level > av_log_get_level()) |
| 47 | return; |
| 48 | |
| 49 | static int print_prefix = 1; |
| 50 | static int count; |
| 51 | static char line[1024] = {}, prev[1024] = {}; |
| 52 | auto avc = ptr ? *static_cast<AVClass **>(ptr) : nullptr; |
| 53 | |
| 54 | int written = 0; |
| 55 | if (print_prefix && avc) { |
| 56 | written = snprintf(line, sizeof(line), "[%s @ %p]", avc->item_name(ptr), ptr); |
| 57 | } |
| 58 | |
| 59 | written += vsnprintf(line + written, sizeof(line) - written, fmt, vl); |
| 60 | |
| 61 | print_prefix = line[written - 1] == '\n'; |
| 62 | line[sizeof(line) - 1] = 0; |
| 63 | if (print_prefix && !strcmp(line, prev)) { |
| 64 | count++; |
| 65 | return; |
| 66 | } |
| 67 | if (count > 0) { |
| 68 | std::stringstream ss; |
| 69 | ss << " Last message repeated " << count << " times\n"; |
| 70 | OutputDebugStringA(ss.str().c_str()); |
| 71 | count = 0; |
| 72 | } |
| 73 | OutputDebugStringA(line); |
| 74 | strcpy(prev, line); |
| 75 | } |
| 76 | |
| 77 | #endif |
| 78 | |
| 79 | FFMS_API(void) FFMS_Init(int, int) { |
| 80 | std::call_once(FFmpegOnce, []() { |
| 81 | #ifdef FFMS_WIN_DEBUG |
| 82 | av_log_set_callback(av_log_windebug_callback); |
| 83 | av_log_set_level(AV_LOG_INFO); |
| 84 | #else |
| 85 | av_log_set_level(AV_LOG_QUIET); |
| 86 | #endif |
| 87 | }); |
| 88 | } |
| 89 | |
| 90 | FFMS_API(int) FFMS_GetVersion() { |
| 91 | return FFMS_VERSION; |
| 92 | } |
| 93 | |
| 94 | FFMS_API(int) FFMS_GetLogLevel() { |
| 95 | return av_log_get_level(); |
| 96 | } |
| 97 | |
| 98 | FFMS_API(void) FFMS_SetLogLevel(int Level) { |
| 99 | av_log_set_level(Level); |
| 100 | } |
| 101 | |
| 102 | FFMS_API(FFMS_VideoSource *) FFMS_CreateVideoSource(const char *SourceFile, int Track, FFMS_Index *Index, int Threads, int SeekMode, FFMS_ErrorInfo *ErrorInfo) { |
| 103 | try { |
| 104 | return new FFMS_VideoSource(SourceFile, *Index, Track, Threads, SeekMode); |
| 105 | } catch (FFMS_Exception &e) { |
| 106 | e.CopyOut(ErrorInfo); |
| 107 | return nullptr; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | FFMS_API(FFMS_AudioSource *) FFMS_CreateAudioSource(const char *SourceFile, int Track, FFMS_Index *Index, int DelayMode, FFMS_ErrorInfo *ErrorInfo) { |
| 112 | return FFMS_CreateAudioSource2(SourceFile, Track, Index, DelayMode, -1, 0, ErrorInfo); |
| 113 | } |
| 114 | |
| 115 | FFMS_API(FFMS_AudioSource *) FFMS_CreateAudioSource2(const char *SourceFile, int Track, FFMS_Index *Index, int DelayMode, int FillGaps, double DrcScale, FFMS_ErrorInfo *ErrorInfo) { |
| 116 | try { |
| 117 | return new FFMS_AudioSource(SourceFile, *Index, Track, DelayMode, FillGaps, DrcScale); |
| 118 | } catch (FFMS_Exception &e) { |
| 119 | e.CopyOut(ErrorInfo); |
| 120 | return nullptr; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | FFMS_API(void) FFMS_DestroyVideoSource(FFMS_VideoSource *V) { |
| 125 | delete V; |
| 126 | } |
| 127 | |
| 128 | FFMS_API(void) FFMS_DestroyAudioSource(FFMS_AudioSource *A) { |
| 129 | delete A; |
| 130 | } |
| 131 | |
| 132 | FFMS_API(const FFMS_VideoProperties *) FFMS_GetVideoProperties(FFMS_VideoSource *V) { |
| 133 | return &V->GetVideoProperties(); |
| 134 | } |
| 135 | |
| 136 | FFMS_API(const FFMS_AudioProperties *) FFMS_GetAudioProperties(FFMS_AudioSource *A) { |
| 137 | return &A->GetAudioProperties(); |
| 138 | } |
| 139 | |
| 140 | FFMS_API(const FFMS_Frame *) FFMS_GetFrame(FFMS_VideoSource *V, int n, FFMS_ErrorInfo *ErrorInfo) { |
| 141 | ClearErrorInfo(ErrorInfo); |
| 142 | try { |
| 143 | return V->GetFrame(n); |
| 144 | } catch (FFMS_Exception &e) { |
| 145 | e.CopyOut(ErrorInfo); |
| 146 | return nullptr; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | FFMS_API(const FFMS_Frame *) FFMS_GetFrameByTime(FFMS_VideoSource *V, double Time, FFMS_ErrorInfo *ErrorInfo) { |
| 151 | ClearErrorInfo(ErrorInfo); |
| 152 | try { |
| 153 | return V->GetFrameByTime(Time); |
| 154 | } catch (FFMS_Exception &e) { |
| 155 | e.CopyOut(ErrorInfo); |
| 156 | return nullptr; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | FFMS_API(int) FFMS_GetAudio(FFMS_AudioSource *A, void *Buf, int64_t Start, int64_t Count, FFMS_ErrorInfo *ErrorInfo) { |
| 161 | ClearErrorInfo(ErrorInfo); |
| 162 | try { |
| 163 | A->GetAudio(Buf, Start, Count); |
| 164 | } catch (FFMS_Exception &e) { |
| 165 | return e.CopyOut(ErrorInfo); |
| 166 | } |
| 167 | return FFMS_ERROR_SUCCESS; |
| 168 | } |
| 169 | |
| 170 | FFMS_API(int) FFMS_SetOutputFormatV2(FFMS_VideoSource *V, const int *TargetFormats, int Width, int Height, int Resizer, FFMS_ErrorInfo *ErrorInfo) { |
| 171 | ClearErrorInfo(ErrorInfo); |
| 172 | try { |
| 173 | V->SetOutputFormat(reinterpret_cast<const AVPixelFormat *>(TargetFormats), Width, Height, Resizer); |
| 174 | } catch (FFMS_Exception &e) { |
| 175 | return e.CopyOut(ErrorInfo); |
| 176 | } |
| 177 | return FFMS_ERROR_SUCCESS; |
| 178 | } |
| 179 | |
| 180 | FFMS_API(void) FFMS_ResetOutputFormatV(FFMS_VideoSource *V) { |
| 181 | V->ResetOutputFormat(); |
| 182 | } |
| 183 | |
| 184 | FFMS_API(int) FFMS_SetInputFormatV(FFMS_VideoSource *V, int ColorSpace, int ColorRange, int Format, FFMS_ErrorInfo *ErrorInfo) { |
| 185 | ClearErrorInfo(ErrorInfo); |
| 186 | try { |
| 187 | V->SetInputFormat(ColorSpace, ColorRange, static_cast<AVPixelFormat>(Format)); |
| 188 | } catch (FFMS_Exception &e) { |
| 189 | return e.CopyOut(ErrorInfo); |
| 190 | } |
| 191 | return FFMS_ERROR_SUCCESS; |
| 192 | } |
| 193 | |
| 194 | FFMS_API(void) FFMS_ResetInputFormatV(FFMS_VideoSource *V) { |
| 195 | V->ResetInputFormat(); |
| 196 | } |
| 197 | |
| 198 | FFMS_API(FFMS_ResampleOptions *) FFMS_CreateResampleOptions(FFMS_AudioSource *A) { |
| 199 | return A->CreateResampleOptions().release(); |
| 200 | } |
| 201 | |
| 202 | FFMS_API(void) FFMS_DestroyResampleOptions(FFMS_ResampleOptions *options) { |
| 203 | delete options; |
| 204 | } |
| 205 | |
| 206 | FFMS_API(int) FFMS_SetOutputFormatA(FFMS_AudioSource *A, const FFMS_ResampleOptions *options, FFMS_ErrorInfo *ErrorInfo) { |
| 207 | ClearErrorInfo(ErrorInfo); |
| 208 | try { |
| 209 | A->SetOutputFormat(*options); |
| 210 | } catch (FFMS_Exception &e) { |
| 211 | return e.CopyOut(ErrorInfo); |
| 212 | } |
| 213 | return FFMS_ERROR_SUCCESS; |
| 214 | } |
| 215 | |
| 216 | FFMS_API(void) FFMS_DestroyIndex(FFMS_Index *Index) { |
| 217 | delete Index; |
| 218 | } |
| 219 | |
| 220 | FFMS_API(FFMS_IndexErrorHandling) FFMS_GetErrorHandling(FFMS_Index *Index) { |
| 221 | return static_cast<FFMS_IndexErrorHandling>(Index->ErrorHandling); |
| 222 | } |
| 223 | |
| 224 | FFMS_API(int) FFMS_GetFirstTrackOfType(FFMS_Index *Index, int TrackType, FFMS_ErrorInfo *ErrorInfo) { |
| 225 | ClearErrorInfo(ErrorInfo); |
| 226 | for (int i = 0; i < static_cast<int>(Index->size()); i++) |
| 227 | if ((*Index)[i].TT == TrackType) |
| 228 | return i; |
| 229 | |
| 230 | try { |
| 231 | throw FFMS_Exception(FFMS_ERROR_INDEX, FFMS_ERROR_NOT_AVAILABLE, |
| 232 | "No suitable, indexed track found"); |
| 233 | } catch (FFMS_Exception &e) { |
| 234 | e.CopyOut(ErrorInfo); |
| 235 | return -1; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | FFMS_API(int) FFMS_GetFirstIndexedTrackOfType(FFMS_Index *Index, int TrackType, FFMS_ErrorInfo *ErrorInfo) { |
| 240 | ClearErrorInfo(ErrorInfo); |
| 241 | for (int i = 0; i < static_cast<int>(Index->size()); i++) |
| 242 | if ((*Index)[i].TT == TrackType && !(*Index)[i].empty()) |
| 243 | return i; |
| 244 | try { |
| 245 | throw FFMS_Exception(FFMS_ERROR_INDEX, FFMS_ERROR_NOT_AVAILABLE, |
| 246 | "No suitable, indexed track found"); |
| 247 | } catch (FFMS_Exception &e) { |
| 248 | e.CopyOut(ErrorInfo); |
| 249 | return -1; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | FFMS_API(int) FFMS_GetNumTracks(FFMS_Index *Index) { |
| 254 | return static_cast<int>(Index->size()); |
| 255 | } |
| 256 | |
| 257 | FFMS_API(int) FFMS_GetNumTracksI(FFMS_Indexer *Indexer) { |
| 258 | return Indexer->GetNumberOfTracks(); |
| 259 | } |
| 260 | |
| 261 | FFMS_API(int) FFMS_GetTrackType(FFMS_Track *T) { |
| 262 | return T->TT; |
| 263 | } |
| 264 | |
| 265 | FFMS_API(int) FFMS_GetTrackTypeI(FFMS_Indexer *Indexer, int Track) { |
| 266 | return Indexer->GetTrackType(Track); |
| 267 | } |
| 268 | |
| 269 | FFMS_API(const char *) FFMS_GetCodecNameI(FFMS_Indexer *Indexer, int Track) { |
| 270 | return Indexer->GetTrackCodec(Track); |
| 271 | } |
| 272 | |
| 273 | FFMS_API(int) FFMS_GetNumFrames(FFMS_Track *T) { |
| 274 | return T->VisibleFrameCount(); |
| 275 | } |
| 276 | |
| 277 | FFMS_API(const FFMS_FrameInfo *) FFMS_GetFrameInfo(FFMS_Track *T, int Frame) { |
| 278 | return T->GetFrameInfo(static_cast<size_t>(Frame)); |
| 279 | } |
| 280 | |
| 281 | FFMS_API(FFMS_Track *) FFMS_GetTrackFromIndex(FFMS_Index *Index, int Track) { |
| 282 | return &(*Index)[Track]; |
| 283 | } |
| 284 | |
| 285 | FFMS_API(FFMS_Track *) FFMS_GetTrackFromVideo(FFMS_VideoSource *V) { |
| 286 | return V->GetTrack(); |
| 287 | } |
| 288 | |
| 289 | FFMS_API(FFMS_Track *) FFMS_GetTrackFromAudio(FFMS_AudioSource *A) { |
| 290 | return A->GetTrack(); |
| 291 | } |
| 292 | |
| 293 | FFMS_API(const FFMS_TrackTimeBase *) FFMS_GetTimeBase(FFMS_Track *T) { |
| 294 | return &T->TB; |
| 295 | } |
| 296 | |
| 297 | FFMS_API(int) FFMS_WriteTimecodes(FFMS_Track *T, const char *TimecodeFile, FFMS_ErrorInfo *ErrorInfo) { |
| 298 | ClearErrorInfo(ErrorInfo); |
| 299 | try { |
| 300 | T->WriteTimecodes(TimecodeFile); |
| 301 | } catch (FFMS_Exception &e) { |
| 302 | return e.CopyOut(ErrorInfo); |
| 303 | } |
| 304 | return FFMS_ERROR_SUCCESS; |
| 305 | } |
| 306 | |
| 307 | FFMS_API(FFMS_Indexer *) FFMS_CreateIndexer(const char *SourceFile, FFMS_ErrorInfo *ErrorInfo) { |
| 308 | return FFMS_CreateIndexer2(SourceFile, nullptr, 0, ErrorInfo); |
| 309 | } |
| 310 | |
| 311 | FFMS_API(FFMS_Indexer *) FFMS_CreateIndexer2(const char *SourceFile, const FFMS_KeyValuePair *DemuxerOptions, int NumOptions, FFMS_ErrorInfo *ErrorInfo) { |
| 312 | ClearErrorInfo(ErrorInfo); |
| 313 | try { |
| 314 | return new FFMS_Indexer(SourceFile, DemuxerOptions, NumOptions); |
| 315 | } catch (FFMS_Exception &e) { |
| 316 | e.CopyOut(ErrorInfo); |
| 317 | return nullptr; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | FFMS_API(FFMS_Index *) FFMS_DoIndexing2(FFMS_Indexer *Indexer, int ErrorHandling, FFMS_ErrorInfo *ErrorInfo) { |
| 322 | ClearErrorInfo(ErrorInfo); |
| 323 | |
| 324 | Indexer->SetErrorHandling(ErrorHandling); |
| 325 | |
| 326 | FFMS_Index *Index = nullptr; |
| 327 | try { |
| 328 | Index = Indexer->DoIndexing(); |
| 329 | } catch (FFMS_Exception &e) { |
| 330 | e.CopyOut(ErrorInfo); |
| 331 | } |
| 332 | delete Indexer; |
| 333 | return Index; |
| 334 | } |
| 335 | |
| 336 | FFMS_API(void) FFMS_TrackIndexSettings(FFMS_Indexer *Indexer, int Track, int Index, int) { |
| 337 | Indexer->SetIndexTrack(Track, !!Index); |
| 338 | } |
| 339 | |
| 340 | FFMS_API(void) FFMS_TrackTypeIndexSettings(FFMS_Indexer *Indexer, int TrackType, int Index, int) { |
| 341 | Indexer->SetIndexTrackType(TrackType, !!Index); |
| 342 | } |
| 343 | |
| 344 | FFMS_API(void) FFMS_SetProgressCallback(FFMS_Indexer *Indexer, TIndexCallback IC, void *ICPrivate) { |
| 345 | Indexer->SetProgressCallback(IC, ICPrivate); |
| 346 | } |
| 347 | |
| 348 | FFMS_API(void) FFMS_CancelIndexing(FFMS_Indexer *Indexer) { |
| 349 | delete Indexer; |
| 350 | } |
| 351 | |
| 352 | FFMS_API(FFMS_Index *) FFMS_ReadIndex(const char *IndexFile, FFMS_ErrorInfo *ErrorInfo) { |
| 353 | ClearErrorInfo(ErrorInfo); |
| 354 | try { |
| 355 | return new FFMS_Index(IndexFile); |
| 356 | } catch (FFMS_Exception &e) { |
| 357 | e.CopyOut(ErrorInfo); |
| 358 | return nullptr; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | FFMS_API(FFMS_Index *) FFMS_ReadIndexFromBuffer(const uint8_t *Buffer, size_t Size, FFMS_ErrorInfo *ErrorInfo) { |
| 363 | ClearErrorInfo(ErrorInfo); |
| 364 | try { |
| 365 | return new FFMS_Index(Buffer, Size); |
| 366 | } catch (FFMS_Exception &e) { |
| 367 | e.CopyOut(ErrorInfo); |
| 368 | return nullptr; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | FFMS_API(int) FFMS_IndexBelongsToFile(FFMS_Index *Index, const char *SourceFile, FFMS_ErrorInfo *ErrorInfo) { |
| 373 | ClearErrorInfo(ErrorInfo); |
| 374 | try { |
| 375 | if (!Index->CompareFileSignature(SourceFile)) |
| 376 | throw FFMS_Exception(FFMS_ERROR_INDEX, FFMS_ERROR_FILE_MISMATCH, |
| 377 | "The index does not belong to the file"); |
| 378 | } catch (FFMS_Exception &e) { |
| 379 | return e.CopyOut(ErrorInfo); |
| 380 | } |
| 381 | return FFMS_ERROR_SUCCESS; |
| 382 | } |
| 383 | |
| 384 | FFMS_API(int) FFMS_WriteIndex(const char *IndexFile, FFMS_Index *Index, FFMS_ErrorInfo *ErrorInfo) { |
| 385 | ClearErrorInfo(ErrorInfo); |
| 386 | try { |
| 387 | Index->WriteIndexFile(IndexFile); |
| 388 | } catch (FFMS_Exception &e) { |
| 389 | return e.CopyOut(ErrorInfo); |
| 390 | } |
| 391 | return FFMS_ERROR_SUCCESS; |
| 392 | } |
| 393 | |
| 394 | FFMS_API(int) FFMS_WriteIndexToBuffer(uint8_t **BufferPtr, size_t *Size, FFMS_Index *Index, FFMS_ErrorInfo *ErrorInfo) { |
| 395 | ClearErrorInfo(ErrorInfo); |
| 396 | uint8_t *buf; |
| 397 | |
| 398 | try { |
| 399 | buf = Index->WriteIndexBuffer(Size); |
| 400 | } catch (FFMS_Exception &e) { |
| 401 | *Size = 0; |
| 402 | *BufferPtr = nullptr; |
| 403 | return e.CopyOut(ErrorInfo); |
| 404 | } |
| 405 | |
| 406 | *BufferPtr = buf; |
| 407 | |
| 408 | return FFMS_ERROR_SUCCESS; |
| 409 | } |
| 410 | |
| 411 | FFMS_API(void) FFMS_FreeIndexBuffer(uint8_t **BufferPtr) { |
| 412 | av_freep(BufferPtr); |
| 413 | } |
| 414 | |
| 415 | FFMS_API(int) FFMS_GetPixFmt(const char *Name) { |
| 416 | return av_get_pix_fmt(Name); |
| 417 | } |
| 418 | |
| 419 | FFMS_API(const char *) FFMS_GetFormatNameI(FFMS_Indexer *Indexer) { |
| 420 | return Indexer->GetFormatName(); |
| 421 | } |