-
Notifications
You must be signed in to change notification settings - Fork 1
/
anipaper.c
1802 lines (1580 loc) · 40.5 KB
/
anipaper.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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* MIT License
*
* Copyright (c) 2021-2022 Davidson Francis <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdio.h>
#include <signal.h>
#include <getopt.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/common.h>
#include <libavutil/imgutils.h>
#include <libavutil/mathematics.h>
#include <libavfilter/avfilter.h>
#include <libswscale/swscale.h>
#include <X11/Xlib.h>
#include "anipaper.h"
/* Queues size. */
#define MAX_PACKET_QUEUE 128
#define MAX_PICTURE_QUEUE 8
/*
* Multiple decode parameters, used during the decoding
* and playing process.
*/
struct av_decode_params dp = {0};
/* Termination flags. */
int should_quit = 0;
int end_pkts = 0;
int end_pics = 0;
/*
* Since AVPacketList is marked as 'deprecated', let's
* use ours.
*/
struct packet_list
{
AVPacket pkt;
struct packet_list *next;
};
/* Packet queue definition. */
struct packet_queue
{
struct packet_list *first_packet;
struct packet_list *last_packet;
int npkts;
int size;
SDL_mutex *mutex;
SDL_cond *cond;
} packet_queue;
/* Picture list definition. */
struct picture_list
{
double pts;
SDL_Texture *picture;
struct picture_list *next;
};
/* Picture queue definition. */
struct picture_queue
{
struct picture_list *first_picture;
struct picture_list *last_picture;
int npics;
SDL_mutex *mutex;
SDL_cond *cond;
} picture_queue;
/* SDL global variables. */
static Display *x11dip;
static SDL_Window *window;
static SDL_Renderer *renderer;
static SDL_mutex *screen_mutex;
static SDL_Thread *enqueue_thread;
static SDL_Thread *decode_thread;
static SDL_Thread *pause_thread;
/* SDL Events. */
static int SDL_EVENT_REFRESH_SCREEN;
/* CMD Flags/parameters. */
#define CMD_BACKGROUND 1 /* As wallpaper background. */
#define CMD_LOOP 2
#define CMD_WINDOWED 4
#define CMD_RESOLUTION_KEEP 8
#define CMD_RESOLUTION_SCALE 16
#define CMD_RESOLUTION_FIT 32
#define CMD_HW_ACCEL 64
#define CMD_BORDERLESS 128
#define CMD_PAUSE_SIGNAL 256
static int cmd_flags = CMD_BACKGROUND | CMD_LOOP | CMD_RESOLUTION_FIT;
static char device_type[16];
static int should_pause;
/**
* @brief Initialize the packet queue.
*
* @param q Packet queue.
*
* @return Returns 0 if sucess, -1 otherwise.
*/
static int init_packet_queue(struct packet_queue *q)
{
memset(q, 0, sizeof(*q));
q->mutex = SDL_CreateMutex();
q->cond = SDL_CreateCond();
if (!q->mutex || !q->cond)
LOG_GOTO("Unable to create SDL mutexes/cond!\n", out);
return (0);
out:
return (-1);
}
/**
* @brief Releases all resources related to the packet
* queue, including the elements itself.
*
* @param q Packet queue to be freed.
*/
static void finish_packet_queue(struct packet_queue *q)
{
struct packet_list *pkl;
struct packet_list *pkl_next;
if (!q)
return;
if (!q->mutex)
SDL_DestroyMutex(q->mutex);
if (!q->cond)
SDL_DestroyCond(q->cond);
/* Go through the queue and clear everything. */
pkl = q->first_packet;
while (pkl)
{
pkl_next = pkl->next;
av_packet_unref(&pkl->pkt);
av_free(pkl);
pkl = pkl_next;
}
}
/**
* @brief Add a new packet @p src_pkt to the queue.
*
* It is important to note that this routine is blocking and if
* there are no space left, the thread remains in blocking state
* until there are room available.
*
* @param q Packet queue.
* @param src_pkt Packet to be added.
*
* @return Returns 1 if success, -1 otherwise.
*/
static int packet_queue_put(struct packet_queue *q, AVPacket *src_pkt)
{
struct packet_list *pkl;
pkl = av_malloc(sizeof(*pkl));
if (!pkl)
return (-1);
/* Fill our new node. */
pkl->pkt = *src_pkt;
pkl->next = NULL;
/* Add to our list. */
SDL_LockMutex(q->mutex);
while (1)
{
if (should_quit)
break;
/* Sleep until a new space or if we should quit. */
if (q->npkts == MAX_PACKET_QUEUE)
{
SDL_CondWait(q->cond, q->mutex);
continue;
}
if (!q->last_packet)
q->first_packet = pkl;
else
q->last_packet->next = pkl;
q->last_packet = pkl;
q->npkts++;
q->size += src_pkt->size;
SDL_CondSignal(q->cond);
break;
}
SDL_UnlockMutex(q->mutex);
return (1);
}
/**
* @brief Removes a packet from the queue and returns it
* as @p pk.
*
* It is important to note that this routine is blocking and if
* there are no new packets, the thread remains in blocking state
* until there are new packets.
*
* @param q Packet queue.
* @param pk Returned packet.
*
* @return Returns 1 if success, -1 otherwise.
*/
static int packet_queue_get(struct packet_queue *q, AVPacket *pk)
{
int ret;
struct packet_list *pkl;
ret = -1;
SDL_LockMutex(q->mutex);
while (1)
{
/* Should we abort? */
if (should_quit || (end_pkts && !q->npkts))
break;
pkl = q->first_packet;
/* If empty, lets wait for something. */
if (!pkl)
{
SDL_CondWait(q->cond, q->mutex);
continue;
}
/* If something, remove head node and return. */
q->first_packet = pkl->next;
if (!q->first_packet)
q->last_packet = NULL;
q->npkts--;
q->size -= pkl->pkt.size;
*pk = pkl->pkt;
/* Release our node. */
av_free(pkl);
ret = 1;
break;
}
SDL_CondSignal(q->cond);
SDL_UnlockMutex(q->mutex);
return (ret);
}
/**
* @brief Initialize the picture queue.
*
* @param q Picture queue.
*
* @return Returns 0 if sucess, -1 otherwise.
*/
static int init_picture_queue(struct picture_queue *q)
{
memset(q, 0, sizeof(*q));
q->mutex = SDL_CreateMutex();
q->cond = SDL_CreateCond();
if (!q->mutex || !q->cond)
LOG_GOTO("Unable to create SDL mutexes/cond in picture_queue!\n",
out);
return (0);
out:
return (-1);
}
/**
* @brief Releases all resources related to the picture
* queue, including the elements itself.
*
* @param q Picture queue to be freed.
*/
static void finish_picture_queue(struct picture_queue *q)
{
struct picture_list *pl;
struct picture_list *pl_next;
if (!q)
return;
if (!q->mutex)
SDL_DestroyMutex(q->mutex);
if (!q->cond)
SDL_DestroyCond(q->cond);
/* Go through the queue and clear everything. */
pl = q->first_picture;
while (pl)
{
pl_next = pl->next;
SDL_DestroyTexture(pl->picture);
av_free(pl);
pl = pl_next;
}
}
/**
* @brief Add a complete frame @p src_frm to the queue.
*
* It is important to note that this routine is blocking and if
* there are no space left, the thread remains in blocking state
* until there are room available.
*
* @param dp av_decode_params structure.
* @param q Picture queue.
* @param src_frm Frame to be added.
*
* @return Returns 1 if success, -1 otherwise.
*/
static int picture_queue_put(struct av_decode_params *dp,
struct picture_queue *q, AVFrame *src_frm)
{
int ret;
struct picture_list *pl;
SDL_Texture *picture;
ret = -1;
/* Allocate a new node and put in the list. */
pl = av_malloc(sizeof(*pl));
if (!pl)
return (-1);
/*
* Create a SDL_Texture.
*
* Yes... I'm assuming all frames will be YUV420...
* maybe emmit an error or abort if the case.
*
* TODO: Handle non-YUV frames.
*/
SDL_LockMutex(screen_mutex);
picture = SDL_CreateTexture(renderer,
SDL_PIXELFORMAT_YV12,
SDL_TEXTUREACCESS_STREAMING,
dp->codec_context->width, dp->codec_context->height);
if (!picture)
{
SDL_UnlockMutex(screen_mutex);
av_free(pl);
return (-1);
}
/* Fill our new node with a new SDL_Surface. */
SDL_UpdateYUVTexture(picture, NULL,
src_frm->data[0], src_frm->linesize[0],
src_frm->data[1], src_frm->linesize[1],
src_frm->data[2], src_frm->linesize[2]);
SDL_UnlockMutex(screen_mutex);
pl->pts = (double)src_frm->best_effort_timestamp * dp->time_base;
pl->picture = picture;
pl->next = NULL;
/* Free frame buffers. */
av_frame_unref(src_frm);
/* Add to our list. */
SDL_LockMutex(q->mutex);
while (1)
{
if (should_quit)
{
ret = -1;
break;
}
/* Sleep until a new space or if we should quit. */
if (q->npics == MAX_PICTURE_QUEUE)
{
SDL_CondWait(q->cond, q->mutex);
continue;
}
if (!q->last_picture)
q->first_picture = pl;
else
q->last_picture->next = pl;
q->last_picture = pl;
ret = 1;
q->npics++;
SDL_CondSignal(q->cond);
break;
}
SDL_UnlockMutex(q->mutex);
return (ret);
}
/**
* @brief Removes a full frame from the queue and returns it
* as @p sdl_pic and @p pts.
*
* It is important to note that this routine is blocking and if
* there are no new frames, the thread remains in blocking until
* there are new frames.
*
* @param q Picture queue.
* @param sdl_pic Returned frame to be drawn.
* @param pts Returned frame pts.
*
* @return Returns 1 if success, -1 otherwise.
*/
static int picture_queue_get(struct picture_queue *q, SDL_Texture **sdl_pic,
double *pts)
{
int ret;
struct picture_list *pl;
ret = -1;
SDL_LockMutex(q->mutex);
while (1)
{
if (should_quit || (end_pics && !q->npics))
break;
pl = q->first_picture;
/* If empty, lets wait for something. */
if (!pl)
{
SDL_CondWait(q->cond, q->mutex);
continue;
}
/* If something, remove head node and return. */
q->first_picture = pl->next;
if (!q->first_picture)
q->last_picture = NULL;
q->npics--;
*sdl_pic = pl->picture;
*pts = pl->pts;
/* Release our node. */
av_free(pl);
ret = 1;
break;
}
SDL_CondSignal(q->cond);
SDL_UnlockMutex(q->mutex);
return (ret);
}
/**
* @brief Screen refresh timer callback, this function triggers
* a refresh screen event, where the main thread must refresh the
* screen.
*
* @param interval Interval time (in ms).
* @param data av_decode_params structure.
*/
static Uint32 refresh_screen_callback(Uint32 interval, void *data)
{
((void)interval);
SDL_Event event;
event.type = SDL_EVENT_REFRESH_SCREEN;
event.user.data1 = data;
SDL_PushEvent(&event);
return (0);
}
/**
* @brief Schedule a new time for the main thread to sleep.
*
* @param dp av_decode_params structure.
* @param delay Delay (in ms).
*/
static void schedule_refresh(struct av_decode_params *dp, int delay)
{
SDL_AddTimer(delay, refresh_screen_callback, dp);
}
/**
* @brief Draws a new frame on the screen, taking
* command line parameters into account.
*
* @param texture_frame Frame to be drawn.
* @param dp av_decode_params structure.
*/
static void draw_frame(SDL_Texture *texture_frame,
struct av_decode_params *dp)
{
SDL_Rect dst = {0};
SDL_Rect *dst_ptr;
double w_ratio;
double h_ratio;
double b_ratio;
dst_ptr = NULL;
/* Update screen. */
SDL_QueryTexture(texture_frame, NULL, NULL, &dst.w, &dst.h);
/* Adjust sizes. */
if (cmd_flags & CMD_RESOLUTION_FIT)
{
if (dp->screen_width && dp->screen_height)
{
dst_ptr = &dst;
w_ratio = (double)dp->screen_width / (double)dst.w;
h_ratio = (double)dp->screen_height / (double)dst.h;
b_ratio = fmin(w_ratio, h_ratio);
dst.w = (double)dst.w * b_ratio;
dst.h = (double)dst.h * b_ratio;
dst.x = dp->screen_width / 2 - dst.w / 2;
dst.y = dp->screen_height / 2 - dst.h / 2;
}
}
else if (cmd_flags & CMD_RESOLUTION_SCALE)
{
if (cmd_flags & CMD_WINDOWED)
{
if (dp->screen_width && dp->screen_height)
{
dst.w = dp->screen_width;
dst.h = dp->screen_height;
dst.x = dp->screen_width / 2 - dst.w / 2;
dst.y = dp->screen_height / 2 - dst.h / 2;
}
}
}
/* CMD_RESOLUTION_KEEP. */
else
{
if (!(cmd_flags & CMD_WINDOWED))
{
if (dp->screen_width && dp->screen_height)
{
dst_ptr = &dst;
dst.x = dp->screen_width / 2 - dst.w / 2;
dst.y = dp->screen_height / 2 - dst.h / 2;
}
}
}
SDL_LockMutex(screen_mutex);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture_frame, NULL, dst_ptr);
SDL_RenderPresent(renderer);
SDL_UnlockMutex(screen_mutex);
}
/**
* @brief Adjust the current video timers.
*
* This routine is the 'magic' point that synchronizes the video:
* the times of each frame are adjusted (according to the pts, and
* the current time), and the new sleep time is calculated. If
* the program is too late, the frame is discarded and a new
* frame is read, this is repeated as many times as necessary.
*
* @param pts Presentation Time Stamp for the current frame.
* @param dp av_decode_params structure.
*
* @return Returns the amount of time the thread should sleep.
*/
static double adjust_timers(double pts, struct av_decode_params *dp)
{
double delay;
double true_delay;
delay = pts - dp->frame_last_pts;
/*
* If delay is negative: pts no set
* If greater than 1.0: too big
*/
if (delay <= 0 || delay >= 1.0)
delay = dp->frame_last_delay;
/* Backup our values. */
dp->frame_last_delay = delay;
dp->frame_last_pts = pts;
/*
* Calculate our true delay:
*
* Since we may be behind or ahead of the next frame's wait
* time, we need to calculate the real delay, using the
* program's current execution time.
*
* If we are too late, we ignore the frame.
*/
dp->frame_timer += delay;
true_delay = dp->frame_timer - time_secs();
return (true_delay);
}
/**
* @brief Changes or keeps execution mode accordingly with the
* current mode and @p should_pause parameter.
*
* @param dp av_decode_params structure.
* @param should_pause non-zero if should pause, 0 otherwise.
*/
static void change_execution(struct av_decode_params *dp, int should_pause)
{
SDL_LockMutex(dp->pause_mutex);
if (should_pause)
{
if (!dp->paused)
dp->time_before_pause = time_secs();
else
goto out;
}
/* Resume. */
else
{
if (dp->paused)
dp->frame_timer += (time_secs() - dp->time_before_pause);
else
goto out;
}
dp->paused = !dp->paused;
SDL_CondSignal(dp->pause_cond);
out:
SDL_UnlockMutex(dp->pause_mutex);
}
/**
* @brief Checks at fixed interval if the total area
* of the non-minimized windows is greater than
* some threshold, if so, pause the Anipaper execution,
* otherwise, resume.
*
* This executes in another thread.
*
* @param arg av_decode_params structure.
*
* @return Always returns 0.
*/
static int pause_execution_thread(void *data)
{
int sp;
int s_area;
struct av_decode_params *dp;
dp = (struct av_decode_params *)data;
while (1)
{
if (should_quit)
break;
sp = should_pause;
if (!sp && (cmd_flags & CMD_BACKGROUND))
{
s_area = screen_area_used(x11dip, dp->screen_width,
dp->screen_height);
if (s_area > SCREEN_AREA_THRESHOLD)
sp = 1;
}
/* Changes or keeps execution mode. */
change_execution(dp, sp);
/* Check again in CHECK_PAUSE_MS (100ms, by default). */
SDL_Delay(CHECK_PAUSE_MS);
}
return (0);
}
/**
* @brief Updates the screen periodically, until
* there is no more data to be processed.
*
* @param data av_decode_params structure.
*/
static void refresh_screen(void *data)
{
int ret;
SDL_Event event;
struct av_decode_params *dp;
SDL_Texture *texture_frame;
double true_delay;
double pts;
dp = (struct av_decode_params *)data;
texture_frame = NULL;
again:
if (!(cmd_flags & (CMD_PAUSE_SIGNAL|CMD_BACKGROUND)))
goto not_pause;
SDL_LockMutex(dp->pause_mutex);
check_pause:
if (dp->paused && !should_quit)
{
/*
* Wait up to 40ms, as we need to check if there is a
* possible SDL_QUIT event to be handled, otherwise,
* the program would not be terminated until restarted
* from pause.
*/
SDL_CondWaitTimeout(dp->pause_cond, dp->pause_mutex, 40);
/* Check if there is a SDL_QUIT event. */
SDL_PumpEvents();
ret = SDL_PeepEvents(&event, 1, SDL_PEEKEVENT, SDL_QUIT,
SDL_QUIT);
if (!ret)
goto check_pause;
else
return;
}
SDL_UnlockMutex(dp->pause_mutex);
not_pause:
/*
* If error, do nothing.
*
* It is worth to note that the routine below
* is blocking, meaning that we will wait until
* get a valid frame to update.
*
* Although seems better to polling, (IMO) it is better
* to just wait instead of sleeping/waking up
* every time, wasting resources. The timer should
* always be adjusted anyway.
*/
if (picture_queue_get(&picture_queue, &texture_frame, &pts) < 0)
{
/*
* If everything is over, send an event to myself signaling
* to quit.
*/
event.type = SDL_QUIT;
SDL_PushEvent(&event);
return;
}
/* === Adjust timers === */
true_delay = adjust_timers(pts, dp);
/* If less than 10ms, skip the frame and read the next. */
if (true_delay < 0.010)
{
SDL_DestroyTexture(texture_frame);
goto again;
}
/* Update screen. */
draw_frame(texture_frame, dp);
/* Release resources. */
SDL_DestroyTexture(texture_frame);
/*
* Set our new timer, with the adjusted delay.
*
* Since SDL_AddTimer is accurate to milliseconds, we
* need to convert first and round the result.
*/
schedule_refresh(dp, (int)((true_delay * 1000) + 0.5));
}
/**
* @brief Given a @p packet, a @p frame pointer and a
* @p dp decode context, decode the packet and saves
* the resulting frame in the picture queue.
*
* @param packet Packet to be decoded.
* @param frame Destination frame.
* @param dp av_decode_params structure.
*
* @return Returns 0 if success, -1 otherwise.
*/
static int decode_packet(AVPacket *packet,
AVFrame *src_frame, AVFrame *dst_frame,
struct av_decode_params *dp)
{
int ret;
AVFrame *frame;
/* Send packet data as input to a decoder. */
ret = avcodec_send_packet(dp->codec_context, packet);
if (ret < 0)
LOG_GOTO("Error while sending packet data to a decoder!\n", out);
while (ret >= 0)
{
/* Get decoded output (i.e: frame) from the decoder. */
ret = avcodec_receive_frame(dp->codec_context, src_frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
break;
else if (ret < 0)
LOG_GOTO("Error while getting a frame from the decoder!\n", out);
/* Check if our frame is CPU or GPU. */
if ((cmd_flags & CMD_HW_ACCEL) &&
src_frame->format == dp->hw_pix_fmt)
{
/* GPU, receive data from GPU to CPU and convert. */
dst_frame->format = AV_PIX_FMT_YUV420P;
ret = av_hwframe_transfer_data(dst_frame, src_frame, 0);
if (ret < 0)
{
av_frame_unref(src_frame);
LOG_GOTO("Error while transfering GPU frame to CPU\n", out);
}
/*
* Looks like av_hwframe_transfer_data() does not copy other
* data from the frame besides the buffer, so we need to
* copy the PTS manually.
*/
dst_frame->pts = src_frame->pts;
dst_frame->best_effort_timestamp = src_frame->best_effort_timestamp;
frame = dst_frame;
/* unref src (GPU frame, since we already use it). */
av_frame_unref(src_frame);
}
/* CPU. */
else
frame = src_frame;
#ifndef DECODE_TO_FILE
/* We have the complete frame, enqueue it */
if (picture_queue_put(dp, &picture_queue, frame) < 0)
{
ret = -1;
goto out;
}
#else
save_frame_ppm(frame, dp);
#endif
}
ret = 0;
out:
return (ret);
}
/**
* @brief Read each packet from the packet queue,
* decode them, and save the resulting frame
* in the picture queue.
*
* This executes in another thread.
*
* @param arg av_decode_params structure.
*
* @return Always returns 0.
*/
static int decode_packets_thread(void *arg)
{
AVPacket packet;
AVFrame *sw_frame;
AVFrame *hw_frame;
struct av_decode_params *dp;
dp = (struct av_decode_params *)arg;
sw_frame = av_frame_alloc();
if (!sw_frame)
LOG_GOTO("Unable to allocate a SW AVFrame!\n", out0);
if (cmd_flags & CMD_HW_ACCEL)
{
hw_frame = av_frame_alloc();
if (!hw_frame)
LOG_GOTO("Unable to allocate a HW AVFrame!\n", out1);
}
else
hw_frame = NULL;
while (1)
{
/* Should quit?. */
if (packet_queue_get(&packet_queue, &packet) < 0)
{
/* Signal the end of pictures and wake up threads. */
end_pics = 1;
SDL_CondSignal(picture_queue.cond);
break;
}
if (decode_packet(&packet, sw_frame, hw_frame, dp) < 0)
break;
av_packet_unref(&packet);
}
av_frame_free(&hw_frame);
out1:
av_frame_free(&sw_frame);
out0:
return (0);
}
/**
* @brief Read each video packet from the video and
* enqueue them for later processing.
*
* This executes in another thread.
*
* @param arg av_decode_params structure.
*
* @return Always returns 0.
*/
static int enqueue_packets_thread(void *arg)
{
AVFrame *frame;
AVPacket *packet;
struct av_decode_params *dp;
dp = (struct av_decode_params *)arg;
/* Allocate memory for AVFrame and AVPacket. */
frame = av_frame_alloc();
if (!frame)
LOG_GOTO("Unable to allocate an AVFrame!\n", out2);
packet = av_packet_alloc();
if (!packet)
LOG_GOTO("Unable to allocate an AVPacket!\n", out3);
start:
while (1)
{
if (should_quit)
goto out2;
/* Error/EOF. */
if (av_read_frame(dp->format_context, packet) < 0)
{
/* Signal the end of packets and wake up threads. */
end_pkts = 1;
SDL_CondSignal(packet_queue.cond);
break;
}
/* Check packet type and enqueue it. */
if (packet->stream_index == dp->video_idx)
packet_queue_put(&packet_queue, packet);
else
av_packet_unref(packet);
}
/* Loop again. */
if (cmd_flags & CMD_LOOP)
{
av_seek_frame(dp->format_context, dp->video_idx, 0,
AVSEEK_FLAG_BACKWARD);
goto start;
}
out2:
av_packet_free(&packet);
out3:
av_frame_free(&frame);
return (0);
}