Debian Codegraph @

src/avisynth/avssources.cpp

raw symbols SCIP
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 #define NOMINMAX
22 #include "avssources.h"
23 #include "../vapoursynth/VSHelper4.h"
24 #include "../core/utils.h"
25
26 #include <algorithm>
27 #include <cassert>
28
29 static AVPixelFormat CSNameToPIXFMT(const char *CSName, AVPixelFormat Default, bool HighBitDepth) {
30 if (!CSName)
31 return AV_PIX_FMT_NONE;
32 std::string s = CSName;
33 std::transform(s.begin(), s.end(), s.begin(), toupper);
34 if (s == "")
35 return Default;
36 if (s == "YUV9" || s == "YUV410P8")
37 return AV_PIX_FMT_YUV410P;
38 if (s == "YV411" || s == "YUV411P8")
39 return AV_PIX_FMT_YUV411P;
40 if (s == "YV12" || s == "YUV420P8")
41 return AV_PIX_FMT_YUV420P;
42 if (s == "YV16" || s == "YUV422P8")
43 return AV_PIX_FMT_YUV422P;
44 if (s == "YV24" || s == "YUV444P8")
45 return AV_PIX_FMT_YUV444P;
46 if (s == "Y8" || s == "GRAY8")
47 return AV_PIX_FMT_GRAY8;
48 if (s == "YUY2")
49 return AV_PIX_FMT_YUYV422;
50 if (s == "RGB24")
51 return AV_PIX_FMT_BGR24;
52 if (s == "RGB32")
53 return AV_PIX_FMT_RGB32;
54 if (HighBitDepth) {
55 if (s == "YUVA420P8")
56 return AV_PIX_FMT_YUVA420P;
57 if (s == "YUVA422P8")
58 return AV_PIX_FMT_YUVA422P;
59 if (s == "YUVA444P8")
60 return AV_PIX_FMT_YUVA444P;
61 if (s == "YUV420P16")
62 return AV_PIX_FMT_YUV420P16;
63 if (s == "YUVA420P16")
64 return AV_PIX_FMT_YUVA420P16;
65 if (s == "YUV422P16")
66 return AV_PIX_FMT_YUV422P16;
67 if (s == "YUVA422P16")
68 return AV_PIX_FMT_YUVA422P16;
69 if (s == "YUV444P16")
70 return AV_PIX_FMT_YUV444P16;
71 if (s == "YUVA444P16")
72 return AV_PIX_FMT_YUVA444P16;
73 if (s == "YUV420P10")
74 return AV_PIX_FMT_YUV420P10;
75 if (s == "YUVA420P10")
76 return AV_PIX_FMT_YUVA420P10;
77 if (s == "YUV422P10")
78 return AV_PIX_FMT_YUV422P10;
79 if (s == "YUVA422P10")
80 return AV_PIX_FMT_YUVA422P10;
81 if (s == "YUV444P10")
82 return AV_PIX_FMT_YUV444P10;
83 if (s == "YUVA444P10")
84 return AV_PIX_FMT_YUVA444P10;
85 if (s == "RGBP8")
86 return AV_PIX_FMT_GBRP;
87 if (s == "RGBP10")
88 return AV_PIX_FMT_GBRP10;
89 if (s == "RGBP12")
90 return AV_PIX_FMT_GBRP12;
91 if (s == "RGBP16")
92 return AV_PIX_FMT_GBRP16;
93 if (s == "RGBPS")
94 return AV_PIX_FMT_GBRPF32;
95 if (s == "RGBAP8")
96 return AV_PIX_FMT_GBRAP;
97 if (s == "RGBAP10")
98 return AV_PIX_FMT_GBRAP10;
99 if (s == "RGBAP12")
100 return AV_PIX_FMT_GBRAP12;
101 if (s == "RGBAP16")
102 return AV_PIX_FMT_GBRAP16;
103 if (s == "RGBAPS")
104 return AV_PIX_FMT_GBRAPF32;
105 if (s == "Y10" || s == "GRAY10")
106 return AV_PIX_FMT_GRAY10;
107 if (s == "Y12" || s == "GRAY12")
108 return AV_PIX_FMT_GRAY12;
109 if (s == "Y16" || s == "GRAY16")
110 return AV_PIX_FMT_GRAY16;
111 }
112
113 return AV_PIX_FMT_NONE;
114 }
115
116 AvisynthVideoSource::AvisynthVideoSource(const char *SourceFile, int Track, FFMS_Index *Index,
117 int AFPSNum, int AFPSDen, int Threads, int SeekMode,
118 int ResizeToWidth, int ResizeToHeight, const char *ResizerName,
119 const char *ConvertToFormatName, const char *VarPrefix, IScriptEnvironment* Env)
120 : FPSNum(AFPSNum)
121 , FPSDen(AFPSDen)
122 , VarPrefix(VarPrefix) {
123 VI = {};
124
125 // check if the two functions we need for many bits are present
126 VI.pixel_type = VideoInfo::CS_Y16;
127 HighBitDepth = (VI.ComponentSize() == 2 && VI.IsY());
128 VI.pixel_type = VideoInfo::CS_UNKNOWN;
129
130 ErrorInfo E;
131 V = FFMS_CreateVideoSource(SourceFile, Track, Index, Threads, SeekMode, &E);
132 if (!V)
133 Env->ThrowError("FFVideoSource: %s", E.Buffer);
134
135 try {
136 InitOutputFormat(ResizeToWidth, ResizeToHeight, ResizerName, ConvertToFormatName, Env);
137 } catch (AvisynthError &) {
138 FFMS_DestroyVideoSource(V);
139 throw;
140 }
141
142 const FFMS_VideoProperties *VP = FFMS_GetVideoProperties(V);
143
144 VI.fps_denominator = VP->FPSDenominator;
145 VI.fps_numerator = VP->FPSNumerator;
146 VI.num_frames = VP->NumFrames;
147 if (FPSNum > 0 && FPSDen > 0) {
148 vsh::reduceRational(&FPSNum, &FPSDen);
149 VI.fps_denominator = FPSDen;
150 VI.fps_numerator = FPSNum;
151 if (VP->NumFrames > 1) {
152 VI.num_frames = static_cast<int>((VP->LastTime - VP->FirstTime) * (1 + 1. / (VP->NumFrames - 1)) * FPSNum / FPSDen + 0.5);
153 if (VI.num_frames < 1)
154 VI.num_frames = 1;
155 } else {
156 VI.num_frames = 1;
157 }
158 }
159
160 // Set AR variables
161 Env->SetVar(Env->Sprintf("%s%s", this->VarPrefix, "FFSAR_NUM"), VP->SARNum);
162 Env->SetVar(Env->Sprintf("%s%s", this->VarPrefix, "FFSAR_DEN"), VP->SARDen);
163 if (VP->SARNum > 0 && VP->SARDen > 0)
164 Env->SetVar(Env->Sprintf("%s%s", this->VarPrefix, "FFSAR"), VP->SARNum / (double)VP->SARDen);
165
166 // Set crop variables
167 Env->SetVar(Env->Sprintf("%s%s", this->VarPrefix, "FFCROP_LEFT"), VP->CropLeft);
168 Env->SetVar(Env->Sprintf("%s%s", this->VarPrefix, "FFCROP_RIGHT"), VP->CropRight);
169 Env->SetVar(Env->Sprintf("%s%s", this->VarPrefix, "FFCROP_TOP"), VP->CropTop);
170 Env->SetVar(Env->Sprintf("%s%s", this->VarPrefix, "FFCROP_BOTTOM"), VP->CropBottom);
171
172 Env->SetGlobalVar("FFVAR_PREFIX", this->VarPrefix);
173
174 has_at_least_v8 = true;
175 try {
176 Env->CheckVersion(8);
177 } catch (const AvisynthError &) {
178 has_at_least_v8 = false;
179 }
180 }
181
182 AvisynthVideoSource::~AvisynthVideoSource() {
183 FFMS_DestroyVideoSource(V);
184 }
185
186 static int GetSubSamplingH(const VideoInfo &vi) {
187 if ((vi.IsYUV() || vi.IsYUVA()) && !vi.IsY() && vi.IsPlanar())
188 return vi.GetPlaneHeightSubsampling(PLANAR_U);
189 else
190 return 0;
191 }
192
193 static int GetSubSamplingW(const VideoInfo &vi) {
194 if ((vi.IsYUV() || vi.IsYUVA()) && !vi.IsY() && vi.IsPlanar())
195 return vi.GetPlaneWidthSubsampling(PLANAR_U);
196 else
197 return 0;
198 }
199
200 void AvisynthVideoSource::InitOutputFormat(
201 int ResizeToWidth, int ResizeToHeight, const char *ResizerName,
202 const char *ConvertToFormatName, IScriptEnvironment *Env) {
203
204 ErrorInfo E;
205 const FFMS_VideoProperties *VP = FFMS_GetVideoProperties(V);
206 const FFMS_Frame *F = FFMS_GetFrame(V, 0, &E);
207 if (!F)
208 Env->ThrowError("FFVideoSource: %s", E.Buffer);
209
210 std::vector<int> TargetFormats;
211 if (HighBitDepth) {
212 TargetFormats.push_back(FFMS_GetPixFmt("yuv420p16"));
213 TargetFormats.push_back(FFMS_GetPixFmt("yuva420p16"));
214 TargetFormats.push_back(FFMS_GetPixFmt("yuv422p16"));
215 TargetFormats.push_back(FFMS_GetPixFmt("yuva422p16"));
216 TargetFormats.push_back(FFMS_GetPixFmt("yuv444p16"));
217 TargetFormats.push_back(FFMS_GetPixFmt("yuva444p16"));
218 TargetFormats.push_back(FFMS_GetPixFmt("yuv420p10"));
219 TargetFormats.push_back(FFMS_GetPixFmt("yuva420p10"));
220 TargetFormats.push_back(FFMS_GetPixFmt("yuv422p10"));
221 TargetFormats.push_back(FFMS_GetPixFmt("yuva422p10"));
222 TargetFormats.push_back(FFMS_GetPixFmt("yuv444p10"));
223 TargetFormats.push_back(FFMS_GetPixFmt("yuva444p10"));
224 TargetFormats.push_back(FFMS_GetPixFmt("gbrpf32"));
225 TargetFormats.push_back(FFMS_GetPixFmt("gbrapf32"));
226 TargetFormats.push_back(FFMS_GetPixFmt("gbrp16"));
227 TargetFormats.push_back(FFMS_GetPixFmt("gbrap16"));
228 TargetFormats.push_back(FFMS_GetPixFmt("gbrp12"));
229 TargetFormats.push_back(FFMS_GetPixFmt("gbrap12"));
230 TargetFormats.push_back(FFMS_GetPixFmt("gbrp10"));
231 TargetFormats.push_back(FFMS_GetPixFmt("gbrap10"));
232 TargetFormats.push_back(FFMS_GetPixFmt("gray16"));
233 TargetFormats.push_back(FFMS_GetPixFmt("gray12"));
234 TargetFormats.push_back(FFMS_GetPixFmt("gray10"));
235 TargetFormats.push_back(FFMS_GetPixFmt("yuva420p"));
236 TargetFormats.push_back(FFMS_GetPixFmt("yuva422p"));
237 TargetFormats.push_back(FFMS_GetPixFmt("yuva444p"));
238 }
239 TargetFormats.push_back(FFMS_GetPixFmt("yuv410p"));
240 TargetFormats.push_back(FFMS_GetPixFmt("yuv411p"));
241 TargetFormats.push_back(FFMS_GetPixFmt("yuv420p"));
242 TargetFormats.push_back(FFMS_GetPixFmt("yuv422p"));
243 TargetFormats.push_back(FFMS_GetPixFmt("yuv444p"));
244 TargetFormats.push_back(FFMS_GetPixFmt("gray8"));
245 TargetFormats.push_back(FFMS_GetPixFmt("yuyv422"));
246 TargetFormats.push_back(FFMS_GetPixFmt("bgra"));
247
248 // Remove unsupported formats from list so they don't appear as an early termination
249 TargetFormats.erase(std::remove(TargetFormats.begin(), TargetFormats.end(), -1), TargetFormats.end());
250
251 TargetFormats.push_back(-1);
252
253 // PIX_FMT_NV21 is misused as a return value different to the defined ones in the function
254 AVPixelFormat TargetPixelFormat = CSNameToPIXFMT(ConvertToFormatName, AV_PIX_FMT_NV21, HighBitDepth);
255 if (TargetPixelFormat == AV_PIX_FMT_NONE)
256 Env->ThrowError("FFVideoSource: Invalid colorspace name specified");
257
258 if (TargetPixelFormat != AV_PIX_FMT_NV21) {
259 TargetFormats.clear();
260 TargetFormats.push_back(TargetPixelFormat);
261 TargetFormats.push_back(-1);
262 }
263
264 if (ResizeToWidth <= 0)
265 ResizeToWidth = F->EncodedWidth;
266
267 if (ResizeToHeight <= 0)
268 ResizeToHeight = F->EncodedHeight;
269
270 int Resizer = ResizerNameToSWSResizer(ResizerName);
271 if (Resizer == 0)
272 Env->ThrowError("FFVideoSource: Invalid resizer name specified");
273
274 if (FFMS_SetOutputFormatV2(V, TargetFormats.data(),
275 ResizeToWidth, ResizeToHeight, Resizer, &E))
276 Env->ThrowError("FFVideoSource: No suitable output format found");
277
278 F = FFMS_GetFrame(V, 0, &E);
279 TargetFormats.clear();
280 TargetFormats.push_back(F->ConvertedPixelFormat);
281 TargetFormats.push_back(-1);
282
283 // This trick is required to first get the "best" default format and then set only that format as the output
284 if (FFMS_SetOutputFormatV2(V, TargetFormats.data(),
285 ResizeToWidth, ResizeToHeight, Resizer, &E))
286 Env->ThrowError("FFVideoSource: No suitable output format found");
287
288 F = FFMS_GetFrame(V, 0, &E);
289
290 if (F->ConvertedPixelFormat == FFMS_GetPixFmt("yuvj420p") || F->ConvertedPixelFormat == FFMS_GetPixFmt("yuv420p"))
291 VI.pixel_type = VideoInfo::CS_I420;
292 else if (F->ConvertedPixelFormat == FFMS_GetPixFmt("yuva420p"))
293 VI.pixel_type = VideoInfo::CS_YUVA420;
294 else if (F->ConvertedPixelFormat == FFMS_GetPixFmt("yuvj422p") || F->ConvertedPixelFormat == FFMS_GetPixFmt("yuv422p"))
295 VI.pixel_type = VideoInfo::CS_YV16;
296 else if (F->ConvertedPixelFormat == FFMS_GetPixFmt("yuva422p"))
297 VI.pixel_type = VideoInfo::CS_YUVA422;
298 else if (F->ConvertedPixelFormat == FFMS_GetPixFmt("yuvj444p") || F->ConvertedPixelFormat == FFMS_GetPixFmt("yuv444p"))
299 VI.pixel_type = VideoInfo::CS_YV24;
300 else if (F->ConvertedPixelFormat == FFMS_GetPixFmt("yuva444p"))
301 VI.pixel_type = VideoInfo::CS_YUVA444;
302 else if (F->ConvertedPixelFormat == FFMS_GetPixFmt("yuv411p"))
303 VI.pixel_type = VideoInfo::CS_YV411;
304 else if (F->ConvertedPixelFormat == FFMS_GetPixFmt("yuv410p"))
305 VI.pixel_type = VideoInfo::CS_YUV9;
306 else if (F->ConvertedPixelFormat == FFMS_GetPixFmt("gray8"))
307 VI.pixel_type = VideoInfo::CS_Y8;
308 else if (F->ConvertedPixelFormat == FFMS_GetPixFmt("yuyv422"))
309 VI.pixel_type = VideoInfo::CS_YUY2;
310 else if (F->ConvertedPixelFormat == FFMS_GetPixFmt("rgb32"))
311 VI.pixel_type = VideoInfo::CS_BGR32;
312 else if (F->ConvertedPixelFormat == FFMS_GetPixFmt("bgr24"))
313 VI.pixel_type = VideoInfo::CS_BGR24;
314 else if (F->ConvertedPixelFormat == FFMS_GetPixFmt("yuv420p16"))
315 VI.pixel_type = VideoInfo::CS_YUV420P16;
316 else if (F->ConvertedPixelFormat == FFMS_GetPixFmt("yuva420p16"))
317 VI.pixel_type = VideoInfo::CS_YUVA420P16;
318 else if (F->ConvertedPixelFormat == FFMS_GetPixFmt("yuv422p16"))
319 VI.pixel_type = VideoInfo::CS_YUV422P16;
320 else if (F->ConvertedPixelFormat == FFMS_GetPixFmt("yuva422p16"))
321 VI.pixel_type = VideoInfo::CS_YUVA422P16;
322 else if (F->ConvertedPixelFormat == FFMS_GetPixFmt("yuv444p16"))
323 VI.pixel_type = VideoInfo::CS_YUV444P16;
324 else if (F->ConvertedPixelFormat == FFMS_GetPixFmt("yuva444p16"))
325 VI.pixel_type = VideoInfo::CS_YUVA444P16;
326 else if (F->ConvertedPixelFormat == FFMS_GetPixFmt("yuv420p10"))
327 VI.pixel_type = VideoInfo::CS_YUV420P10;
328 else if (F->ConvertedPixelFormat == FFMS_GetPixFmt("yuva420p10"))
329 VI.pixel_type = VideoInfo::CS_YUVA420P10;
330 else if (F->ConvertedPixelFormat == FFMS_GetPixFmt("yuv422p10"))
331 VI.pixel_type = VideoInfo::CS_YUV422P10;
332 else if (F->ConvertedPixelFormat == FFMS_GetPixFmt("yuva422p10"))
333 VI.pixel_type = VideoInfo::CS_YUVA422P10;
334 else if (F->ConvertedPixelFormat == FFMS_GetPixFmt("yuv444p10"))
335 VI.pixel_type = VideoInfo::CS_YUV444P10;
336 else if (F->ConvertedPixelFormat == FFMS_GetPixFmt("yuva444p10"))
337 VI.pixel_type = VideoInfo::CS_YUVA444P10;
338 else if (F->ConvertedPixelFormat == FFMS_GetPixFmt("gbrpf32"))
339 VI.pixel_type = VideoInfo::CS_RGBPS;
340 else if (F->ConvertedPixelFormat == FFMS_GetPixFmt("gbrp16"))
341 VI.pixel_type = VideoInfo::CS_RGBP16;
342 else if (F->ConvertedPixelFormat == FFMS_GetPixFmt("gbrp12"))
343 VI.pixel_type = VideoInfo::CS_RGBP12;
344 else if (F->ConvertedPixelFormat == FFMS_GetPixFmt("gbrp10"))
345 VI.pixel_type = VideoInfo::CS_RGBP10;
346 else if (F->ConvertedPixelFormat == FFMS_GetPixFmt("gbrapf32"))
347 VI.pixel_type = VideoInfo::CS_RGBAPS;
348 else if (F->ConvertedPixelFormat == FFMS_GetPixFmt("gbrap16"))
349 VI.pixel_type = VideoInfo::CS_RGBAP16;
350 else if (F->ConvertedPixelFormat == FFMS_GetPixFmt("gbrap12"))
351 VI.pixel_type = VideoInfo::CS_RGBAP12;
352 else if (F->ConvertedPixelFormat == FFMS_GetPixFmt("gbrap10"))
353 VI.pixel_type = VideoInfo::CS_RGBAP10;
354 else if (F->ConvertedPixelFormat == FFMS_GetPixFmt("gbrp"))
355 VI.pixel_type = VideoInfo::CS_RGBP;
356 else if (F->ConvertedPixelFormat == FFMS_GetPixFmt("gbrap"))
357 VI.pixel_type = VideoInfo::CS_RGBAP;
358 else if (F->ConvertedPixelFormat == FFMS_GetPixFmt("gray16"))
359 VI.pixel_type = VideoInfo::CS_Y16;
360 else if (F->ConvertedPixelFormat == FFMS_GetPixFmt("gray12"))
361 VI.pixel_type = VideoInfo::CS_Y12;
362 else if (F->ConvertedPixelFormat == FFMS_GetPixFmt("gray10"))
363 VI.pixel_type = VideoInfo::CS_Y10;
364 else
365 Env->ThrowError("FFVideoSource: No suitable output format found");
366
367 // set color information variables
368 Env->SetVar(Env->Sprintf("%s%s", this->VarPrefix, "FFCOLOR_SPACE"), F->ColorSpace);
369 Env->SetVar(Env->Sprintf("%s%s", this->VarPrefix, "FFCOLOR_RANGE"), F->ColorRange);
370
371 if (VP->TopFieldFirst)
372 VI.image_type = VideoInfo::IT_TFF;
373 else
374 VI.image_type = VideoInfo::IT_BFF;
375
376 VI.width = F->ScaledWidth;
377 VI.height = F->ScaledHeight;
378
379 // Crop to obey subsampling width/height requirements
380 VI.width -= VI.width % (1 << GetSubSamplingW(VI));
381 VI.height -= VI.height % (1 << (GetSubSamplingH(VI)));
382 }
383
384 static void BlitPlane(const FFMS_Frame *Frame, PVideoFrame &Dst, IScriptEnvironment *Env, int Plane, int PlaneId) {
385 Env->BitBlt(Dst->GetWritePtr(PlaneId), Dst->GetPitch(PlaneId),
386 Frame->Data[Plane], Frame->Linesize[Plane],
387 Dst->GetRowSize(PlaneId), Dst->GetHeight(PlaneId));
388 }
389
390 void AvisynthVideoSource::OutputFrame(const FFMS_Frame *Frame, PVideoFrame &Dst, IScriptEnvironment *Env) {
391 if (VI.IsPlanar()) {
392 BlitPlane(Frame, Dst, Env, 0, VI.IsRGB() ? PLANAR_G : PLANAR_Y);
393 if (HighBitDepth ? !VI.IsY() : !VI.IsY8()) {
394 BlitPlane(Frame, Dst, Env, 1, VI.IsRGB() ? PLANAR_B : PLANAR_U);
395 BlitPlane(Frame, Dst, Env, 2, VI.IsRGB() ? PLANAR_R : PLANAR_V);
396 }
397 if (VI.IsYUVA() || VI.IsPlanarRGBA())
398 BlitPlane(Frame, Dst, Env, 3, PLANAR_A);
399 } else if (VI.IsYUY2()) {
400 BlitPlane(Frame, Dst, Env, 0, 0);
401 } else if (VI.IsRGB24() || VI.IsRGB32()) {
402 Env->BitBlt(
403 Dst->GetWritePtr() + Dst->GetPitch() * (Dst->GetHeight() - 1), -Dst->GetPitch(),
404 Frame->Data[0], Frame->Linesize[0],
405 Dst->GetRowSize(), Dst->GetHeight());
406 } else {
407 assert(false);
408 }
409 }
410
411 static void BlitField(const FFMS_Frame *Frame, PVideoFrame &Dst, IScriptEnvironment *Env, int Plane, int PlaneId, int Field) {
412 Env->BitBlt(
413 Dst->GetWritePtr(PlaneId) + Dst->GetPitch(PlaneId) * Field, Dst->GetPitch(PlaneId) * 2,
414 Frame->Data[Plane] + Frame->Linesize[Plane] * Field, Frame->Linesize[Plane] * 2,
415 Dst->GetRowSize(PlaneId), Dst->GetHeight(PlaneId) / 2);
416 }
417
418 void AvisynthVideoSource::OutputField(const FFMS_Frame *Frame, PVideoFrame &Dst, int Field, IScriptEnvironment *Env) {
419 const FFMS_Frame *SrcPicture = Frame;
420 if (VI.IsPlanar()) {
421 BlitField(Frame, Dst, Env, 0, VI.IsRGB() ? PLANAR_G : PLANAR_Y, Field);
422 if (HighBitDepth ? !VI.IsY() : !VI.IsY8()) {
423 BlitField(Frame, Dst, Env, 1, VI.IsRGB() ? PLANAR_B : PLANAR_U, Field);
424 BlitField(Frame, Dst, Env, 2, VI.IsRGB() ? PLANAR_R : PLANAR_V, Field);
425 }
426 if (VI.IsYUVA() || VI.IsPlanarRGBA())
427 BlitField(Frame, Dst, Env, 3, PLANAR_A, Field);
428 } else if (VI.IsYUY2()) {
429 BlitField(Frame, Dst, Env, 0, 0, Field);
430 } else if (VI.IsRGB24() || VI.IsRGB32()) {
431 Env->BitBlt(
432 Dst->GetWritePtr() + Dst->GetPitch() * (Dst->GetHeight() - 1 - Field), -Dst->GetPitch() * 2,
433 SrcPicture->Data[0] + SrcPicture->Linesize[0] * Field, SrcPicture->Linesize[0] * 2,
434 Dst->GetRowSize(), Dst->GetHeight() / 2);
435 } else {
436 assert(false);
437 }
438 }
439
440 PVideoFrame AvisynthVideoSource::GetFrame(int n, IScriptEnvironment *Env) {
441 n = std::min(std::max(n, 0), VI.num_frames - 1);
442
443 PVideoFrame Dst = Env->NewVideoFrame(VI);
444 AVSMap *props = has_at_least_v8 ? Env->getFramePropsRW(Dst) : nullptr;
445
446 ErrorInfo E;
447 const FFMS_Frame *Frame = nullptr;
448
449 if (FPSNum > 0 && FPSDen > 0) {
450 double currentTime = FFMS_GetVideoProperties(V)->FirstTime +
451 (double)(n * (int64_t)FPSDen) / FPSNum;
452 Frame = FFMS_GetFrameByTime(V, currentTime, &E);
453 Env->SetVar(Env->Sprintf("%s%s", this->VarPrefix, "FFVFR_TIME"), -1);
454 if (has_at_least_v8) {
455 Env->propSetInt(props, "_DurationNum", FPSDen, 0);
456 Env->propSetInt(props, "_DurationDen", FPSNum, 0);
457 Env->propSetFloat(props, "_AbsoluteTime", currentTime, 0);
458 }
459 } else {
460 Frame = FFMS_GetFrame(V, n, &E);
461 FFMS_Track *T = FFMS_GetTrackFromVideo(V);
462 const FFMS_TrackTimeBase *TB = FFMS_GetTimeBase(T);
463 Env->SetVar(Env->Sprintf("%s%s", this->VarPrefix, "FFVFR_TIME"), static_cast<int>(FFMS_GetFrameInfo(T, n)->PTS * static_cast<double>(TB->Num) / TB->Den));
464 if (has_at_least_v8) {
465 int64_t num;
466 if (n + 1 < VI.num_frames)
467 num = FFMS_GetFrameInfo(T, n + 1)->PTS - FFMS_GetFrameInfo(T, n)->PTS;
468 else if (n > 0) // simply use the second to last frame's duration for the last one, should be good enough
469 num = FFMS_GetFrameInfo(T, n)->PTS - FFMS_GetFrameInfo(T, n - 1)->PTS;
470 else // just make it one timebase if it's a single frame clip
471 num = 1;
472 int64_t DurNum = TB->Num * num;
473 int64_t DurDen = TB->Den * 1000;
474 vsh::reduceRational(&DurNum, &DurDen);
475 Env->propSetInt(props, "_DurationNum", DurNum, 0);
476 Env->propSetInt(props, "_DurationDen", DurDen, 0);
477 Env->propSetFloat(props, "_AbsoluteTime", ((static_cast<double>(TB->Num) / 1000) * FFMS_GetFrameInfo(T, n)->PTS) / TB->Den, 0);
478 }
479 }
480
481 if (Frame == nullptr)
482 Env->ThrowError("FFVideoSource: %s", E.Buffer);
483
484 Env->SetVar(Env->Sprintf("%s%s", this->VarPrefix, "FFPICT_TYPE"), static_cast<int>(Frame->PictType));
485 OutputFrame(Frame, Dst, Env);
486
487 if (has_at_least_v8) {
488 const FFMS_VideoProperties *VP = FFMS_GetVideoProperties(V);
489 if (VP->SARNum > 0 && VP->SARDen > 0) {
490 Env->propSetInt(props, "_SARNum", VP->SARNum, 0);
491 Env->propSetInt(props, "_SARDen", VP->SARDen, 0);
492 }
493
494 Env->propSetInt(props, "_Matrix", Frame->ColorSpace, 0);
495 Env->propSetInt(props, "_Primaries", Frame->ColorPrimaries, 0);
496 Env->propSetInt(props, "_Transfer", Frame->TransferCharateristics, 0);
497 if (Frame->ChromaLocation > 0)
498 Env->propSetInt(props, "_ChromaLocation", Frame->ChromaLocation - 1, 0);
499
500 if (Frame->ColorRange == FFMS_CR_MPEG)
501 Env->propSetInt(props, "_ColorRange", 1, 0);
502 else if (Frame->ColorRange == FFMS_CR_JPEG)
503 Env->propSetInt(props, "_ColorRange", 0, 0);
504 Env->propSetData(props, "_PictType", &Frame->PictType, 1, 0);
505
506 // Set field information
507 int FieldBased = 0;
508 if (Frame->InterlacedFrame)
509 FieldBased = (Frame->TopFieldFirst ? 2 : 1);
510 Env->propSetInt(props, "_FieldBased", FieldBased, 0);
511
512 if (Frame->HasMasteringDisplayPrimaries) {
513 Env->propSetFloatArray(props, "MasteringDisplayPrimariesX", Frame->MasteringDisplayPrimariesX, 3);
514 Env->propSetFloatArray(props, "MasteringDisplayPrimariesY", Frame->MasteringDisplayPrimariesY, 3);
515 Env->propSetFloat(props, "MasteringDisplayWhitePointX", Frame->MasteringDisplayWhitePointX, 0);
516 Env->propSetFloat(props, "MasteringDisplayWhitePointY", Frame->MasteringDisplayWhitePointY, 0);
517 }
518
519 if (Frame->HasMasteringDisplayLuminance) {
520 Env->propSetFloat(props, "MasteringDisplayMinLuminance", Frame->MasteringDisplayMinLuminance, 0);
521 Env->propSetFloat(props, "MasteringDisplayMaxLuminance", Frame->MasteringDisplayMaxLuminance, 0);
522 }
523
524 if (Frame->HasContentLightLevel) {
525 Env->propSetFloat(props, "ContentLightLevelMax", Frame->ContentLightLevelMax, 0);
526 Env->propSetFloat(props, "ContentLightLevelAverage", Frame->ContentLightLevelAverage, 0);
527 }
528
529 if (Frame->DolbyVisionRPU && Frame->DolbyVisionRPUSize > 0) {
530 Env->propSetData(props, "DolbyVisionRPU", reinterpret_cast<const char *>(Frame->DolbyVisionRPU), Frame->DolbyVisionRPUSize, 0);
531 }
532
533 if (Frame->HDR10Plus && Frame->HDR10PlusSize > 0) {
534 Env->propSetData(props, "HDR10Plus", reinterpret_cast<const char *>(Frame->HDR10Plus), Frame->HDR10PlusSize, 0);
535 }
536 }
537
538 return Dst;
539 }
540
541 bool AvisynthVideoSource::GetParity(int n) {
542 return VI.image_type == VideoInfo::IT_TFF;
543 }
544
545 AvisynthAudioSource::AvisynthAudioSource(const char *SourceFile, int Track, FFMS_Index *Index,
546 int AdjustDelay, int FillGaps, double DrcScale, const char *VarPrefix, IScriptEnvironment* Env) {
547 VI = {};
548
549 ErrorInfo E;
550 A = FFMS_CreateAudioSource2(SourceFile, Track, Index, AdjustDelay, FillGaps, DrcScale, &E);
551 if (!A)
552 Env->ThrowError("FFAudioSource: %s", E.Buffer);
553
554 const FFMS_AudioProperties *AP = FFMS_GetAudioProperties(A);
555 VI.nchannels = AP->Channels;
556 VI.num_audio_samples = AP->NumSamples;
557 VI.audio_samples_per_second = AP->SampleRate;
558 VI.SetChannelMask(true, AP->ChannelLayout);
559
560 Env->SetVar(Env->Sprintf("%s%s", VarPrefix, "FFCHANNEL_LAYOUT"), static_cast<int>(AP->ChannelLayout));
561
562 Env->SetGlobalVar("FFVAR_PREFIX", VarPrefix);
563
564 switch (AP->SampleFormat) {
565 case FFMS_FMT_U8: VI.sample_type = SAMPLE_INT8; break;
566 case FFMS_FMT_S16: VI.sample_type = SAMPLE_INT16; break;
567 case FFMS_FMT_S32: VI.sample_type = SAMPLE_INT32; break;
568 case FFMS_FMT_FLT: VI.sample_type = SAMPLE_FLOAT; break;
569 default: Env->ThrowError("FFAudioSource: Bad audio format");
570 }
571 }
572
573 AvisynthAudioSource::~AvisynthAudioSource() {
574 FFMS_DestroyAudioSource(A);
575 }
576
577 void AvisynthAudioSource::GetAudio(void* Buf, int64_t Start, int64_t Count, IScriptEnvironment *Env) {
578 ErrorInfo E;
579 if (FFMS_GetAudio(A, Buf, Start, Count, &E))
580 Env->ThrowError("FFAudioSource: %s", E.Buffer);
581 }