-
Notifications
You must be signed in to change notification settings - Fork 1
/
mov2avif.mm
479 lines (404 loc) · 16.5 KB
/
mov2avif.mm
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
// Dump the first frame of a mov file.
// clang++ mov2avif.mm -framework AVFoundation -framework QuartzCore -framework CoreMedia -framework VideoToolbox -framework Cocoa -lavif -O2 && ./a.out test.mov
#include <Cocoa/Cocoa.h>
#include <AVFoundation/AVFoundation.h>
#include <CoreFoundation/CoreFoundation.h>
#include <CoreGraphics/CoreGraphics.h>
#include <CoreVideo/CoreVideo.h>
#include <VideoToolbox/VTDecompressionSession.h>
#include <map>
#include <deque>
#include <stdio.h>
#include "avif/avif.h"
#define CHECK(x) \
do { \
if (!(x)) { \
fprintf(stderr, "Failed: '%s' at %s:%d\n", #x, __FILE__, __LINE__); \
exit(1); \
} \
} while (0)
CALayer* background_layer = nil;
VTDecompressionSessionRef vt_decompression_session = 0;
AVSampleBufferDisplayLayer* sample_display_layer = nil;
typedef std::map<CFAbsoluteTime, CVImageBufferRef> TimeToFrameMap;
TimeToFrameMap decoded_images;
std::deque<CVImageBufferRef> displayed_images;
uint8_t avif_irot_angle = 0;
uint8_t avif_imir_mode = 0;
void CGAffineTransformToAVIF(CGAffineTransform t, uint8_t* irot_angle, uint8_t* imir_mode) {
*imir_mode = 0;
if (t.a == 1 && t.b == 0 && t.c == 0 && t.d == 1) {
*irot_angle = 0;
} else if (t.a == 0 && t.b == 1 && t.c == -1 && t.d == 0) {
*irot_angle = 3;
} else if (t.a == -1 && t.b == 0 && t.c == 0 && t.d == -1) {
*irot_angle = 2;
} else if (t.a == 0 && t.b == -1 && t.c == 1 && t.d == -1) {
*irot_angle = 1;
} else {
printf("Failed to convert CGAFffineTransform\n");
}
}
void DumpPixelBuffer(CVPixelBufferRef pixel_buffer) {
CVPixelBufferLockBaseAddress(pixel_buffer, kCVPixelBufferLock_ReadOnly);
int width = CVPixelBufferGetWidth(pixel_buffer);
int height = CVPixelBufferGetHeight(pixel_buffer);
int returnCode = 1;
avifRWData avifOutput = AVIF_DATA_EMPTY;
avifImage * image = avifImageCreate(width, height, 10, AVIF_PIXEL_FORMAT_YUV420);
image->colorPrimaries = AVIF_COLOR_PRIMARIES_BT2020;
image->transferCharacteristics = AVIF_TRANSFER_CHARACTERISTICS_HLG;
image->matrixCoefficients = AVIF_MATRIX_COEFFICIENTS_BT2020_NCL;
image->transformFlags = AVIF_TRANSFORM_IROT;
image->irot.angle = avif_irot_angle;
image->yuvRange = AVIF_RANGE_LIMITED;
avifImageAllocatePlanes(image, AVIF_PLANES_YUV);
{
int plane_width = image->width;
int plane_height = image->height;
uint8_t* in_y = (uint8_t*)CVPixelBufferGetBaseAddressOfPlane(pixel_buffer, 0);
size_t in_y_stride = CVPixelBufferGetBytesPerRowOfPlane(pixel_buffer, 0);
CHECK(plane_width == CVPixelBufferGetWidthOfPlane(pixel_buffer, 0));
CHECK(plane_height == CVPixelBufferGetHeightOfPlane(pixel_buffer, 0));
for (int y = 0; y < plane_height; ++y) {
uint16_t* in_y_row = (uint16_t*)(in_y + y*in_y_stride);
uint16_t* out_y_row = (uint16_t*)(image->yuvPlanes[AVIF_CHAN_Y] + y*image->yuvRowBytes[AVIF_CHAN_Y]);
for (int x = 0; x < plane_width; ++x) {
out_y_row[x] = in_y_row[x] >> 6;
}
}
}
{
uint8_t* in_uv = (uint8_t*)CVPixelBufferGetBaseAddressOfPlane(pixel_buffer, 1);
size_t in_uv_stride = CVPixelBufferGetBytesPerRowOfPlane(pixel_buffer, 1);
int plane_width = image->width / 2;
int plane_height = image->height / 2;
for (int y = 0; y < plane_height; ++y) {
uint16_t* in_uv_row = (uint16_t*)(in_uv + y*in_uv_stride);
uint16_t* out_u_row = (uint16_t*)(image->yuvPlanes[AVIF_CHAN_U] + y*image->yuvRowBytes[AVIF_CHAN_U]);
uint16_t* out_v_row = (uint16_t*)(image->yuvPlanes[AVIF_CHAN_V] + y*image->yuvRowBytes[AVIF_CHAN_V]);
for (int x = 0; x < plane_width; ++x) {
out_u_row[x] = in_uv_row[2*x] >> 6;
out_v_row[x] = in_uv_row[2*x+1] >> 6;
}
}
}
avifEncoder * encoder = NULL;
encoder = avifEncoderCreate();
encoder->maxThreads = 8;
// encoder->speed = 10;
printf("Going to encode...\n");
// Call avifEncoderAddImage() for each image in your sequence
// Only set AVIF_ADD_IMAGE_FLAG_SINGLE if you're not encoding a sequence
// Use avifEncoderAddImageGrid() instead with an array of avifImage* to make a grid image
avifResult addImageResult = avifEncoderAddImage(encoder, image, 1, AVIF_ADD_IMAGE_FLAG_SINGLE);
if (addImageResult != AVIF_RESULT_OK) {
fprintf(stderr, "Failed to add image to encoder: %s\n", avifResultToString(addImageResult));
exit(1);
}
printf("Finishing encode\n");
avifResult finishResult = avifEncoderFinish(encoder, &avifOutput);
if (finishResult != AVIF_RESULT_OK) {
fprintf(stderr, "Failed to finish encode: %s\n", avifResultToString(finishResult));
exit(1);
}
printf("Encode success: %zu total bytes\n", avifOutput.size);
char filename[1024];
static int counter = 0;
sprintf(filename, "out%05d.avif", counter);
counter += 1;
FILE * f = fopen(filename, "wb");
size_t bytesWritten = fwrite(avifOutput.data, 1, avifOutput.size, f);
fclose(f);
if (bytesWritten != avifOutput.size) {
fprintf(stderr, "Failed to write %zu bytes\n", avifOutput.size);
exit(1);
}
printf("Wrote %s\n", filename);
if (image) {
avifImageDestroy(image);
}
if (encoder) {
avifEncoderDestroy(encoder);
}
avifRWDataFree(&avifOutput);
CVPixelBufferUnlockBaseAddress(pixel_buffer, kCVPixelBufferLock_ReadOnly);
exit(0);
}
// Read an entire mp4 file in filename into cm_sample_buffers_from_asset_reader.
std::deque<CMSampleBufferRef> cm_sample_buffers_from_asset_reader;
void ReadFileFromDisk(const char* filename) {
AVAssetReaderOutput* asset_reader_output = nil;
AVAsset* asset = nil;
AVAssetTrack* video_track = nil;
AVAssetReader* asset_reader = nil;
NSURL* url = [NSURL fileURLWithPath:[[NSString alloc]
initWithUTF8String:filename]];
asset = [AVAsset assetWithURL:url];
video_track = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
asset_reader_output = [[AVAssetReaderTrackOutput alloc]
initWithTrack:video_track outputSettings:nil];
NSError* error = nil;
asset_reader = [AVAssetReader assetReaderWithAsset:asset error:&error];
CHECK(!error);
[asset_reader addOutput:asset_reader_output];
[asset_reader startReading];
printf("getting orientation\n");
fflush(stdout);
CGAffineTransformToAVIF([video_track preferredTransform],
&avif_irot_angle,
&avif_imir_mode);
printf("angle:%d, mode:%d\n", avif_irot_angle, avif_imir_mode);
printf("Reading the entire stream from disk...\n");
while (1) {
CMSampleBufferRef cm_sample_buffer =
[asset_reader_output copyNextSampleBuffer];
if (!cm_sample_buffer)
break;
// CFShow(cm_sample_buffer);
cm_sample_buffers_from_asset_reader.push_back(cm_sample_buffer);
}
printf("Done.\n");
[asset_reader cancelReading];
[asset_reader_output release];
[asset_reader release];
[asset release];
}
// Initialize the CALayer, which will have its contents set to each frame.
void InitializeLayer() {
[sample_display_layer removeFromSuperlayer];
[sample_display_layer release];
sample_display_layer = nil;
sample_display_layer = [[AVSampleBufferDisplayLayer alloc] init];
[sample_display_layer setBackgroundColor:CGColorGetConstantColor(kCGColorBlack)];
[background_layer addSublayer:sample_display_layer];
[sample_display_layer setFrame:CGRectMake(0, 0, 1240, 690)];
}
static void DecompressionSessionOutputCallback(
void* decompression_output_refcon,
void* source_frame_refcon,
OSStatus status,
VTDecodeInfoFlags info_flags,
CVImageBufferRef image_buffer,
CMTime presentation_time_stamp,
CMTime presentation_duration) {
CHECK(image_buffer);
CHECK(!status);
CHECK(CFGetTypeID(image_buffer) == CVPixelBufferGetTypeID());
CFRetain(image_buffer);
CFAbsoluteTime key_time = CMTimeGetSeconds(presentation_time_stamp);
if (decoded_images[key_time])
CFRelease(decoded_images[key_time]);
decoded_images[key_time] = image_buffer;
}
// This will re-allocate a VTDecompressionSession that is capable of decoding
// |cm_sample_buffer|, if needed.
void PrepareDecompressionSessionForCMSampleBuffer(
CMSampleBufferRef cm_sample_buffer) {
CHECK(cm_sample_buffer);
// Retrieve the CMVideoFormatDescription.
CMVideoFormatDescriptionRef cm_video_format_description =
CMSampleBufferGetFormatDescription(cm_sample_buffer);
CHECK(cm_video_format_description);
// If we already have initialized the VTDecompressionSession, and it can
// accept this |cm_sample_buffer|, we're done.
if (vt_decompression_session) {
if (VTDecompressionSessionCanAcceptFormatDescription(
vt_decompression_session, cm_video_format_description)) {
return;
}
printf("Creating a new VTDecompressionSession\n");
VTDecompressionSessionWaitForAsynchronousFrames(vt_decompression_session);
CFRelease(vt_decompression_session);
vt_decompression_session = 0;
} else {
printf("Creating first VTDecompressionSession\n");
}
// Construct the decoder configuration.
CFMutableDictionaryRef decoder_parameters = CFDictionaryCreateMutable(
kCFAllocatorDefault,
0,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
CHECK(decoder_parameters);
{
CFDictionarySetValue(decoder_parameters,
kVTVideoDecoderSpecification_RequireHardwareAcceleratedVideoDecoder,
kCFBooleanTrue);
}
// Construct the output pixel buffer attributes.
// This doen'st help.
CFMutableDictionaryRef pixel_buffer_attributes = CFDictionaryCreateMutable(
kCFAllocatorDefault,
0,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
CHECK(pixel_buffer_attributes);
{
// Retrieve the video dimensions (for the output pixel buffer attributes).
CMVideoDimensions cm_video_dimensions =
CMVideoFormatDescriptionGetDimensions(cm_video_format_description);
// None of these seem to make any difference.
CFDictionarySetValue(pixel_buffer_attributes,
kCVPixelBufferWidthKey,
CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &cm_video_dimensions.width));
CFDictionarySetValue(pixel_buffer_attributes,
kCVPixelBufferHeightKey,
CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &cm_video_dimensions.height));
// This makes a big difference. Without it we get some &xvo format that... boh.
int32_t pixel_format = kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange;
CFDictionarySetValue(
pixel_buffer_attributes,
kCVPixelBufferPixelFormatTypeKey,
CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &pixel_format));
// Also doesn't seem to matter.
CFDictionarySetValue(pixel_buffer_attributes,
kCVPixelBufferIOSurfaceCoreAnimationCompatibilityKey, kCFBooleanTrue);
}
// Configure the frame-is-decoded callback.
VTDecompressionOutputCallbackRecord vt_decompression_callback_record;
{
vt_decompression_callback_record.decompressionOutputCallback =
DecompressionSessionOutputCallback;
vt_decompression_callback_record.decompressionOutputRefCon = 0;
}
// Allocate the VTDecompressionSession.
OSStatus decompression_session_create_status = VTDecompressionSessionCreate(
kCFAllocatorDefault,
cm_video_format_description,
decoder_parameters,
pixel_buffer_attributes,
&vt_decompression_callback_record,
&vt_decompression_session);
if (decompression_session_create_status) {
printf("Failed VTDecompressionSessionCreate ... this is usually because hardware\n");
printf("acceleration wasn't present ... sometimes quitting Chrome makes it re-appear.\n");
}
CHECK(!decompression_session_create_status);
CFRelease(decoder_parameters);
CFRelease(pixel_buffer_attributes);
}
void DecodeNextFrame() {
// Loop through the compressed samples ad infinitum.
CMSampleBufferRef cm_sample_buffer = cm_sample_buffers_from_asset_reader.front();
cm_sample_buffers_from_asset_reader.pop_front();
cm_sample_buffers_from_asset_reader.push_back(cm_sample_buffer);
CHECK(cm_sample_buffer);
// Pull the video format description from the sample buffer.
CMVideoFormatDescriptionRef cm_video_format_description =
CMSampleBufferGetFormatDescription(cm_sample_buffer);
if (!cm_video_format_description)
return DecodeNextFrame();
// Ensure that we have a compatible VTDecompressionSession.
PrepareDecompressionSessionForCMSampleBuffer(cm_sample_buffer);
// Decode the frame. Use synchronous decode so that we don't have to think
// about locking the various structures.
VTDecodeFrameFlags decode_flags = 0; // kVTDecodeFrame_EnableAsynchronousDecompression;
void* source_frame_ref_con = 0;
VTDecodeInfoFlags info_flags_out;
OSStatus decompression_session_decode_frame_status =
VTDecompressionSessionDecodeFrame(
vt_decompression_session,
cm_sample_buffer,
decode_flags,
source_frame_ref_con,
&info_flags_out);
CHECK(!decompression_session_decode_frame_status);
}
IOSurfaceRef g_io_surface = 0;
void DisplayNextDecodedFrame(CVPixelBufferRef cv_pixel_buffer) {
printf("DisplayNextDecodedFrame\n");
CFShow(cv_pixel_buffer);
DumpPixelBuffer(cv_pixel_buffer);
CHECK(cv_pixel_buffer);
OSStatus status;
// Create the CMVideoFormatDescription.
CMVideoFormatDescriptionRef video_info = NULL;
status = CMVideoFormatDescriptionCreateForImageBuffer(NULL, cv_pixel_buffer, &video_info);
CHECK(!status);
CHECK(video_info);
// Create the CMSampleTimingInfo.
CMSampleTimingInfo timing = {kCMTimeInvalid, kCMTimeInvalid, kCMTimeInvalid};
// Create the CMSampleBuffer.
CMSampleBufferRef sample_buffer = nullptr;
status = CMSampleBufferCreateForImageBuffer(kCFAllocatorDefault, cv_pixel_buffer, YES, NULL, NULL, video_info, &timing, &sample_buffer);
CHECK(!status);
CHECK(sample_buffer);
// Set attachments on the CMSampleBuffer.
CFArrayRef attachments = CMSampleBufferGetSampleAttachmentsArray(sample_buffer, YES);
CHECK(attachments);
CFMutableDictionaryRef dict = (CFMutableDictionaryRef)CFArrayGetValueAtIndex(attachments, 0);
CHECK(dict);
CFDictionarySetValue(dict, kCMSampleAttachmentKey_DisplayImmediately, kCFBooleanTrue);
[sample_display_layer flush];
[sample_display_layer enqueueSampleBuffer:sample_buffer];
CFRelease(sample_buffer);
CFRelease(video_info);
}
@interface MainWindow : NSWindow
- (void)tick;
@end
@implementation MainWindow
- (void)keyDown:(NSEvent *)event {
if ([event isARepeat]) return;
NSString *characters = [event charactersIgnoringModifiers];
if ([characters length] != 1) return;
switch ([characters characterAtIndex:0]) {
case 'q': [NSApp terminate:nil]; break;
}
}
- (BOOL)canBecomeKeyWindow { return YES; }
- (BOOL)canBecomeMainWindow { return YES; }
- (void)tick {
[self performSelector:@selector(tick) withObject:nil afterDelay:0.01];
// Decode 8 frames at a time to give some hope to getting them in order.
while (decoded_images.size() < 8)
DecodeNextFrame();
/*
// Don't stuff the layer.
if (![sample_display_layer isReadyForMoreMediaData]) {
printf("Not ready for more media\n");
return;
}
*/
// Draw the frame with the first timestamp.
TimeToFrameMap::iterator map_iter = decoded_images.begin();
CVPixelBufferRef cv_pixel_buffer = map_iter->second;
decoded_images.erase(map_iter);
DisplayNextDecodedFrame(cv_pixel_buffer);
CFRelease(cv_pixel_buffer);
// The decoded images list should never grow beyond 8.
CHECK(decoded_images.size() < 8);
}
@end
int main(int argc, char* argv[]) {
if (argc != 2) {
printf("Usage: %s file_to_play.mp4\n", argv[0]);
return 1;
}
[NSApplication sharedApplication];
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
NSMenu* menubar = [NSMenu alloc];
[NSApp setMainMenu:menubar];
ReadFileFromDisk(argv[1]);
MainWindow* window = [[MainWindow alloc]
initWithContentRect:NSMakeRect(0, 0, 1240, 690)
styleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskResizable
backing:NSBackingStoreBuffered
defer:NO];
[window setOpaque:YES];
[window setBackgroundColor:[NSColor blackColor]];
[window setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary];
NSView* view = [window contentView];
background_layer = [[CALayer alloc] init];
[view setLayer:background_layer];
[view setWantsLayer:YES];
InitializeLayer();
[window setTitle:@"VTDecompressionSession AVSampleBufferDisplayLayer test"];
[window makeKeyAndOrderFront:nil];
// Start the window going.
[window tick];
[NSApp activateIgnoringOtherApps:YES];
[NSApp run];
return 0;
}