-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathScaleVideo.cs
More file actions
68 lines (58 loc) · 2.7 KB
/
Copy pathScaleVideo.cs
File metadata and controls
68 lines (58 loc) · 2.7 KB
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
using System;
using System.IO;
using FFmpeg.AutoGen;
namespace FFmpeg.Sharp.Example
{
/// <summary>
/// Maps to FFmpeg example: scale_video.c
/// Generate a synthetic 320×240 YUV420P video signal and rescale it to a
/// user-specified size (default 640×480) in RGB24 using libswscale, writing
/// the raw output to a file.
/// </summary>
public unsafe class ScaleVideo : ExampleBase
{
public ScaleVideo() { Index = 14; Enable = false; }
public override void Execute()
{
var outFile = args.Length > 0 ? args[0] : "out_scaled.rgb24";
int dstW = args.Length > 1 ? int.Parse(args[1]) : 640;
int dstH = args.Length > 2 ? int.Parse(args[2]) : 480;
const int srcW = 320, srcH = 240;
const AVPixelFormat srcFmt = AVPixelFormat.AV_PIX_FMT_YUV420P;
const AVPixelFormat dstFmt = AVPixelFormat.AV_PIX_FMT_RGB24;
// Allocate source and destination frames; Swscale handles context creation lazily.
using var srcFrame = MediaFrame.CreateVideoFrame(srcW, srcH, srcFmt);
using var dstFrame = MediaFrame.CreateVideoFrame(dstW, dstH, dstFmt);
using var scaler = new Swscale();
using var outStream = File.OpenWrite(outFile);
for (int i = 0; i < 100; i++)
{
FillYuvImage(srcFrame, srcW, srcH, i);
scaler.Convert(srcFrame, dstFrame);
// Flatten (possibly padded) frame to a tightly-packed buffer for file output.
int dstBufSize = dstFrame.GetBytesSize(padding: false);
var buf = new byte[dstBufSize];
dstFrame.GetBytes(buf, padding: false);
outStream.Write(buf);
}
Console.Error.WriteLine($"Scaling succeeded. Play with:");
Console.Error.WriteLine($"ffplay -f rawvideo -pix_fmt {dstFmt.GetName()} -video_size {dstW}x{dstH} {outFile}");
}
private static void FillYuvImage(MediaFrame frame, int width, int height, int frameIndex)
{
// Y plane.
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
frame.Ref.data[0][y * frame.Ref.linesize[0] + x] = (byte)(x + y + frameIndex * 3);
// Cb and Cr planes.
for (int y = 0; y < height / 2; y++)
{
for (int x = 0; x < width / 2; x++)
{
frame.Ref.data[1][y * frame.Ref.linesize[1] + x] = (byte)(128 + y + frameIndex * 2);
frame.Ref.data[2][y * frame.Ref.linesize[2] + x] = (byte)(64 + x + frameIndex * 5);
}
}
}
}
}