-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathvideo_thumb.c
519 lines (439 loc) · 13.7 KB
/
video_thumb.c
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/* MiniDLNA project
*
* http://sourceforge.net/projects/minidlna/
*
* MiniDLNA media server
* Copyright (C) 2008-2009 Justin Maggard
*
* This file is part of MiniDLNA.
*
* MiniDLNA is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* MiniDLNA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MiniDLNA. If not, see <http://www.gnu.org/licenses/>.
*
* Portions of the code from the MiniUPnP project:
*
* Copyright (c) 2006-2007, Thomas Bernard
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include <unistd.h>
#include <libgen.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef ENABLE_VIDEO_THUMB
#if HAVE_FFMPEG_LIBSWSCALE_SWSCALE_H
#include <ffmpeg/libswscale/swscale.h>
#elif HAVE_LIBAV_LIBSWSCALE_SWSCALE_H
#include <libav/libswscale/swscale.h>
#elif HAVE_LIBSWSCALE_SWSCALE_H
#include <libswscale/swscale.h>
#elif HAVE_FFMPEG_SWSCALE_H
#include <ffmpeg/swscale.h>
#elif HAVE_LIBAV_SWSCALE_H
#include <libav/swscale.h>
#elif HAVE_SWSCALE_H
#include <swscale.h>
#endif
#endif
#include "upnpglobalvars.h"
#include "image_utils.h"
#include "libav.h"
#include "log.h"
#include "video_thumb.h"
#include "utils.h"
#ifdef ENABLE_VIDEO_THUMB
static int
get_video_packet(AVFormatContext *ctx, AVCodecContext *vctx,
AVPacket *pkt, AVFrame *frame, int vstream)
{
int moreframes, decoded, finished;
int avret, i, j, l;
av_free_packet(pkt);
finished = 0;
for (l = 0; !finished && l < 500; l++)
{
moreframes = 1;
decoded = 0;
for (i = 0; moreframes && ! decoded && i < 2000; i++)
{
avret = av_read_frame(ctx, pkt);
if ((moreframes = (avret >= 0)))
{
if (!(decoded = (pkt->stream_index == vstream)))
{
av_free_packet(pkt);
continue;
}
for (j = 0; !finished && j < 50; j++)
{
lav_frame_unref(frame);
avret = lav_avcodec_decode_video(vctx, frame, &finished, pkt);
if (avret < 0)
return 0;
}
}
}
}
return (finished > 0);
}
static int
video_seek(int seconds, AVFormatContext *ctx, AVCodecContext *vctx,
AVPacket *pkt, AVFrame *frame, int vstream)
{
int64_t tstamp = AV_TIME_BASE * (int64_t) seconds;
if (tstamp < 0)
tstamp = 0;
if ((av_seek_frame(ctx, -1, tstamp, 0) >=0))
avcodec_flush_buffers(ctx->streams[vstream]->codec);
return get_video_packet(ctx, vctx, pkt, frame, vstream);
}
#endif
int
video_thumb_generate_tofile(const char *moviefname, const char *thumbfname, int seek, int width)
{
#ifdef ENABLE_VIDEO_THUMB
image_s img;
int ret = 0;
clock_t start, end;
start = clock();
memset(&img, 0, sizeof(image_s));
if ((video_thumb_generate_tobuff(moviefname, &img, seek, width, PIX_FMT_RGB32_1) < 0))
{
DPRINTF(E_WARN, L_METADATA, "video_thumb_generate_tofile: unable to generate thumbnail to buffer! \n");
return -1;
}
if (!image_save_to_jpeg_file(&img, (char*) thumbfname))
{
DPRINTF(E_WARN, L_METADATA, "video_thumb_generate_tofile: unable to save jpeg file : %s \n", thumbfname);
ret = -1;
}
free(img.buf);
end = clock();
DPRINTF(E_DEBUG, L_METADATA, "Generated thumbnail for (%s) in %ldms.\n", moviefname, (end-start) * 1000 / CLOCKS_PER_SEC);
return ret;
#else
return -1;
#endif
}
#ifdef ENABLE_VIDEO_THUMB
static int
video_thumb_generate_ctx_tobuff(AVFormatContext *fctx, void* imgbuffer, int seek, int width, enum AVPixelFormat pixfmt)
{
AVFrame *frame = NULL, *scframe = NULL;
AVPacket packet;
AVCodecContext *vcctx = NULL;
AVCodec *vcodec = NULL;
struct SwsContext *scctx = NULL;
int avret, i, vs, ret = -1;
int dwidth, dheight;
image_s* buffer = (image_s*) imgbuffer;
if (!fctx)
return -1;
if (!buffer)
return -1;
if (seek < 0)
seek = 0;
else if (seek > 90)
seek = 90;
if (width < 64)
width = 64;
else if (width > 480)
width = 480;
av_init_packet(&packet);
vs = -1;
for (i =0; i<fctx->nb_streams; i++)
{
if (fctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
{
vs = i;
vcctx = fctx->streams[vs]->codec;
break;
}
}
if( vs < 0)
{
DPRINTF(E_WARN, L_METADATA, "video_thumb_generate_tobuff: unable to find a video stream \n");
return ret;
}
vcodec = avcodec_find_decoder(vcctx->codec_id);
if (!vcodec)
{
DPRINTF(E_WARN, L_METADATA, "video_thumb_generate_tobuff: unable to find a video decoder \n");
goto thumb_generate_error;
}
vcctx->workaround_bugs = 1;
avret = lav_avcodec_open(vcctx, vcodec, NULL);
if(avret < 0)
{
DPRINTF(E_WARN, L_METADATA, "video_thumb_generate_tobuff: unable to open a decoder \n");
goto thumb_generate_error;
}
frame = lav_frame_alloc();
if (!frame)
{
DPRINTF(E_WARN, L_METADATA, "video_thumb_generate_tobuff: malloc error!! Unable to alloc memory for a new frame \n");
goto thumb_generate_error;
}
if (!video_seek(seek*(fctx->duration/AV_TIME_BASE)/100,
fctx, vcctx, &packet, frame, vs))
{
DPRINTF(E_WARN, L_METADATA, "video_thumb_generate_tobuff: unable to seek video for %d%% position \n", seek);
goto thumb_generate_error;
}
if (frame->interlaced_frame)
{
DPRINTF(E_DEBUG, L_METADATA, "video_thumb_generate_tobuff: got an interlaced video \n");
avpicture_deinterlace((AVPicture*) frame, (AVPicture*) frame,
vcctx->pix_fmt, vcctx->width, vcctx->height);
}
dwidth = width;
dheight = (int) ((float) (width * vcctx->height) / vcctx->width );
scframe = lav_frame_alloc();
if (!scframe)
{
DPRINTF(E_WARN, L_METADATA, "video_thumb_generate_tobuff: malloc error!! Unable to alloc memory for the scaled frame \n");
goto thumb_generate_error;
}
avret = avpicture_alloc((AVPicture*)scframe, pixfmt, dwidth, dheight);
if (avret < 0)
{
DPRINTF(E_WARN, L_METADATA, "video_thumb_generate_tobuff: malloc error!! Unable to alloc memory for the scaled frame buffer \n");
goto thumb_generate_error;
}
scctx = sws_getCachedContext(scctx, vcctx->width, vcctx->height, vcctx->pix_fmt,
dwidth, dheight, pixfmt, SWS_BICUBIC, NULL, NULL, NULL);
if (!scctx)
{
DPRINTF(E_WARN, L_METADATA, "video_thumb_generate_tobuff: Unable to get a scale context! \n");
goto thumb_generate_error;
}
avret = sws_scale(scctx, (const uint8_t * const*)frame->data, frame->linesize, 0, vcctx->height,
scframe->data, scframe->linesize);
if (avret <= 0)
{
DPRINTF(E_WARN, L_METADATA, "video_thumb_generate_tobuff: Unable to scale thumbnail! \n");
goto thumb_generate_error;
}
free(buffer->buf);
buffer->buf = (pix*) malloc(sizeof(uint8_t) * scframe->linesize[0] * dheight);
if(!buffer->buf)
{
DPRINTF(E_WARN, L_METADATA, "video_thumb_generate_tobuff: malloc error!! Unable to alloc memory to buffer the picture \n");
goto thumb_generate_error;
}
buffer->width = dwidth;
buffer->height = dheight;
memcpy(buffer->buf, scframe->data[0], scframe->linesize[0] * buffer->height);
ret = 0;
thumb_generate_error:
sws_freeContext(scctx);
avpicture_free((struct AVPicture*)scframe);
av_free(scframe);
av_free_packet(&packet);
av_free(frame);
avcodec_close(vcctx);
return ret;
}
#endif
int
video_thumb_generate_tobuff(const char *moviefname, void* imgbuffer, int seek, int width, enum AVPixelFormat pixfmt)
{
#ifdef ENABLE_VIDEO_THUMB
AVFormatContext *fctx = NULL;
int ret = -1;
if(!moviefname)
return -1;
if (lav_open(&fctx, moviefname))
{
DPRINTF(E_WARN, L_METADATA, "video_thumb_generate_tobuff: unable to open movie file (%s) \n", moviefname);
return -1;
}
ret = video_thumb_generate_ctx_tobuff(fctx, imgbuffer, seek, width, pixfmt);
lav_close(fctx);
return ret;
#else
return -1;
#endif
}
#define MTA_WIDTH 96
char*
video_thumb_generate_mta_file(const char *moviefname, int duration, int allblack)
{
static const char mta_header[] =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
"<SEC:SECMeta xsi:schemaLocation=\"urn:samsung:metadata:2009 Video_Metadata_v1.0.xsd\""
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:SEC=\"urn:samsung:metadata:2009\">\r\n"
"<SpecVersion>1.0</SpecVersion>\r\n"
"<MediaInformation>\r\n"
"<VideoLocator>\r\n"
"<MediaUri>file://samsung_content.con</MediaUri>\r\n"
"</VideoLocator>\r\n"
"</MediaInformation>\r\n"
"<ContentInformation>\r\n"
"<Chaptering>\r\n";
static const char mta_tail[] =
"</Chaptering>\r\n"
"</ContentInformation>\r\n"
"</SEC:SECMeta>\r\n";
static const char mta_chapter_header[] =
"<ChapterSegment>\r\n"
"<KeyFrame>\r\n"
"<InlineMedia>";
static const char mta_chapter_tail[] =
"</InlineMedia>\r\n"
"</KeyFrame>\r\n"
"<MediaPosition>\r\n"
"<MediaTime timePoint=\"%d\"/>\r\n"
"</MediaPosition>\r\n"
"</ChapterSegment>\r\n";
/* 128x72 Black JPEG base 64 encoded */
static const char bjpg64[] =
"/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQECAgMCAgICAgQDAwIDBQQFBQUEBAQFB"
"gcGBQUHBgQEBgkGBwgICAgIBQYJCgkICgcICAj/2wBDAQEBAQICAgQCAgQIBQQFCAgICAgICAgICAgICAg"
"ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAj/wAARCABIAIADASIAAhEBAxEB/8QAHwAAA"
"QUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1F"
"hByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZ"
"WZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NX"
"W19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAt"
"REAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8Rc"
"YGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXm"
"JmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAM"
"BAAIRAxEAPwD/AD/6KKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKK"
"KKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKK"
"ACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKAP//Z";
#ifdef ENABLE_VIDEO_THUMB
AVFormatContext *fctx = NULL;
unsigned char *jpeg;
int jpegsize;
#endif
image_s img;
FILE *fp = NULL;
char *mta_path, *mta_dir;
char *img64 = NULL;
size_t sizei64;
int i, res, ret = -1;
int per[] = { 15, 35, 50, 65, 85 };
clock_t start, end;
if (!moviefname)
return 0;
if (duration <= 0)
return 0;
if (!is_video(moviefname))
return 0;
memset(&img, 0, sizeof(image_s));
start = clock();
res = xasprintf(&mta_path, "%s/art_cache%s.mta", db_path, moviefname);
if ( res < 0 )
return 0;
if ( !access(mta_path, F_OK) )
{
DPRINTF(E_INFO, L_METADATA, "The MTA file (%s) already exists.", mta_path);
return mta_path;
}
res = xasprintf(&mta_dir, "%s/art_cache%s.mta", db_path, moviefname);
if ( res < 0 )
{
mta_dir = NULL;
goto mta_error;
}
if ( make_dir(dirname(mta_dir), S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) )
goto mta_error;
if ( !(fp = fopen(mta_path, "w")) )
goto mta_error;
#ifdef ENABLE_VIDEO_THUMB
if(!allblack)
lav_open(&fctx, moviefname);
#endif
res = fwrite(mta_header, 1, sizeof(mta_header) -1, fp);
if(res != sizeof(mta_header) - 1)
goto mta_error;
for (i = 0; i < sizeof(per)/sizeof(int); i++)
{
res = fwrite(mta_chapter_header, 1, sizeof(mta_chapter_header) - 1, fp);
if(res != sizeof(mta_chapter_header) - 1)
goto mta_error;
#ifdef ENABLE_VIDEO_THUMB
if ( !allblack && fctx)
{
res = video_thumb_generate_ctx_tobuff(fctx, &img, per[i], MTA_WIDTH, PIX_FMT_RGB32_1);
if (img.buf)
{
jpeg = image_save_to_jpeg_buf(&img, &jpegsize);
if(jpeg)
{
img64 = base64_encode(jpeg, jpegsize, &sizei64);
free(jpeg);
jpeg = NULL;
}
}
}
#endif
if (!allblack && img64)
{
res = fwrite(img64, 1, sizei64, fp);
free(img64);
if(res != sizei64)
goto mta_error;
}
else
{
res = fwrite(bjpg64, 1, sizeof(bjpg64) -1, fp);
if(res != sizeof(bjpg64) -1)
goto mta_error;
}
fprintf(fp, mta_chapter_tail, (duration * per[i])/100);
}
res = fwrite(mta_tail, 1, sizeof(mta_tail) - 1, fp);
if( res != sizeof(mta_tail) -1 )
goto mta_error;
ret = 0;
end = clock();
DPRINTF(E_DEBUG, L_METADATA, "Generated MTA file for (%s) in %ldms.\n", moviefname, (end-start) * 1000 / CLOCKS_PER_SEC);
mta_error:
free(img.buf);
#ifdef ENABLE_VIDEO_THUMB
if (fctx)
lav_close(fctx);
#endif
fclose(fp);
free(mta_dir);
if (ret)
{
remove(mta_path);
free(mta_path);
mta_path = NULL;
}
return mta_path;
}