Lines Matching refs:ms

282 static void deallocate(MediaState *ms) {  in deallocate()  argument
285 SurfaceQueueEntry *sqe = dequeue_surface(&ms->surface_queue); in deallocate()
297 if (ms->sws) { in deallocate()
298 sws_freeContext(ms->sws); in deallocate()
301 if (ms->video_decode_frame) { in deallocate()
302 av_frame_free(&ms->video_decode_frame); in deallocate()
305 av_packet_unref(&ms->video_pkt); in deallocate()
308 if (ms->swr) { in deallocate()
309 swr_free(&ms->swr); in deallocate()
312 if (ms->audio_decode_frame) { in deallocate()
313 av_frame_free(&ms->audio_decode_frame); in deallocate()
316 if (ms->audio_out_frame) { in deallocate()
317 av_frame_free(&ms->audio_out_frame); in deallocate()
321 AVFrame *f = dequeue_frame(&ms->audio_queue); in deallocate()
331 free_packet_queue(&ms->audio_packet_queue); in deallocate()
332 free_packet_queue(&ms->video_packet_queue); in deallocate()
334 if (ms->video_context) { in deallocate()
335 avcodec_free_context(&ms->video_context); in deallocate()
337 if (ms->audio_context) { in deallocate()
338 avcodec_free_context(&ms->audio_context); in deallocate()
341 if (ms->ctx) { in deallocate()
343 if (ms->ctx->pb) { in deallocate()
344 if (ms->ctx->pb->buffer) { in deallocate()
345 av_freep(&ms->ctx->pb->buffer); in deallocate()
347 av_freep(&ms->ctx->pb); in deallocate()
350 avformat_close_input(&ms->ctx); in deallocate()
351 avformat_free_context(ms->ctx); in deallocate()
355 if (ms->cond) { in deallocate()
356 SDL_DestroyCond(ms->cond); in deallocate()
358 if (ms->lock) { in deallocate()
359 SDL_DestroyMutex(ms->lock); in deallocate()
362 if (ms->rwops) { in deallocate()
363 rwops_close(ms->rwops); in deallocate()
366 if (ms->filename) { in deallocate()
367 av_free(ms->filename); in deallocate()
374 ms->next = deallocate_queue; in deallocate()
375 deallocate_queue = ms; in deallocate()
386 MediaState *ms = deallocate_queue; in deallocate_deferred() local
387 deallocate_queue = ms->next; in deallocate_deferred()
389 if (ms->thread) { in deallocate_deferred()
390 SDL_WaitThread(ms->thread, NULL); in deallocate_deferred()
393 av_free(ms); in deallocate_deferred()
509 static void check_surface_queue(MediaState *ms) {
511 SurfaceQueueEntry **queue = &ms->surface_queue;
520 if (count != ms->surface_queue_size) {
532 static int read_packet(MediaState *ms, PacketQueue *pq, AVPacket *pkt) { in read_packet() argument
542 if (av_read_frame(ms->ctx, &scratch)) { in read_packet()
548 if (scratch.stream_index == ms->video_stream && ! ms->video_finished) { in read_packet()
549 enqueue_packet(&ms->video_packet_queue, &scratch); in read_packet()
550 } else if (scratch.stream_index == ms->audio_stream && ! ms->audio_finished) { in read_packet()
551 enqueue_packet(&ms->audio_packet_queue, &scratch); in read_packet()
661 static void decode_audio(MediaState *ms) { in decode_audio() argument
666 if (!ms->audio_context) { in decode_audio()
667 ms->audio_finished = 1; in decode_audio()
671 if (ms->audio_decode_frame == NULL) { in decode_audio()
672 ms->audio_decode_frame = av_frame_alloc(); in decode_audio()
675 if (ms->audio_decode_frame == NULL) { in decode_audio()
676 ms->audio_finished = 1; in decode_audio()
682 double timebase = av_q2d(ms->ctx->streams[ms->audio_stream]->time_base); in decode_audio()
684 if (ms->audio_queue_target_samples < audio_target_samples) { in decode_audio()
685 ms->audio_queue_target_samples += audio_sample_increase; in decode_audio()
688 while (ms->audio_queue_samples < ms->audio_queue_target_samples) { in decode_audio()
690 read_packet(ms, &ms->audio_packet_queue, &pkt); in decode_audio()
696 int read_size = decode_common(ms->audio_context, ms->audio_decode_frame, &got_frame, &pkt_temp); in decode_audio()
704 ms->audio_finished = 1; in decode_audio()
713 ms->audio_finished = 1; in decode_audio()
724 ms->audio_finished = 1; in decode_audio()
732 if (!ms->audio_decode_frame->channel_layout) { in decode_audio()
733ms->audio_decode_frame->channel_layout = av_get_default_channel_layout(ms->audio_decode_frame->cha… in decode_audio()
735 if (audio_equal_mono && (ms->audio_decode_frame->channels == 1)) { in decode_audio()
737 ms->swr, in decode_audio()
741 ms->audio_decode_frame->channel_layout, in decode_audio()
742 ms->audio_decode_frame->format, in decode_audio()
743 ms->audio_decode_frame->sample_rate, in decode_audio()
747 swr_set_matrix(ms->swr, stereo_matrix, 1); in decode_audio()
751 if(swr_convert_frame(ms->swr, converted_frame, ms->audio_decode_frame)) { in decode_audio()
756 double start = ms->audio_decode_frame->best_effort_timestamp * timebase; in decode_audio()
759 SDL_LockMutex(ms->lock); in decode_audio()
761 if (start >= ms->skip) { in decode_audio()
764 ms->audio_queue_samples += converted_frame->nb_samples; in decode_audio()
765 enqueue_frame(&ms->audio_queue, converted_frame); in decode_audio()
767 } else if (end < ms->skip) { in decode_audio()
774 ms->audio_out_frame = converted_frame; in decode_audio()
775 ms->audio_out_index = BPS * (int) ((ms->skip - start) * audio_sample_rate); in decode_audio()
779 SDL_UnlockMutex(ms->lock); in decode_audio()
815 static SurfaceQueueEntry *decode_video_frame(MediaState *ms) { in decode_video_frame() argument
819 if (! ms->video_pkt_tmp.size) { in decode_video_frame()
820 av_packet_unref(&ms->video_pkt); in decode_video_frame()
821 read_packet(ms, &ms->video_packet_queue, &ms->video_pkt); in decode_video_frame()
822 ms->video_pkt_tmp = ms->video_pkt; in decode_video_frame()
826 …int read_size = decode_common(ms->video_context, ms->video_decode_frame, &got_frame, &ms->video_pk… in decode_video_frame()
829 ms->video_finished = 1; in decode_video_frame()
833 ms->video_pkt_tmp.data += read_size; in decode_video_frame()
834 ms->video_pkt_tmp.size -= read_size; in decode_video_frame()
840 if (!got_frame && !ms->video_pkt.size) { in decode_video_frame()
841 ms->video_finished = 1; in decode_video_frame()
847 …double pts = ms->video_decode_frame->best_effort_timestamp * av_q2d(ms->ctx->streams[ms->video_str… in decode_video_frame()
849 if (pts < ms->skip) { in decode_video_frame()
854 if (ms->video_pts_offset && (ms->video_pts_offset + pts < ms->video_read_time)) { in decode_video_frame()
858 if (ms->video_pts_offset + pts < ms->video_read_time - 5.0) { in decode_video_frame()
859 ms->video_finished = 1; in decode_video_frame()
862 if (ms->frame_drops) { in decode_video_frame()
869 ms->sws = sws_getCachedContext( in decode_video_frame()
870 ms->sws, in decode_video_frame()
872 ms->video_decode_frame->width, in decode_video_frame()
873 ms->video_decode_frame->height, in decode_video_frame()
874 ms->video_decode_frame->format, in decode_video_frame()
876 ms->video_decode_frame->width, in decode_video_frame()
877 ms->video_decode_frame->height, in decode_video_frame()
887 if (!ms->sws) { in decode_video_frame()
888 ms->video_finished = 1; in decode_video_frame()
894 ms->video_finished = 1; in decode_video_frame()
897 rv->w = ms->video_decode_frame->width + FRAME_PADDING * 2; in decode_video_frame()
898 rv->h = ms->video_decode_frame->height + FRAME_PADDING * 2; in decode_video_frame()
923 ms->sws, in decode_video_frame()
925 (const uint8_t * const *) ms->video_decode_frame->data, in decode_video_frame()
926 ms->video_decode_frame->linesize, in decode_video_frame()
929 ms->video_decode_frame->height, in decode_video_frame()
939 static void decode_video(MediaState *ms) { in decode_video() argument
940 if (!ms->video_context) { in decode_video()
941 ms->video_finished = 1; in decode_video()
945 if (!ms->video_decode_frame) { in decode_video()
946 ms->video_decode_frame = av_frame_alloc(); in decode_video()
949 if (!ms->video_decode_frame) { in decode_video()
950 ms->video_finished = 1; in decode_video()
954 SDL_LockMutex(ms->lock); in decode_video()
956 if (!ms->video_finished && (ms->surface_queue_size < FRAMES)) { in decode_video()
958 SDL_UnlockMutex(ms->lock); in decode_video()
960 SurfaceQueueEntry *sqe = decode_video_frame(ms); in decode_video()
962 SDL_LockMutex(ms->lock); in decode_video()
965 enqueue_surface(&ms->surface_queue, sqe); in decode_video()
966 ms->surface_queue_size += 1; in decode_video()
970 if (!ms->video_finished && (ms->surface_queue_size < FRAMES)) { in decode_video()
971 ms->needs_decode = 1; in decode_video()
974 SDL_UnlockMutex(ms->lock); in decode_video()
979 void media_read_sync(struct MediaState *ms);
980 void media_read_sync_finish(struct MediaState *ms);
986 int media_video_ready(struct MediaState *ms) { in media_video_ready() argument
991 if (ms->video_stream == -1) { in media_video_ready()
995 SDL_LockMutex(ms->lock); in media_video_ready()
997 if (!ms->ready) { in media_video_ready()
1001 if (ms->pause_time > 0) { in media_video_ready()
1005 double offset_time = current_time - ms->time_offset; in media_video_ready()
1010 if (ms->video_pts_offset) { in media_video_ready()
1011 while (ms->surface_queue) { in media_video_ready()
1014 if (ms->surface_queue->pts + ms->video_pts_offset >= ms->video_read_time) { in media_video_ready()
1019 SurfaceQueueEntry *sqe = dequeue_surface(&ms->surface_queue); in media_video_ready()
1020 ms->surface_queue_size -= 1; in media_video_ready()
1034 if (ms->surface_queue) { in media_video_ready()
1035 if (ms->video_pts_offset) { in media_video_ready()
1036 if (ms->surface_queue->pts + ms->video_pts_offset <= offset_time + frame_early_delivery) { in media_video_ready()
1048 ms->needs_decode = 1; in media_video_ready()
1049 SDL_CondBroadcast(ms->cond); in media_video_ready()
1052 SDL_UnlockMutex(ms->lock); in media_video_ready()
1058 SDL_Surface *media_read_video(MediaState *ms) { in media_read_video() argument
1063 if (ms->video_stream == -1) { in media_read_video()
1067 double offset_time = current_time - ms->time_offset; in media_read_video()
1069 SDL_LockMutex(ms->lock); in media_read_video()
1072 while (!ms->ready) { in media_read_video()
1073 SDL_CondWait(ms->cond, ms->lock); in media_read_video()
1077 if (ms->pause_time > 0) { in media_read_video()
1081 if (!ms->surface_queue_size) { in media_read_video()
1085 if (ms->video_pts_offset == 0.0) { in media_read_video()
1086 ms->video_pts_offset = offset_time - ms->surface_queue->pts; in media_read_video()
1089 if (ms->surface_queue->pts + ms->video_pts_offset <= offset_time + frame_early_delivery) { in media_read_video()
1090 sqe = dequeue_surface(&ms->surface_queue); in media_read_video()
1091 ms->surface_queue_size -= 1; in media_read_video()
1099 ms->needs_decode = 1; in media_read_video()
1100 ms->video_read_time = offset_time; in media_read_video()
1101 SDL_CondBroadcast(ms->cond); in media_read_video()
1104 SDL_UnlockMutex(ms->lock); in media_read_video()
1129 MediaState *ms = (MediaState *) arg; in decode_thread() local
1137 ms->ctx = ctx; in decode_thread()
1139 AVIOContext *io_context = rwops_open(ms->rwops); in decode_thread()
1145 err = avformat_open_input(&ctx, ms->filename, NULL, NULL); in decode_thread()
1148 ms->ctx = NULL; in decode_thread()
1158 ms->video_stream = -1; in decode_thread()
1159 ms->audio_stream = -1; in decode_thread()
1163 if (ms->want_video && ms->video_stream == -1) { in decode_thread()
1164 ms->video_stream = i; in decode_thread()
1169 if (ms->audio_stream == -1) { in decode_thread()
1170 ms->audio_stream = i; in decode_thread()
1175 ms->video_context = find_context(ctx, ms->video_stream); in decode_thread()
1176 ms->audio_context = find_context(ctx, ms->audio_stream); in decode_thread()
1178 ms->swr = swr_alloc(); in decode_thread()
1179 if (ms->swr == NULL) { in decode_thread()
1183 av_init_packet(&ms->video_pkt); in decode_thread()
1186 if (ms->audio_duration < 0) { in decode_thread()
1190 ms->audio_duration = (unsigned int) (duration / AV_TIME_BASE); in decode_thread()
1192 ms->total_duration = 1.0 * ctx->duration / AV_TIME_BASE; in decode_thread()
1196 if (ms->audio_duration < 0 || ms->audio_duration > 3600 * audio_sample_rate) { in decode_thread()
1197 ms->audio_duration = -1; in decode_thread()
1200 ms->audio_duration -= (unsigned int) (ms->skip * audio_sample_rate); in decode_thread()
1204 ms->audio_duration = -1; in decode_thread()
1208 if (ms->skip != 0.0) { in decode_thread()
1209 av_seek_frame(ctx, -1, (int64_t) (ms->skip * AV_TIME_BASE), AVSEEK_FLAG_BACKWARD); in decode_thread()
1212 while (!ms->quit) { in decode_thread()
1214 if (! ms->audio_finished) { in decode_thread()
1215 decode_audio(ms); in decode_thread()
1218 if (! ms->video_finished) { in decode_thread()
1219 decode_video(ms); in decode_thread()
1222 SDL_LockMutex(ms->lock); in decode_thread()
1224 if (!ms->ready) { in decode_thread()
1225 ms->ready = 1; in decode_thread()
1226 SDL_CondBroadcast(ms->cond); in decode_thread()
1229 if (!(ms->needs_decode || ms->quit)) { in decode_thread()
1230 SDL_CondWait(ms->cond, ms->lock); in decode_thread()
1233 ms->needs_decode = 0; in decode_thread()
1235 SDL_UnlockMutex(ms->lock); in decode_thread()
1244 SDL_LockMutex(ms->lock); in decode_thread()
1247 if (!ms->ready) { in decode_thread()
1248 ms->ready = 1; in decode_thread()
1249 SDL_CondBroadcast(ms->cond); in decode_thread()
1252 while (!ms->quit) { in decode_thread()
1253 SDL_CondWait(ms->cond, ms->lock); in decode_thread()
1256 SDL_UnlockMutex(ms->lock); in decode_thread()
1258 deallocate(ms); in decode_thread()
1264 void media_read_sync_finish(struct MediaState *ms) { in media_read_sync_finish() argument
1271 SDL_LockMutex(ms->lock); in media_read_sync_finish()
1274 if (!ms->ready) { in media_read_sync_finish()
1275 ms->ready = 1; in media_read_sync_finish()
1276 SDL_CondBroadcast(ms->cond); in media_read_sync_finish()
1279 while (!ms->quit) { in media_read_sync_finish()
1283 SDL_UnlockMutex(ms->lock); in media_read_sync_finish()
1285 deallocate(ms); in media_read_sync_finish()
1291 MediaState *ms = (MediaState *) arg; in decode_sync_start() local
1297 media_read_sync_finish(ms); in decode_sync_start()
1299 ms->ctx = ctx; in decode_sync_start()
1301 AVIOContext *io_context = rwops_open(ms->rwops); in decode_sync_start()
1303 media_read_sync_finish(ms); in decode_sync_start()
1307 err = avformat_open_input(&ctx, ms->filename, NULL, NULL); in decode_sync_start()
1310 ms->ctx = NULL; in decode_sync_start()
1311 media_read_sync_finish(ms); in decode_sync_start()
1316 media_read_sync_finish(ms); in decode_sync_start()
1320 ms->video_stream = -1; in decode_sync_start()
1321 ms->audio_stream = -1; in decode_sync_start()
1325 if (ms->want_video && ms->video_stream == -1) { in decode_sync_start()
1326 ms->video_stream = i; in decode_sync_start()
1331 if (ms->audio_stream == -1) { in decode_sync_start()
1332 ms->audio_stream = i; in decode_sync_start()
1337 ms->video_context = find_context(ctx, ms->video_stream); in decode_sync_start()
1338 ms->audio_context = find_context(ctx, ms->audio_stream); in decode_sync_start()
1340 ms->swr = swr_alloc(); in decode_sync_start()
1341 if (ms->swr == NULL) { in decode_sync_start()
1342 media_read_sync_finish(ms); in decode_sync_start()
1345 av_init_packet(&ms->video_pkt); in decode_sync_start()
1348 if (ms->audio_duration < 0) { in decode_sync_start()
1352 ms->audio_duration = (unsigned int) (duration / AV_TIME_BASE); in decode_sync_start()
1354 ms->total_duration = 1.0 * ctx->duration / AV_TIME_BASE; in decode_sync_start()
1358 if (ms->audio_duration < 0 || ms->audio_duration > 3600 * audio_sample_rate) { in decode_sync_start()
1359 ms->audio_duration = -1; in decode_sync_start()
1362 ms->audio_duration -= (unsigned int) (ms->skip * audio_sample_rate); in decode_sync_start()
1366 ms->audio_duration = -1; in decode_sync_start()
1370 if (ms->skip != 0.0) { in decode_sync_start()
1371 av_seek_frame(ctx, -1, (int64_t) (ms->skip * AV_TIME_BASE), AVSEEK_FLAG_BACKWARD); in decode_sync_start()
1380 void media_read_sync(struct MediaState *ms) { in media_read_sync() argument
1385 if (!ms->quit) { in media_read_sync()
1387 if (! ms->audio_finished) { in media_read_sync()
1388 decode_audio(ms); in media_read_sync()
1391 if (! ms->video_finished) { in media_read_sync()
1392 decode_video(ms); in media_read_sync()
1395 SDL_LockMutex(ms->lock); in media_read_sync()
1397 if (!ms->ready) { in media_read_sync()
1398 ms->ready = 1; in media_read_sync()
1399 SDL_CondBroadcast(ms->cond); in media_read_sync()
1402 if (!(ms->needs_decode || ms->quit)) { in media_read_sync()
1406 ms->needs_decode = 0; in media_read_sync()
1408 SDL_UnlockMutex(ms->lock); in media_read_sync()
1413 int media_read_audio(struct MediaState *ms, Uint8 *stream, int len) { in media_read_audio() argument
1415 media_read_sync(ms); in media_read_audio()
1418 SDL_LockMutex(ms->lock); in media_read_audio()
1420 if(!ms->ready) { in media_read_audio()
1421 SDL_UnlockMutex(ms->lock); in media_read_audio()
1428 if (ms->audio_duration >= 0) { in media_read_audio()
1429 unsigned int remaining = (ms->audio_duration - ms->audio_read_samples) * BPS; in media_read_audio()
1435 ms->audio_finished = 1; in media_read_audio()
1442 if (!ms->audio_out_frame) { in media_read_audio()
1443 ms->audio_out_frame = dequeue_frame(&ms->audio_queue); in media_read_audio()
1444 ms->audio_out_index = 0; in media_read_audio()
1447 if (!ms->audio_out_frame) { in media_read_audio()
1451 AVFrame *f = ms->audio_out_frame; in media_read_audio()
1453 int avail = f->nb_samples * BPS - ms->audio_out_index; in media_read_audio()
1462 memcpy(stream, &f->data[0][ms->audio_out_index], count); in media_read_audio()
1464 ms->audio_out_index += count; in media_read_audio()
1466 ms->audio_read_samples += count / BPS; in media_read_audio()
1467 ms->audio_queue_samples -= count / BPS; in media_read_audio()
1473 if (ms->audio_out_index >= f->nb_samples * BPS) { in media_read_audio()
1474 av_frame_free(&ms->audio_out_frame); in media_read_audio()
1475 ms->audio_out_index = 0; in media_read_audio()
1481 ms->needs_decode = 1; in media_read_audio()
1482 SDL_CondBroadcast(ms->cond); in media_read_audio()
1485 SDL_UnlockMutex(ms->lock); in media_read_audio()
1487 if (ms->audio_duration >= 0) { in media_read_audio()
1488 if ((ms->audio_duration - ms->audio_read_samples) * BPS < len) { in media_read_audio()
1489 len = (ms->audio_duration - ms->audio_read_samples) * BPS; in media_read_audio()
1493 ms->audio_read_samples += len / BPS; in media_read_audio()
1500 void media_wait_ready(struct MediaState *ms) { in media_wait_ready() argument
1502 SDL_LockMutex(ms->lock); in media_wait_ready()
1504 while (!ms->ready) { in media_wait_ready()
1505 SDL_CondWait(ms->cond, ms->lock); in media_wait_ready()
1508 SDL_UnlockMutex(ms->lock); in media_wait_ready()
1513 double media_duration(MediaState *ms) { in media_duration() argument
1514 return ms->total_duration; in media_duration()
1517 void media_start(MediaState *ms) { in media_start() argument
1520 decode_sync_start(ms); in media_start()
1525 snprintf(buf, 1024, "decode: %s", ms->filename); in media_start()
1526 SDL_Thread *t = SDL_CreateThread(decode_thread, buf, (void *) ms); in media_start()
1527 ms->thread = t; in media_start()
1536 MediaState *ms = av_calloc(1, sizeof(MediaState)); in media_open() local
1537 if (ms == NULL) { in media_open()
1541 ms->filename = av_strdup(filename); in media_open()
1542 if (ms->filename == NULL) { in media_open()
1543 deallocate(ms); in media_open()
1546 ms->rwops = rwops; in media_open()
1549 ms->cond = SDL_CreateCond(); in media_open()
1550 if (ms->cond == NULL) { in media_open()
1551 deallocate(ms); in media_open()
1554 ms->lock = SDL_CreateMutex(); in media_open()
1555 if (ms->lock == NULL) { in media_open()
1556 deallocate(ms); in media_open()
1561 ms->audio_duration = -1; in media_open()
1562 ms->frame_drops = 1; in media_open()
1564 return ms; in media_open()
1577 void media_start_end(MediaState *ms, double start, double end) { in media_start_end() argument
1578 ms->skip = start; in media_start_end()
1582 ms->audio_duration = 0; in media_start_end()
1584 ms->audio_duration = (int) ((end - start) * audio_sample_rate); in media_start_end()
1592 void media_want_video(MediaState *ms, int video) { in media_want_video() argument
1593 ms->want_video = 1; in media_want_video()
1594 ms->frame_drops = (video != 2); in media_want_video()
1597 void media_pause(MediaState *ms, int pause) { in media_pause() argument
1598 if (pause && (ms->pause_time == 0)) { in media_pause()
1599 ms->pause_time = current_time; in media_pause()
1600 } else if ((!pause) && (ms->pause_time > 0)) { in media_pause()
1601 ms->time_offset += current_time - ms->pause_time; in media_pause()
1602 ms->pause_time = 0; in media_pause()
1606 void media_close(MediaState *ms) { in media_close() argument
1608 if (!ms->thread) { in media_close()
1609 deallocate(ms); in media_close()
1614 SDL_LockMutex(ms->lock); in media_close()
1615 ms->quit = 1; in media_close()
1618 media_read_sync_finish(ms); in media_close()
1621 SDL_CondBroadcast(ms->cond); in media_close()
1622 SDL_UnlockMutex(ms->lock); in media_close()