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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
|
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswresample/swresample.h>
#include <libavutil/time.h>
#include <libavutil/pixfmt.h>
#include <libswscale/swscale.h>
#include <SDL.h>
#include <SDL_thread.h>
#if defined(__arm__) && !(__MACOS__ || __IPHONEOS__)
#define USE_MEMALIGN
#include <malloc.h>
#endif
/* The output audio sample rate. */
static int audio_sample_rate = 44100;
static int audio_sample_increase = 44100 / 5;
static int audio_target_samples = 44100 * 2;
const int CHANNELS = 2;
const int BPC = 2; // Bytes per channel.
const int BPS = 4; // Bytes per sample.
const int FRAMES = 3;
// The number of pixels on each side. This has to be greater that 0 (since
// Ren'Py needs some padding), FRAME_PADDING * BPS has to be a multiple of
// 16 (alignment issues on ARM NEON), and has to match the crop in the
// read_video function of renpysound.pyx.
const int FRAME_PADDING = 4;
const int SPEED = 1;
static SDL_Surface *rgb_surface = NULL;
static SDL_Surface *rgba_surface = NULL;
// http://dranger.com/ffmpeg/
/*******************************************************************************
* SDL_RWops <-> AVIOContext
* */
static int rwops_read(void *opaque, uint8_t *buf, int buf_size) {
SDL_RWops *rw = (SDL_RWops *) opaque;
int rv = rw->read(rw, buf, 1, buf_size);
return rv;
}
static int rwops_write(void *opaque, uint8_t *buf, int buf_size) {
printf("Writing to an SDL_rwops is a really bad idea.\n");
return -1;
}
static int64_t rwops_seek(void *opaque, int64_t offset, int whence) {
SDL_RWops *rw = (SDL_RWops *) opaque;
if (whence == AVSEEK_SIZE) {
return rw->size(rw);
}
// Ignore flags like AVSEEK_FORCE.
whence &= (SEEK_SET | SEEK_CUR | SEEK_END);
int64_t rv = rw->seek(rw, (int) offset, whence);
return rv;
}
#define RWOPS_BUFFER 65536
static AVIOContext *rwops_open(SDL_RWops *rw) {
unsigned char *buffer = av_malloc(RWOPS_BUFFER);
AVIOContext *rv = avio_alloc_context(
buffer,
RWOPS_BUFFER,
0,
rw,
rwops_read,
rwops_write,
rwops_seek);
return rv;
}
static void rwops_close(SDL_RWops *rw) {
rw->close(rw);
}
static double current_time = 0;
typedef struct PacketQueue {
AVPacketList *first;
AVPacketList *last;
} PacketQueue;
typedef struct FrameQueue {
AVFrame *first;
AVFrame *last;
} FrameQueue;
typedef struct SurfaceQueueEntry {
struct SurfaceQueueEntry *next;
SDL_Surface *surf;
/* The pts, converted to seconds. */
double pts;
/* The format. This is not refcounted, but it's kept alive by being
* the format of one of the sampel surfaces.
*/
SDL_PixelFormat *format;
/* As with SDL_Surface. */
int w, h, pitch;
void *pixels;
} SurfaceQueueEntry;
typedef struct MediaState {
/* The condition and lock. */
SDL_cond* cond;
SDL_mutex* lock;
SDL_RWops *rwops;
char *filename;
/*
* True if we this stream should have video.
*/
int want_video;
/*
* This becomes true when the decode thread starts, when
* it is the decode thread's job to deallocate this object.
*/
int started;
/* This becomes true once the decode thread has finished initializing
* and the readers and writers can do their thing.
*/
int ready; // Lock.
/* This is set to true when data has been read, in order to ask the
* decode thread to produce more data.
*/
int needs_decode; // Lock.
/*
* This is set to true when data has been read, in order to ask the
* decode thread to shut down and deallocate all resources.
*/
int quit; // Lock
/* The number of seconds to skip at the start. */
double skip;
/* These become true when the audio and video finish. */
int audio_finished;
int video_finished;
/* Indexes of video and audio streams. */
int video_stream;
int audio_stream;
/* The main context. */
AVFormatContext *ctx;
/* Contexts for decoding audio and video streams. */
AVCodecContext *video_context;
AVCodecContext *audio_context;
/* Queues of packets going to the audio and video
* streams.
*/
PacketQueue video_packet_queue;
PacketQueue audio_packet_queue;
/* The total duration of the video. Only used for information purposes. */
double total_duration;
/* Audio Stuff ***********************************************************/
/* The queue of converted audio frames. */
FrameQueue audio_queue; // Lock
/* The size of the audio queue, and the target size in seconds. */
int audio_queue_samples;
int audio_queue_target_samples;
/* A frame used for decoding. */
AVFrame *audio_decode_frame;
/* The audio frame being read from, and the index into the audio frame. */
AVFrame *audio_out_frame; // Lock
int audio_out_index; // Lock
SwrContext *swr;
/* The duration of the audio stream, in samples.
* -1 means to play until we run out of data.
*/
int audio_duration;
/* The number of samples that have been read so far. */
int audio_read_samples; // Lock
/* A frame that video is decoded into. */
AVFrame *video_decode_frame;
/* The video packet we're decoding, and the partial packet. */
AVPacket video_pkt;
AVPacket video_pkt_tmp;
/* Video Stuff ***********************************************************/
/* Software rescaling context. */
struct SwsContext *sws;
/* A queue of decoded video frames. */
SurfaceQueueEntry *surface_queue; // Lock
int surface_queue_size; // Lock
/* The offset between a pts timestamp and realtime. */
double video_pts_offset;
/* The wall time the last video frame was read. */
double video_read_time;
/* Are frame drops allowed? */
int frame_drops;
} MediaState;
static AVFrame *dequeue_frame(FrameQueue *fq);
static void free_packet_queue(PacketQueue *pq);
static SurfaceQueueEntry *dequeue_surface(SurfaceQueueEntry **queue);
static void deallocate(MediaState *ms) {
/* Destroy video stuff. */
while (1) {
SurfaceQueueEntry *sqe = dequeue_surface(&ms->surface_queue);
if (! sqe) {
break;
}
SDL_free(sqe->pixels);
av_free(sqe);
}
sws_freeContext(ms->sws);
av_frame_free(&ms->video_decode_frame);
av_packet_unref(&ms->video_pkt);
/* Destroy audio stuff. */
swr_free(&ms->swr);
av_frame_free(&ms->audio_decode_frame);
av_frame_free(&ms->audio_out_frame);
while (1) {
AVFrame *f = dequeue_frame(&ms->audio_queue);
if (!f) {
break;
}
av_frame_free(&f);
}
/* Destroy/Close core stuff. */
free_packet_queue(&ms->audio_packet_queue);
free_packet_queue(&ms->video_packet_queue);
avcodec_free_context(&ms->video_context);
avcodec_free_context(&ms->audio_context);
if (ms->ctx) {
for (int i = 0; i < ms->ctx->nb_streams; i++) {
avcodec_close(ms->ctx->streams[i]->codec);
}
}
if (ms->ctx->pb) {
av_freep(&ms->ctx->pb->buffer);
av_freep(&ms->ctx->pb);
}
avformat_close_input(&ms->ctx);
/* Destroy alloc stuff. */
SDL_DestroyCond(ms->cond);
SDL_DestroyMutex(ms->lock);
rwops_close(ms->rwops);
av_free(ms->filename);
av_free(ms);
}
static void enqueue_frame(FrameQueue *fq, AVFrame *frame) {
frame->opaque = NULL;
if (fq->first) {
fq->last->opaque = frame;
fq->last = frame;
} else {
fq->first = fq->last = frame;
}
}
static AVFrame *dequeue_frame(FrameQueue *fq) {
if (!fq->first) {
return NULL;
}
AVFrame *rv = fq->first;
fq->first = (AVFrame *) rv->opaque;
if (!fq->first) {
fq->last = NULL;
}
return rv;
}
static void enqueue_packet(PacketQueue *pq, AVPacket *pkt) {
AVPacketList *pl = av_malloc(sizeof(AVPacketList));
av_init_packet(&pl->pkt);
av_packet_ref(&pl->pkt, pkt);
pl->next = NULL;
if (!pq->first) {
pq->first = pq->last = pl;
} else {
pq->last->next = pl;
pq->last = pl;
}
}
static int dequeue_packet(PacketQueue *pq, AVPacket *pkt) {
if (! pq->first ) {
return 0;
}
AVPacketList *pl = pq->first;
av_packet_move_ref(pkt, &pl->pkt);
pq->first = pl->next;
if (!pq->first) {
pq->last = NULL;
}
av_free(pl);
return 1;
}
static int count_packet_queue(PacketQueue *pq) {
AVPacketList *pl = pq->first;
int rv = 0;
while (pl) {
rv += 1;
pl = pl->next;
}
return rv;
}
static void free_packet_queue(PacketQueue *pq) {
AVPacket scratch;
av_init_packet(&scratch);
while (dequeue_packet(pq, &scratch)) {
av_packet_unref(&scratch);
}
}
static void enqueue_surface(SurfaceQueueEntry **queue, SurfaceQueueEntry *sqe) {
while (*queue) {
queue = &(*queue)->next;
}
*queue = sqe;
}
static SurfaceQueueEntry *dequeue_surface(SurfaceQueueEntry **queue) {
SurfaceQueueEntry *rv = *queue;
if (rv) {
*queue = rv->next;
}
return rv;
}
#if 0
static void check_surface_queue(MediaState *ms) {
SurfaceQueueEntry **queue = &ms->surface_queue;
int count = 0;
while (*queue) {
count += 1;
queue = &(*queue)->next;
}
if (count != ms->surface_queue_size) {
abort();
}
}
#endif
/**
* Reads a packet from one of the queues, filling the other queue if
* necessary.
*/
static int read_packet(MediaState *ms, PacketQueue *pq, AVPacket *pkt) {
AVPacket scratch;
av_init_packet(&scratch);
while (1) {
if (dequeue_packet(pq, pkt)) {
return 1;
}
if (av_read_frame(ms->ctx, &scratch)) {
pkt->data = NULL;
pkt->size = 0;
return 0;
}
if (scratch.stream_index == ms->video_stream && ! ms->video_finished) {
enqueue_packet(&ms->video_packet_queue, &scratch);
} else if (scratch.stream_index == ms->audio_stream && ! ms->audio_finished) {
enqueue_packet(&ms->audio_packet_queue, &scratch);
}
av_packet_unref(&scratch);
}
}
static AVCodecContext *find_context(AVFormatContext *ctx, int index) {
if (index == -1) {
return NULL;
}
AVCodec *codec;
AVCodecContext *codec_ctx = NULL;
AVCodecContext *codec_ctx_orig = ctx->streams[index]->codec;
codec = avcodec_find_decoder(codec_ctx_orig->codec_id);
if (codec == NULL) {
return NULL;
}
codec_ctx = avcodec_alloc_context3(codec);
if (avcodec_copy_context(codec_ctx, codec_ctx_orig)) {
goto fail;
}
if (avcodec_open2(codec_ctx, codec, NULL)) {
goto fail;
}
return codec_ctx;
fail:
avcodec_free_context(&codec_ctx);
return NULL;
}
/**
* Decodes audio. Returns 0 if no audio was decoded, or 1 if some audio was
* decoded.
*/
static void decode_audio(MediaState *ms) {
AVPacket pkt;
AVPacket pkt_temp;
AVFrame *converted_frame;
if (!ms->audio_context) {
ms->audio_finished = 1;
return;
}
if (ms->audio_decode_frame == NULL) {
ms->audio_decode_frame = av_frame_alloc();
}
av_init_packet(&pkt);
double timebase = av_q2d(ms->ctx->streams[ms->audio_stream]->time_base);
if (ms->audio_queue_target_samples < audio_target_samples) {
ms->audio_queue_target_samples += audio_sample_increase;
}
while (ms->audio_queue_samples < ms->audio_queue_target_samples) {
read_packet(ms, &ms->audio_packet_queue, &pkt);
pkt_temp = pkt;
do {
int got_frame;
int read_size = avcodec_decode_audio4(ms->audio_context, ms->audio_decode_frame, &got_frame, &pkt_temp);
if (read_size < 0) {
if (pkt.data) {
av_packet_unref(&pkt);
}
ms->audio_finished = 1;
return;
}
pkt_temp.data += read_size;
pkt_temp.size -= read_size;
if (!got_frame) {
if (pkt.data == NULL) {
ms->audio_finished = 1;
av_packet_unref(&pkt);
return;
}
break;
}
if (!ms->audio_decode_frame->channel_layout) {
ms->audio_decode_frame->channel_layout = av_get_default_channel_layout(ms->audio_decode_frame->channels);
}
converted_frame = av_frame_alloc();
converted_frame->sample_rate = audio_sample_rate;
converted_frame->channel_layout = AV_CH_LAYOUT_STEREO;
converted_frame->format = AV_SAMPLE_FMT_S16;
if(swr_convert_frame(ms->swr, converted_frame, ms->audio_decode_frame)) {
av_frame_free(&converted_frame);
continue;
}
double start = av_frame_get_best_effort_timestamp(ms->audio_decode_frame) * timebase;
double end = start + 1.0 * converted_frame->nb_samples / audio_sample_rate;
SDL_LockMutex(ms->lock);
if (start >= ms->skip) {
// Normal case, queue the frame.
ms->audio_queue_samples += converted_frame->nb_samples;
enqueue_frame(&ms->audio_queue, converted_frame);
} else if (end < ms->skip) {
// Totally before, drop the frame.
av_frame_free(&converted_frame);
} else {
// The frame straddles skip, so we queue the (necessarily single)
// frame and set the index into the frame.
ms->audio_out_frame = converted_frame;
ms->audio_out_index = BPS * (int) ((ms->skip - start) * audio_sample_rate);
}
SDL_UnlockMutex(ms->lock);
} while (pkt_temp.size);
if (pkt.data) {
av_packet_unref(&pkt);
}
}
return;
}
static enum AVPixelFormat get_pixel_format(SDL_Surface *surf) {
uint32_t pixel;
uint8_t *bytes = (uint8_t *) &pixel;
pixel = SDL_MapRGBA(surf->format, 1, 2, 3, 4);
enum AVPixelFormat fmt;
if ((bytes[0] == 4 || bytes[0] == 0) && bytes[1] == 1) {
fmt = AV_PIX_FMT_ARGB;
} else if ((bytes[0] == 4 || bytes[0] == 0) && bytes[1] == 3) {
fmt = AV_PIX_FMT_ABGR;
} else if (bytes[0] == 1) {
fmt = AV_PIX_FMT_RGBA;
} else {
fmt = AV_PIX_FMT_BGRA;
}
return fmt;
}
static SurfaceQueueEntry *decode_video_frame(MediaState *ms) {
while (1) {
if (! ms->video_pkt_tmp.size) {
av_packet_unref(&ms->video_pkt);
read_packet(ms, &ms->video_packet_queue, &ms->video_pkt);
ms->video_pkt_tmp = ms->video_pkt;
}
int got_frame = 0;
int read_size = avcodec_decode_video2(ms->video_context, ms->video_decode_frame, &got_frame, &ms->video_pkt_tmp);
if (read_size < 0) {
ms->video_finished = 1;
return NULL;
}
ms->video_pkt_tmp.data += read_size;
ms->video_pkt_tmp.size -= read_size;
if (got_frame) {
break;
}
if (!got_frame && !ms->video_pkt.size) {
ms->video_finished = 1;
return NULL;
}
}
double pts = av_frame_get_best_effort_timestamp(ms->video_decode_frame) \
* av_q2d(ms->ctx->streams[ms->video_stream]->time_base);
if (pts < ms->skip) {
return NULL;
}
// If we're behind on decoding the frame, drop it.
if (ms->video_pts_offset && (ms->video_pts_offset + pts < ms->video_read_time)) {
// If we're 5s behind, give up on video for the time being, so we don't
// blow out memory.
if (ms->video_pts_offset + pts < ms->video_read_time - 5.0) {
ms->video_finished = 1;
}
if (ms->frame_drops) {
return NULL;
}
}
SDL_Surface *sample = rgba_surface;
ms->sws = sws_getCachedContext(
ms->sws,
ms->video_decode_frame->width,
ms->video_decode_frame->height,
ms->video_decode_frame->format,
ms->video_decode_frame->width,
ms->video_decode_frame->height,
get_pixel_format(rgba_surface),
SWS_POINT,
NULL,
NULL,
NULL
);
if (!ms->sws) {
ms->video_finished = 1;
return NULL;
}
SurfaceQueueEntry *rv = av_malloc(sizeof(SurfaceQueueEntry));
rv->w = ms->video_decode_frame->width + FRAME_PADDING * 2;
rv->pitch = rv->w * sample->format->BytesPerPixel;
rv->h = ms->video_decode_frame->height + FRAME_PADDING * 2;
// We have to use SDL_calloc here, since SDL frees these pixels. This
// Should be
#ifdef USE_MEMALIGN
rv->pixels = memalign(16, rv->pitch * rv->h);
memset(rv->pixels, 0, rv->pitch * rv->h);
#else
rv->pixels = SDL_calloc(1, rv->pitch * rv->h);
#endif
rv->format = sample->format;
rv->next = NULL;
rv->pts = pts;
uint8_t *surf_pixels = (uint8_t *) rv->pixels;
uint8_t *surf_data[] = { &surf_pixels[FRAME_PADDING * rv->pitch + FRAME_PADDING * sample->format->BytesPerPixel] };
int surf_linesize[] = { rv->pitch };
sws_scale(
ms->sws,
(const uint8_t * const *) ms->video_decode_frame->data,
ms->video_decode_frame->linesize,
0,
ms->video_decode_frame->height,
surf_data,
surf_linesize
);
return rv;
}
static void decode_video(MediaState *ms) {
if (!ms->video_context) {
ms->video_finished = 1;
return;
}
if (!ms->video_decode_frame) {
ms->video_decode_frame = av_frame_alloc();
}
SDL_LockMutex(ms->lock);
if (!ms->video_finished && (ms->surface_queue_size < FRAMES)) {
SDL_UnlockMutex(ms->lock);
SurfaceQueueEntry *sqe = decode_video_frame(ms);
SDL_LockMutex(ms->lock);
if (sqe) {
enqueue_surface(&ms->surface_queue, sqe);
ms->surface_queue_size += 1;
}
}
if (!ms->video_finished && (ms->surface_queue_size < FRAMES)) {
ms->needs_decode = 1;
}
SDL_UnlockMutex(ms->lock);
}
/**
* Returns 1 if there is a video frame ready on this channel, or 0 otherwise.
*/
int media_video_ready(struct MediaState *ms) {
int consumed = 0;
int rv = 0;
if (ms->video_stream == -1) {
return 1;
}
SDL_LockMutex(ms->lock);
if (!ms->ready) {
goto done;
}
/*
* If we have an obsolete frame, drop it.
*/
if (ms->video_pts_offset) {
while (ms->surface_queue) {
/* The PTS is greater that the last frame read, so we're good. */
if (ms->surface_queue->pts + ms->video_pts_offset >= ms->video_read_time) {
break;
}
/* Otherwise, drop it without display. */
SurfaceQueueEntry *sqe = dequeue_surface(&ms->surface_queue);
ms->surface_queue_size -= 1;
SDL_free(sqe->pixels);
av_free(sqe);
consumed = 1;
}
}
/*
* Otherwise, check to see if we have a frame with a PTS that has passed.
*/
if (ms->surface_queue) {
if (ms->video_pts_offset) {
if (ms->surface_queue->pts + ms->video_pts_offset <= current_time) {
rv = 1;
}
} else {
rv = 1;
}
}
done:
/* Only signal if we've consumed something. */
if (consumed) {
ms->needs_decode = 1;
SDL_CondBroadcast(ms->cond);
}
SDL_UnlockMutex(ms->lock);
return rv;
}
SDL_Surface *media_read_video(MediaState *ms) {
SDL_Surface *rv = NULL;
SurfaceQueueEntry *sqe = NULL;
if (ms->video_stream == -1) {
return NULL;
}
SDL_LockMutex(ms->lock);
while (!ms->ready) {
SDL_CondWait(ms->cond, ms->lock);
}
if (!ms->surface_queue_size) {
goto done;
}
if (ms->video_pts_offset == 0.0) {
ms->video_pts_offset = current_time - ms->surface_queue->pts;
}
if (ms->surface_queue->pts + ms->video_pts_offset <= current_time) {
sqe = dequeue_surface(&ms->surface_queue);
ms->surface_queue_size -= 1;
}
done:
/* Only signal if we've consumed something. */
if (sqe) {
ms->needs_decode = 1;
ms->video_read_time = current_time;
SDL_CondBroadcast(ms->cond);
}
SDL_UnlockMutex(ms->lock);
if (sqe) {
rv = SDL_CreateRGBSurfaceFrom(
sqe->pixels,
sqe->w,
sqe->h,
sqe->format->BitsPerPixel,
sqe->pitch,
sqe->format->Rmask,
sqe->format->Gmask,
sqe->format->Bmask,
sqe->format->Amask
);
/* Force SDL to take over management of pixels. */
rv->flags &= ~SDL_PREALLOC;
av_free(sqe);
}
return rv;
}
static int decode_thread(void *arg) {
MediaState *ms = (MediaState *) arg;
int err;
AVFormatContext *ctx = avformat_alloc_context();
ms->ctx = ctx;
AVIOContext *io_context = rwops_open(ms->rwops);
ctx->pb = io_context;
err = avformat_open_input(&ctx, ms->filename, NULL, NULL);
if (err) {
goto finish;
}
err = avformat_find_stream_info(ctx, NULL);
if (err) {
goto finish;
}
ms->video_stream = -1;
ms->audio_stream = -1;
for (int i = 0; i < ctx->nb_streams; i++) {
if (ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
if (ms->want_video && ms->video_stream == -1) {
ms->video_stream = i;
}
}
if (ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
if (ms->audio_stream == -1) {
ms->audio_stream = i;
}
}
}
ms->video_context = find_context(ctx, ms->video_stream);
ms->audio_context = find_context(ctx, ms->audio_stream);
ms->swr = swr_alloc();
av_init_packet(&ms->video_pkt);
// Compute the number of samples we need to play back.
if (ms->audio_duration < 0) {
if (av_fmt_ctx_get_duration_estimation_method(ctx) != AVFMT_DURATION_FROM_BITRATE) {
long long duration = ((long long) ctx->duration) * audio_sample_rate;
ms->audio_duration = (unsigned int) (duration / AV_TIME_BASE);
ms->total_duration = 1.0 * ctx->duration / AV_TIME_BASE;
// Check that the duration is reasonable (between 0s and 3600s). If not,
// reject it.
if (ms->audio_duration < 0 || ms->audio_duration > 3600 * audio_sample_rate) {
ms->audio_duration = -1;
}
ms->audio_duration -= (unsigned int) (ms->skip * audio_sample_rate);
} else {
ms->audio_duration = -1;
}
}
if (ms->skip != 0.0) {
av_seek_frame(ctx, -1, (int64_t) (ms->skip * AV_TIME_BASE), AVSEEK_FLAG_BACKWARD);
}
while (!ms->quit) {
if (! ms->audio_finished) {
decode_audio(ms);
}
if (! ms->video_finished) {
decode_video(ms);
}
SDL_LockMutex(ms->lock);
if (!ms->ready) {
ms->ready = 1;
SDL_CondBroadcast(ms->cond);
}
if (!(ms->needs_decode || ms->quit)) {
SDL_CondWait(ms->cond, ms->lock);
}
ms->needs_decode = 0;
SDL_UnlockMutex(ms->lock);
}
finish:
/* Data used by the decoder should be freed here, while data shared with
* the readers should be freed in media_close.
*/
SDL_LockMutex(ms->lock);
/* Ensures that every stream becomes ready. */
if (!ms->ready) {
ms->ready = 1;
SDL_CondBroadcast(ms->cond);
}
while (!ms->quit) {
SDL_CondWait(ms->cond, ms->lock);
}
SDL_UnlockMutex(ms->lock);
deallocate(ms);
return 0;
}
int media_read_audio(struct MediaState *ms, Uint8 *stream, int len) {
SDL_LockMutex(ms->lock);
if(!ms->ready) {
SDL_UnlockMutex(ms->lock);
memset(stream, 0, len);
return len;
}
int rv = 0;
if (ms->audio_duration >= 0) {
unsigned int remaining = (ms->audio_duration - ms->audio_read_samples) * BPS;
if (len > remaining) {
len = remaining;
}
if (!remaining) {
ms->audio_finished = 1;
}
}
while (len) {
if (!ms->audio_out_frame) {
ms->audio_out_frame = dequeue_frame(&ms->audio_queue);
ms->audio_out_index = 0;
}
if (!ms->audio_out_frame) {
break;
}
AVFrame *f = ms->audio_out_frame;
int avail = f->nb_samples * BPS - ms->audio_out_index;
int count;
if (len > avail) {
count = avail;
} else {
count = len;
}
memcpy(stream, &f->data[0][ms->audio_out_index], count);
ms->audio_out_index += count;
ms->audio_read_samples += count / BPS;
ms->audio_queue_samples -= count / BPS;
rv += count;
len -= count;
stream += count;
if (ms->audio_out_index >= f->nb_samples * BPS) {
av_frame_free(&ms->audio_out_frame);
ms->audio_out_index = 0;
}
}
/* Only signal if we've consumed something. */
if (rv) {
ms->needs_decode = 1;
SDL_CondBroadcast(ms->cond);
}
SDL_UnlockMutex(ms->lock);
if (ms->audio_duration >= 0) {
if ((ms->audio_duration - ms->audio_read_samples) * BPS < len) {
len = (ms->audio_duration - ms->audio_read_samples) * BPS;
}
memset(stream, 0, len);
ms->audio_read_samples += len / BPS;
rv += len;
}
return rv;
}
void media_wait_ready(struct MediaState *ms) {
SDL_LockMutex(ms->lock);
while (!ms->ready) {
SDL_CondWait(ms->cond, ms->lock);
}
SDL_UnlockMutex(ms->lock);
}
double media_duration(MediaState *ms) {
return ms->total_duration;
}
void media_start(MediaState *ms) {
char buf[1024];
snprintf(buf, 1024, "decode: %s", ms->filename);
SDL_Thread *t = SDL_CreateThread(decode_thread, buf, (void *) ms);
if (t) {
ms->started = 1;
SDL_DetachThread(t);
}
}
MediaState *media_open(SDL_RWops *rwops, const char *filename) {
MediaState *ms = av_calloc(1, sizeof(MediaState));
ms->filename = av_strdup(filename);
ms->rwops = rwops;
ms->cond = SDL_CreateCond();
ms->lock = SDL_CreateMutex();
ms->audio_duration = -1;
ms->frame_drops = 1;
return ms;
}
/**
* Sets the start and end of the stream. This must be called before
* media_start.
*
* start
* The time in the stream at which the media starts playing.
* end
* If not 0, the time at which the stream is forced to end if it has not
* already. If 0, the stream plays until its natural end.
*/
void media_start_end(MediaState *ms, double start, double end) {
ms->skip = start;
if (end >= 0) {
if (end < start) {
ms->audio_duration = 0;
} else {
ms->audio_duration = (int) ((end - start) * audio_sample_rate);
}
}
}
/**
* Marks the channel as having video.
*/
void media_want_video(MediaState *ms, int video) {
ms->want_video = 1;
ms->frame_drops = (video != 2);
}
void media_close(MediaState *ms) {
if (!ms->started) {
deallocate(ms);
return;
}
/* Tell the decoder to terminate. It will deallocate everything for us. */
SDL_LockMutex(ms->lock);
ms->quit = 1;
SDL_CondBroadcast(ms->cond);
SDL_UnlockMutex(ms->lock);
}
void media_advance_time(void) {
current_time = SPEED * av_gettime() * 1e-6;
}
void media_sample_surfaces(SDL_Surface *rgb, SDL_Surface *rgba) {
rgb_surface = rgb;
rgba_surface = rgba;
}
void media_init(int rate, int status) {
audio_sample_rate = rate / SPEED;
av_register_all();
if (status) {
av_log_set_level(AV_LOG_INFO);
} else {
av_log_set_level(AV_LOG_ERROR);
}
}
|