-
Notifications
You must be signed in to change notification settings - Fork 1
/
ThumbnailProvider.m
205 lines (175 loc) · 8 KB
/
ThumbnailProvider.m
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
//
// ThumbnailProvider.m
// VideoThumbnailing
//
// Copyright © 2021 Stephen Salerno. All rights reserved.
//
#import "ThumbnailProvider.h"
#import <UIKit/UIKit.h>
#import <libavcodec/avcodec.h>
#import <libavformat/avformat.h>
#import <libswscale/swscale.h>
#import <libavutil/imgutils.h>
#define THUMBNAIL_POSITION_PERCENT 25
#define THUMBNAIL_ERROR(msg) [[NSError alloc] initWithDomain:@"VideoThumbnailing" code:100 userInfo:@{@"Error": msg}]
@implementation ThumbnailProvider
- (void)provideThumbnailForFileRequest:(QLFileThumbnailRequest *)request completionHandler:(void (^)(QLThumbnailReply * _Nullable, NSError * _Nullable))handler {
@synchronized (self) {
CGSize maxSize = request.maximumSize;
NSString *path = request.fileURL.path;
char *cPath = strdup(path.fileSystemRepresentation);
int ret;
// Open file
AVFormatContext *formatContext = NULL;
ret = avformat_open_input(&formatContext, cPath, NULL, NULL);
free(cPath);
if (ret < 0) {
handler(NULL, THUMBNAIL_ERROR(@"Couldn't open input stream"));
return;
}
// Load media streams
ret = avformat_find_stream_info(formatContext, NULL);
if (ret < 0) {
handler(NULL, THUMBNAIL_ERROR(@"Couldn't get stream info"));
avformat_close_input(&formatContext);
return;
}
// Find the main video stream
AVCodec *decoder = NULL;
int videoStreamId = -1;
videoStreamId = av_find_best_stream(formatContext, AVMEDIA_TYPE_VIDEO, -1, -1, &decoder, 0);
if (videoStreamId < 0) {
handler(NULL, THUMBNAIL_ERROR(@"No video stream found"));
avformat_close_input(&formatContext);
return;
}
AVStream *videoStream = formatContext->streams[videoStreamId];
// Open codec
AVCodecContext *codecContext = avcodec_alloc_context3(decoder);
avcodec_parameters_to_context(codecContext, videoStream->codecpar);
codecContext->time_base = videoStream->time_base;
enum AVPixelFormat pixelFormat = codecContext->pix_fmt;
ret = avcodec_open2(codecContext, decoder, NULL);
if (ret < 0 || pixelFormat == AV_PIX_FMT_NONE) {
handler(NULL, THUMBNAIL_ERROR(@"Couldn't open codec"));
avcodec_free_context(&codecContext);
avformat_close_input(&formatContext);
return;
}
// Allocate frame to decode to
AVFrame *rawFrame = av_frame_alloc();
if (rawFrame == NULL) {
handler(NULL, THUMBNAIL_ERROR(@"Couldn't alloc frame"));
avcodec_free_context(&codecContext);
avformat_close_input(&formatContext);
return;
}
// Seek to the point to capture thumbnail from
int64_t duration = av_rescale_q(formatContext->duration, AV_TIME_BASE_Q, videoStream->time_base);
int64_t seek_pos = (duration * THUMBNAIL_POSITION_PERCENT) / 100 + videoStream->start_time;
avcodec_flush_buffers(codecContext);
ret = av_seek_frame(formatContext, videoStreamId, seek_pos, AVSEEK_FLAG_BACKWARD);
if (ret < 0) {
handler(NULL, THUMBNAIL_ERROR(@"Seek failed"));
avcodec_free_context(&codecContext);
avformat_close_input(&formatContext);
av_frame_free(&rawFrame);
return;
}
avcodec_flush_buffers(codecContext);
// Find the nearest video frame
AVPacket packet;
while(av_read_frame(formatContext, &packet) >= 0) {
if (packet.stream_index == videoStreamId) {
if (avcodec_send_packet(codecContext, &packet) < 0) {
break;
}
ret = avcodec_receive_frame(codecContext, rawFrame);
if (ret < 0 || ret == AVERROR(EAGAIN)) {
av_frame_unref(rawFrame);
av_packet_unref(&packet);
continue;
}
break;
}
av_packet_unref(&packet);
}
avcodec_free_context(&codecContext);
avformat_close_input(&formatContext);
av_packet_unref(&packet);
// Allocate frame for output
__block AVFrame *outputFrame = av_frame_alloc();
if (outputFrame == NULL) {
handler(NULL, THUMBNAIL_ERROR(@"Couldn't alloc output frame"));
avcodec_free_context(&codecContext);
avformat_close_input(&formatContext);
av_frame_free(&rawFrame);
return;
}
int thumbWidth = maxSize.width * request.scale;
int thumbHeight = thumbWidth / ((float)rawFrame->width / rawFrame->height);
outputFrame->width = thumbWidth;
outputFrame->height = thumbHeight;
outputFrame->format = AV_PIX_FMT_RGBA;
int size = av_image_get_buffer_size(outputFrame->format, thumbWidth, thumbHeight, 1);
uint8_t *outputFrameBuffer = (uint8_t *)av_malloc(size);
ret = av_image_fill_arrays(outputFrame->data,
outputFrame->linesize,
outputFrameBuffer,
outputFrame->format,
thumbWidth,
thumbHeight,
1);
if (ret < 0) {
handler(NULL, THUMBNAIL_ERROR(@"Couldn't fill output frame"));
av_frame_free(&rawFrame);
av_frame_free(&outputFrame);
av_free(outputFrameBuffer);
return;
}
// Create sws context for scaling
struct SwsContext *swsContext = sws_getContext(rawFrame->width,
rawFrame->height,
rawFrame->format,
outputFrame->width,
outputFrame->height,
outputFrame->format,
SWS_BILINEAR,
NULL, NULL, NULL);
ret = sws_scale(swsContext,
(const uint8_t * const *)rawFrame->data,
rawFrame->linesize,
0,
rawFrame->height,
outputFrame->data,
outputFrame->linesize);
sws_freeContext(swsContext);
av_frame_free(&rawFrame);
if (ret < 0) {
handler(NULL, THUMBNAIL_ERROR(@"Resize failed"));
av_frame_free(&outputFrame);
av_free(outputFrameBuffer);
return;
}
// Draw to core graphics context
CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = (CGBitmapInfo) kCGImageAlphaPremultipliedLast;
CFDataRef data = CFDataCreateWithBytesNoCopy(kCFAllocatorDefault,
outputFrame->data[0],
outputFrame->linesize[0] * thumbHeight,
kCFAllocatorNull);
__block CGDataProviderRef dataProvider = CGDataProviderCreateWithCFData(data);
__block CGImageRef image = CGImageCreate(thumbWidth, thumbHeight, 8, 32, thumbWidth * 4, rgb, bitmapInfo, dataProvider, NULL, false, kCGRenderingIntentDefault);
QLThumbnailReply *reply = [QLThumbnailReply replyWithContextSize:CGSizeMake(thumbWidth/request.scale, thumbHeight/request.scale) drawingBlock:^BOOL(CGContextRef _Nonnull context) {
CGContextDrawImage(context, CGRectMake(0, 0, thumbWidth, thumbHeight), image);
CGImageRelease(image);
CGDataProviderRelease(dataProvider);
CGColorSpaceRelease(rgb);
av_free(outputFrameBuffer);
av_frame_free(&outputFrame);
return YES;
}];
handler(reply, nil);
}
}
@end