/
SubtitleCodecElement.cpp
executable file
·1030 lines (792 loc) · 25.4 KB
/
SubtitleCodecElement.cpp
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
/*
*
* Copyright (C) 2016 OtherCrashOverride@users.noreply.github.com.
* All rights reserved.
*
* This program 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.
*
* This program 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.
*
*/
#include "SubtitleCodecElement.h"
void SubtitleDecoderElement::static_msg_callback(int level, const char* fmt, va_list va, void *data)
{
//SubtitleDecoderElement* this_ = (SubtitleDecoderElement*)data;
//if (level > 6)
// return;
//#ifdef DEBUG
// char buffer[4096] = { 0 };
// vsprintf(buffer, fmt, va);
// std::string msg = "static_msg_callback: " + std::string(buffer) + "\n";
// printf("%s", msg.c_str());
//#endif // DEBUG
}
void SubtitleDecoderElement::SetupCodec()
{
SubtitleFormatEnum format = inPin->InfoAs()->Format;
switch (format)
{
case SubtitleFormatEnum::SubRip:
avcodec = avcodec_find_decoder(AV_CODEC_ID_SUBRIP);
break;
case SubtitleFormatEnum::Pgs:
avcodec = avcodec_find_decoder(CODEC_ID_HDMV_PGS_SUBTITLE);
break;
case SubtitleFormatEnum::Dvb:
avcodec = avcodec_find_decoder(CODEC_ID_DVB_SUBTITLE);
break;
case SubtitleFormatEnum::DvbTeletext:
avcodec = avcodec_find_decoder(CODEC_ID_DVB_TELETEXT);
break;
case SubtitleFormatEnum::Dvd:
avcodec = avcodec_find_decoder(AV_CODEC_ID_DVD_SUBTITLE);
break;
default:
printf("Subtitle format %d is not supported.\n", (int)format);
throw NotSupportedException();
}
if (!avcodec)
{
throw Exception("codec not found\n");
}
avcodecContext = avcodec_alloc_context3(avcodec);
if (!avcodecContext)
{
throw Exception("avcodec_alloc_context3 failed.\n");
}
// open codec
if (avcodec_open2(avcodecContext, avcodec, NULL) < 0)
{
throw Exception("could not open codec\n");
}
//const char* header = (const char*)avcodecContext->subtitle_header;
//const int header_size = avcodecContext->subtitle_header_size;
//if (header && header > 0)
//{
// ass_process_data(ass_track, (char*)header, header_size);
//}
}
void SubtitleDecoderElement::ProcessBuffer(AVPacketBufferSPTR buffer)
{
AVPacket* pkt = buffer->GetAVPacket();
if (avSubtitle == nullptr)
{
avSubtitle = (AVSubtitle*)calloc(1, sizeof(AVSubtitle));
}
//AVFrame* frame = av_frame_alloc();
//avsubtitle_free (AVSubtitle *sub)
//avcodecContext->pkt_timebase = buffer->TimeBase();
av_codec_set_pkt_timebase(avcodecContext, buffer->TimeBase());
printf("buffer->TimeBase() = num=%u, den=%u\n", buffer->TimeBase().num, buffer->TimeBase().den);
int bytesDecoded = 0;
while (IsExecuting() && bytesDecoded < pkt->size)
{
int got_sub_ptr;
int len = avcodec_decode_subtitle2(avcodecContext,
avSubtitle,
&got_sub_ptr,
pkt);
if (len < 0)
{
// Report the error, but otherwise ignore it.
char errmsg[1024] = { 0 };
av_strerror(len, errmsg, 1024);
Log("Error while decoding: %s\n", errmsg);
break;
}
else
{
bytesDecoded += len;
}
//Log("decoded audio frame OK (len=%x, pkt.size=%x)\n", len, buffer->GetAVPacket()->size);
// Convert audio to ALSA format
if (got_sub_ptr)
{
//typedef struct AVSubtitle {
//3300 uint16_t format; /* 0 = graphics */
//3301 uint32_t start_display_time; /* relative to packet pts, in ms */
//3302 uint32_t end_display_time; /* relative to packet pts, in ms */
//3303 unsigned num_rects;
//3304 AVSubtitleRect **rects;
//3305 int64_t pts; ///< Same as packet pts, in AV_TIME_BASE
//3306
//} AVSubtitle;
double timeStamp = pkt->pts * av_q2d(buffer->TimeBase());
double duration = pkt->duration * av_q2d(buffer->TimeBase());
printf("Subtitle: pts=%f duration=%f\n",
timeStamp, //avSubtitle->pts / (double)AV_TIME_BASE,
duration);
printf("Subtitle: format=%d, start_display_time=%d, end_display_time=%d, num_rects=%d, pts=%" PRId64 "\n",
avSubtitle->format, avSubtitle->start_display_time, avSubtitle->end_display_time, avSubtitle->num_rects, avSubtitle->pts);
if (avSubtitle->num_rects < 1)
{
printf("Subtitle: No Rects\n");
}
bool hasAssa = false;
ImageListSPTR imageList = std::make_shared<ImageList>();
for (unsigned int i = 0; i < avSubtitle->num_rects; i++)
{
AVSubtitleRect* rect = avSubtitle->rects[i];
#if 0 // AVSubtitleRect definition
//typedef struct AVSubtitleRect {
// 3274 int x; ///< top left corner of pict, undefined when pict is not set
// 3275 int y; ///< top left corner of pict, undefined when pict is not set
// 3276 int w; ///< width of pict, undefined when pict is not set
// 3277 int h; ///< height of pict, undefined when pict is not set
// 3278 int nb_colors; ///< number of colors in pict, undefined when pict is not set
// 3279
// 3280 /**
// 3281 * data+linesize for the bitmap of this subtitle.
// 3282 * can be set for text/ass as well once they where rendered
// 3283 */
// 3284 AVPicture pict;
// 3285 enum AVSubtitleType type;
// 3286
// 3287 char *text; ///< 0 terminated plain UTF-8 text
// 3288
// 3289 /**
// 3290 * 0 terminated ASS/SSA compatible event line.
// 3291 * The presentation of this is unaffected by the other values in this
// 3292 * struct.
// 3293 */
// 3294 char *ass;
// 3295
// 3296 int flags;
// 3297
//} AVSubtitleRect;
#endif
printf("Subtitle: Rect - x=%d, y=%d, w=%d, h=%d, nb_colors=%d, text=%p, ass=%p\n",
rect->x, rect->y, rect->w, rect->h, rect->nb_colors, rect->text, rect->ass);
#if 0 //AVPicture definition
//3439 typedef struct AVPicture {
// 3440 uint8_t *data[AV_NUM_DATA_POINTERS]; ///< pointers to the image data planes
// 3441 int linesize[AV_NUM_DATA_POINTERS]; ///< number of bytes per line
// 3442
//} AVPicture;
#endif
//for (int j = 0; j < AV_NUM_DATA_POINTERS; ++j)
//{
// printf("Subtitle: pict: data[%d]=%p linesize=%d\n", j, rect->pict.data[j], rect->pict.linesize[j]);
//}
switch (rect->type)
{
case SUBTITLE_BITMAP:
{
printf("Subtitle:\tBITMAP\n");
AllocatedImageSPTR image = std::make_shared<AllocatedImage>(ImageFormatEnum::R8G8B8A8,
rect->w, rect->h);
unsigned int* imageData = (unsigned int*)image->Data();
unsigned char* pixData = rect->pict.data[0];
unsigned int* paletteData = (unsigned int*)rect->pict.data[1];
for (int y = 0; y < rect->h; ++y)
{
for (int x = 0; x < rect->w; ++x)
{
unsigned char index = *(pixData++);
unsigned int color = paletteData[index];
//color |= 0xff0000ff;
//printf("0x%08x ", color);
imageData[y * rect->w + x] = color;
}
}
printf("\n");
ImageBufferSPTR imageBuffer = std::make_shared<ImageBuffer>(
shared_from_this(),
image);
imageBuffer->SetTimeStamp(pkt->pts * av_q2d(buffer->TimeBase()));
imageBuffer->SetDuration(pkt->duration * av_q2d(buffer->TimeBase()));
imageBuffer->SetX(rect->x);
imageBuffer->SetY(rect->y);
//outPin->SendBuffer(imageBuffer);
imageList->push_back(imageBuffer);
break;
}
case SUBTITLE_TEXT:
printf("Subtitle:\tTEXT: %s\n", rect->text);
break;
case SUBTITLE_ASS:
{
printf("Subtitle:\tSSA/ASSA: %s\n", rect->ass);
//printf("----\n");
//for (int k = 0; k < pkt->size; ++k)
//{
// printf("%c", pkt->data[k]);
//}
//printf("\n----\n");
if (!isSaaHeaderSent)
{
//printf("Subtitle:\tsubtitle_header=%s\n", avcodecContext->subtitle_header); //->subtitle_header_size
const char* header = (const char*)avcodecContext->subtitle_header;
const int header_size = avcodecContext->subtitle_header_size + 1;
if (header && header > 0)
{
//ass_process_data(ass_track, (char*)header, header_size);
ass_process_codec_private(ass_track, (char*)header, header_size);
}
isSaaHeaderSent = true;
}
char *ass_line = rect->ass;
if (!ass_line)
break;
//size_t len = strlen(ass_line);
//ass_process_data(ass_track, ass_line, len);
std::string assString(ass_line);
std::string targetString("0:00:00.00,0:00:00.00");
std::size_t found = assString.find(targetString);
if (found != std::string::npos)
{
printf("replacing bogus timestamp\n");
char buffer[1024];
int s_hrs = (int)(timeStamp / 3600);
int s_min = (int)((timeStamp - (s_hrs * 3600)) / 60);
double s_sec = (timeStamp - s_hrs * 3600 - s_min * 60);
sprintf(buffer, "%d:%02d:%02.2f", s_hrs, s_min, s_sec);
printf("Start Time=%s\n", buffer);
std::string start(buffer);
double endTime = timeStamp + duration;
int e_hrs = (int)(endTime / 3600);
int e_min = (int)((endTime - (e_hrs * 3600)) / 60);
double e_sec = (endTime - e_hrs * 3600 - e_min * 60);
sprintf(buffer, "%d:%02d:%02.2f", e_hrs, e_min, e_sec);
printf("End Time=%s\n", buffer);
std::string end(buffer);
std::string tmp;
tmp.append(assString.substr(0, found + 1));
tmp.append(start);
tmp.append(",");
tmp.append(end);
tmp.append(assString.substr(found + targetString.size()));
printf("New string=%s\n", tmp.c_str());
assString = tmp;
}
//ass_process_chunk
ass_process_data(ass_track, (char*)assString.c_str(), assString.size()); //, // +/- 12
// (long long)(timeStamp * 1000), (long long)(duration * 1000));
hasAssa = true;
//ass_flush_events(ass_track);
//long long now = (long long)((timeStamp + duration / 2) * 1000);
//int changed;
//ASS_Image *img =
// ass_render_frame(ass_renderer, ass_track, now, &changed);
//if (img)
//{
// printf("ass_render_frame OK\n");
#if 0
// //typedef struct ass_image {
// // int w, h; // Bitmap width/height
// // int stride; // Bitmap stride
// // unsigned char *bitmap; // 1bpp stride*h alpha buffer
// // // Note: the last row may not be padded to
// // // bitmap stride!
// // uint32_t color; // Bitmap color and alpha, RGBA
// // int dst_x, dst_y; // Bitmap placement inside the video frame
// // struct ass_image *next; // Next image, or NULL
// // enum {
// // IMAGE_TYPE_CHARACTER,
// // IMAGE_TYPE_OUTLINE,
// // IMAGE_TYPE_SHADOW
// // } type;
// //} ASS_Image;
#endif
// printf("ASS_Image: w=%d, h=%d, stride=%d, bitmap=%p, color=0x%04x, dst_x=%d, dst_y=%d, next=%p, type=%d\n",
// img->w, img->h, img->stride, img->bitmap, img->color, img->dst_x, img->dst_y, img->next, img->type);
//}
break;
}
case SUBTITLE_NONE:
printf("Subtitle:\tNONE\n");
break;
default:
printf("Subtitle:\tUnknown (%d)\n", rect->type);
break;
}
}
if (hasAssa)
{
// * \param now video timestamp in milliseconds
long long now = (long long)((timeStamp + duration / 2) * 1000);
ASS_Image *img = ass_render_frame(ass_renderer, ass_track, now, NULL);
while (img)
{
printf("ass_render_frame OK\n");
//typedef struct ass_image {
// int w, h; // Bitmap width/height
// int stride; // Bitmap stride
// unsigned char *bitmap; // 1bpp stride*h alpha buffer
// // Note: the last row may not be padded to
// // bitmap stride!
// uint32_t color; // Bitmap color and alpha, RGBA
// int dst_x, dst_y; // Bitmap placement inside the video frame
// struct ass_image *next; // Next image, or NULL
// enum {
// IMAGE_TYPE_CHARACTER,
// IMAGE_TYPE_OUTLINE,
// IMAGE_TYPE_SHADOW
// } type;
//} ASS_Image;
printf("ASS_Image: w=%d, h=%d, stride=%d, bitmap=%p, color=0x%08x, dst_x=%d, dst_y=%d, next=%p, type=%d\n",
img->w, img->h, img->stride, img->bitmap, img->color, img->dst_x, img->dst_y, img->next, img->type);
AllocatedImageSPTR image = std::make_shared<AllocatedImage>(ImageFormatEnum::R8G8B8A8,
img->w, img->h);
unsigned int* imageData = (unsigned int*)image->Data();
unsigned char* pixData;
for (int y = 0; y < img->h; ++y)
{
pixData = img->bitmap + (y * img->stride);
for (int x = 0; x < img->w; ++x)
{
unsigned char lum = *(pixData++);
unsigned char red = (img->color >> 24) * lum / 0xff;
unsigned char green = (img->color >> 16) * lum / 0xff;
unsigned char blue = (img->color >> 8) * lum / 0xff;
unsigned char alpha = 0xff - (img->color & 0xff);
alpha = alpha * lum / 0xff;
//unsigned int color = (red << 24) | (green << 16) | (blue << 8) | alpha;
unsigned int color = (alpha << 24) | (blue << 16) | (green << 8) | (red << 0);
//color |= 0xff0000ff;
//printf("0x%08x ", color);
#if 0
//DEBUG
if (color == 0)
{
//color = 0xffff00ff;
//color = 0xffffff00; //ARGB
//color = 0x00ffffff; //BGRA
color = 0xff00ffff; // ABGR
}
#endif
imageData[y * img->w + x] = color;
}
}
ImageBufferSPTR imageBuffer = std::make_shared<ImageBuffer>(
shared_from_this(),
image);
imageBuffer->SetTimeStamp(timeStamp);
imageBuffer->SetDuration(duration);
imageBuffer->SetX(img->dst_x);
imageBuffer->SetY(img->dst_y);
//outPin->SendBuffer(imageBuffer);
imageList->push_back(imageBuffer);
//printf("ASS_Image: Added Image timeStamp=%f/%f duration=%f/%f\n",
// timeStamp, imageBuffer->TimeStamp(),
// duration, imageBuffer->Duration());
img = img->next;
}
}
//if (imageList->size() > 0)
{
ImageListBufferSPTR imageListBuffer = std::make_shared<ImageListBuffer>(
shared_from_this(),
imageList);
imageListBuffer->SetTimeStamp(timeStamp);
outPin->SendBuffer(imageListBuffer);
}
avsubtitle_free(avSubtitle);
}
}
}
SubtitleDecoderElement::SubtitleDecoderElement()
{
ass_library = ass_library_init();
if (!ass_library)
{
throw Exception("ass_library_init failed!\n");
}
ass_set_message_cb(ass_library, SubtitleDecoderElement::static_msg_callback, (void*)this);
ass_renderer = ass_renderer_init(ass_library);
if (!ass_renderer)
{
throw Exception("ass_renderer_init failed!\n");
}
ass_set_use_margins(ass_renderer, false);
ass_set_hinting(ass_renderer, ASS_HINTING_LIGHT);
ass_set_font_scale(ass_renderer, 1.0);
ass_set_line_spacing(ass_renderer, 0.0);
ass_set_frame_size(ass_renderer, 1920, 1080);
ass_set_fonts(ass_renderer, NULL, "sans-serif",
ASS_FONTPROVIDER_AUTODETECT, NULL, 1);
//ass_set_fonts(ass_renderer,
// "/usr/share/kodi/media/Fonts/arial.ttf", "Arial",
// ASS_FONTPROVIDER_AUTODETECT, NULL, 1);
ass_track = ass_new_track(ass_library);
if (!ass_track)
{
throw Exception("ass_new_track failed!\n");
}
//ass_set_check_readorder(ass_track, 0);
}
SubtitleDecoderElement::~SubtitleDecoderElement()
{
}
void SubtitleDecoderElement::Initialize()
{
ClearOutputPins();
ClearInputPins();
// TODO: Pin format negotiation
// Create an input pin
inInfo = std::make_shared<SubtitlePinInfo>();
inInfo->Format = SubtitleFormatEnum::Unknown;
ElementWPTR weakPtr = shared_from_this();
inPin = std::make_shared<SubtitleInPin>(weakPtr, inInfo);
AddInputPin(inPin);
// Create an audio out pin
// TODO: Figure out what the output is
outInfo = std::make_shared<PinInfo>(MediaCategoryEnum::Unknown);
//ElementWPTR weakPtr = shared_from_this();
outPin = std::make_shared<OutPin>(weakPtr, outInfo);
AddOutputPin(outPin);
//// Create buffer(s)
//frame = std::make_shared<AVFrameBuffer>(shared_from_this());
}
void SubtitleDecoderElement::DoWork()
{
BufferSPTR buffer;
BufferSPTR outBuffer;
// Reap output buffers
while (outPin->TryGetAvailableBuffer(&buffer))
{
// New buffers are created as needed so just
// drop this buffer.
Wake();
}
if (inPin->TryGetFilledBuffer(&buffer))
{
if (buffer->Type() != BufferTypeEnum::AVPacket)
{
// The input buffer is not usuable for processing
switch (buffer->Type())
{
case BufferTypeEnum::Marker:
{
MarkerBufferSPTR markerBuffer = std::static_pointer_cast<MarkerBuffer>(buffer);
switch (markerBuffer->Marker())
{
case MarkerEnum::EndOfStream:
// Send all Output Pins an EOS buffer
for (int i = 0; i < Outputs()->Count(); ++i)
{
MarkerBufferSPTR eosBuffer = std::make_shared<MarkerBuffer>(shared_from_this(), MarkerEnum::EndOfStream);
Outputs()->Item(i)->SendBuffer(eosBuffer);
}
//SetExecutionState(ExecutionStateEnum::Idle);
SetState(MediaState::Pause);
break;
default:
// ignore unknown
break;
}
break;
}
default:
// Ignore
break;
}
inPin->PushProcessedBuffer(buffer);
inPin->ReturnProcessedBuffers();
}
else
{
if (isFirstData)
{
OutPinSPTR otherPin = inPin->Source();
if (otherPin)
{
if (otherPin->Info()->Category() != MediaCategoryEnum::Subtitle)
{
throw InvalidOperationException("Not connected to a subtitle pin.");
}
SubtitlePinInfoSPTR info = std::static_pointer_cast<SubtitlePinInfo>(otherPin->Info());
inInfo->Format = info->Format;
// TODO: Copy in pin info
// TODO: Set output info
//outInfo->SampleRate = info->SampleRate;
SetupCodec();
isFirstData = false;
}
}
AVPacketBufferSPTR avPacketBuffer = std::static_pointer_cast<AVPacketBuffer>(buffer);
ProcessBuffer(avPacketBuffer);
inPin->PushProcessedBuffer(buffer);
inPin->ReturnProcessedBuffers();
}
}
}
void SubtitleDecoderElement::ChangeState(MediaState oldState, MediaState newState)
{
Element::ChangeState(oldState, newState);
}
void SubtitleDecoderElement::Flush()
{
Element::Flush();
if (avcodecContext)
{
avcodec_flush_buffers(avcodecContext);
}
}
//-------------------------
void SubtitleRenderElement::timer_Expired(void* sender, const EventArgs& args)
{
if (State() == MediaState::Play)
{
entriesMutex.Lock();
//printf("SubtitleRenderElement::timer_Expired currentTime=%f\n", currentTime);
// Remove stale entries
{
std::vector<SpriteEntry> expiredEntries;
for (SpriteEntry& entry : spriteEntries)
{
if (currentTime >= entry.StopTime)
{
expiredEntries.push_back(entry);
}
}
bool clearAllActiveFlag = false;
for (SpriteEntry& entry : expiredEntries)
{
if (!entry.Sprite)
{
clearAllActiveFlag = true;
break;
}
}
if (clearAllActiveFlag)
{
for (SpriteEntry& entry : spriteEntries)
{
if (entry.IsActive)
{
// Check if entry is already on the
// exired list.
bool found = false;
for (SpriteEntry& existing : spriteEntries)
{
if (existing.Sprite == entry.Sprite)
{
found = true;
break;
}
}
if (!found)
expiredEntries.push_back(entry);
}
}
}
SpriteList removals;
for (SpriteEntry& entry : expiredEntries)
{
if (entry.IsActive && entry.Sprite)
{
removals.push_back(entry.Sprite);
printf("SubtitleRenderElement::timer_Expired - [%f] removal (Sprite=%p)\n",
currentTime, entry.Sprite.get());
}
/*auto iter = std::find(std::begin(spriteEntries), std::end(spriteEntries), entry);
if (iter == spriteEntries.end())
throw InvalidOperationException();*/
for (auto iter = spriteEntries.begin(); iter != spriteEntries.end(); ++iter)
{
if (*iter == entry)
{
spriteEntries.erase(iter);
break;
}
}
}
if (removals.size() > 0)
compositor->RemoveSprites(removals);
}
// Add new entries
{
SpriteList additions;
for (SpriteEntry& entry : spriteEntries)
{
if (currentTime >= entry.StartTime &&
!entry.IsActive)
{
entry.IsActive = true;
if (entry.Sprite)
{
additions.push_back(entry.Sprite);
printf("SubtitleRenderElement::timer_Expired - [%f] addition (Sprite=%p)\n",
currentTime, entry.Sprite.get());
}
//printf("SubtitleRenderElement: Displaying sprite. (StartTime=%f StopTime=%f\n",
// entry.StartTime, entry.StopTime);
}
}
if (additions.size() > 0)
compositor->AddSprites(additions);
}
currentTime += timer.Interval();
entriesMutex.Unlock();
}
}
void SubtitleRenderElement::SetupCodec()
{
}
void SubtitleRenderElement::ProcessBuffer(ImageListBufferSPTR buffer)
{
entriesMutex.Lock();
if (buffer->Payload()->size() < 1)
{
// Empty buffer is used to signal "clear all"
SpriteEntry entry;
entry.StartTime = buffer->TimeStamp();
entry.StopTime = buffer->TimeStamp();
//entry.Sprite = sprite;
entry.IsActive = false;
spriteEntries.push_back(entry);
printf("SubtitleRenderElement::ProcessBuffer - Got empty ImageListBuffer. (TimeStamp = %f)\n", buffer->TimeStamp());
}
else
{
float z = -1;
for (ImageBufferSPTR image : *(buffer->Payload()))
{
SourceSPTR source = std::make_shared<Source>(image->Payload());
SpriteSPTR sprite = std::make_shared<Sprite>(source);
sprite->SetDestinationRect(Rectangle(image->X(), image->Y(), image->Payload()->Width(), image->Payload()->Height()));
sprite->SetColor(PackedColor(0xff, 0xff, 0xff, 0xff));
sprite->SetZOrder(z);
SpriteEntry entry;
entry.StartTime = image->TimeStamp();
if (image->Duration() > 0)
{
entry.StopTime = image->TimeStamp() + image->Duration();
}
else
{
// If a duration is not specified, use a default
entry.StopTime = image->TimeStamp() + 2.0; // seconds
}
entry.Sprite = sprite;
entry.IsActive = false;
spriteEntries.push_back(entry);
z += 0.001;
printf("SubtitleRenderElement::ProcessBuffer - Added (StartTime=%f StopTime=%f Sprite=%p)\n",
entry.StartTime, entry.StopTime, entry.Sprite.get());
}
}
entriesMutex.Unlock();
}
SubtitleRenderElement::SubtitleRenderElement(CompositorSPTR compositor)
: compositor(compositor)
{
if (!compositor)
throw ArgumentNullException();
timerExpiredListener = std::make_shared<EventListener<EventArgs>>(
std::bind(&SubtitleRenderElement::timer_Expired, this, std::placeholders::_1, std::placeholders::_2));
timer.Expired.AddListener(timerExpiredListener);
timer.SetInterval(1.0 / 60.0); // 60fps
timer.Start();
}
SubtitleRenderElement::~SubtitleRenderElement()
{
}
void SubtitleRenderElement::Initialize()
{
ClearOutputPins();
ClearInputPins();
// TODO: Pin format negotiation
// Create an input pin
inInfo = std::make_shared<PinInfo>(MediaCategoryEnum::Picture);
//inInfo->Format = ImageFormatEnum::Unknown;
ElementWPTR weakPtr = shared_from_this();
inPin = std::make_shared<InPin>(weakPtr, inInfo);
AddInputPin(inPin);
}
void SubtitleRenderElement::DoWork()
{
BufferSPTR buffer;
if (inPin->TryGetFilledBuffer(&buffer))
{
if (buffer->Type() != BufferTypeEnum::ImageList)
{
// The input buffer is not usuable for processing
switch (buffer->Type())
{
case BufferTypeEnum::Marker:
{
MarkerBufferSPTR markerBuffer = std::static_pointer_cast<MarkerBuffer>(buffer);
switch (markerBuffer->Marker())
{
case MarkerEnum::EndOfStream:
// Send all Output Pins an EOS buffer
for (int i = 0; i < Outputs()->Count(); ++i)
{
MarkerBufferSPTR eosBuffer = std::make_shared<MarkerBuffer>(shared_from_this(), MarkerEnum::EndOfStream);
Outputs()->Item(i)->SendBuffer(eosBuffer);
}
//SetExecutionState(ExecutionStateEnum::Idle);
//SetState(MediaState::Pause);
break;
default:
// ignore unknown
break;
}
break;
}
default:
// Ignore
break;
}
inPin->PushProcessedBuffer(buffer);
inPin->ReturnProcessedBuffers();
}
else
{
if (isFirstData)
{
OutPinSPTR otherPin = inPin->Source();
if (otherPin)
{
//if (otherPin->Info()->Category() != MediaCategoryEnum::Picture)
//{
// throw InvalidOperationException("Not connected to a picture pin.");
//}
PinInfoSPTR info = otherPin->Info();
//inInfo->Format = info->Format;
// TODO: Copy in pin info
SetupCodec();
isFirstData = false;
}
}
ImageListBufferSPTR imageBuffer = std::static_pointer_cast<ImageListBuffer>(buffer);
ProcessBuffer(imageBuffer);
inPin->PushProcessedBuffer(buffer);
inPin->ReturnProcessedBuffers();
}
}
}
void SubtitleRenderElement::ChangeState(MediaState oldState, MediaState newState)
{
Element::ChangeState(oldState, newState);
}
void SubtitleRenderElement::Flush()
{