History

ffmpeg: explicitly handle sub2video subpicture initialization

Each time the sub2video structure is initialized, the sub2video subpicture is initialized together with the first received heartbeat. The heartbeat's PTS is utilized as the subpicture start time. Additionally, add some documentation on the stages.

Jan Ekström committed 7 years ago

Commit 7ba3ea21 parent 39fbd063 files · +24 −9Browse files

Unified · Split

3 files
  1. fftools/ffmpeg.c +15 −7
  2. fftools/ffmpeg.h +2 −1
  3. fftools/ffmpeg_filter.c +7 −1
fftools/ffmpeg.c+15−7
@@ -237,7 +237,7 @@ static void sub2video_push_ref(InputStream *ist, int64_t pts) · 236 unmodified lines
237237 }
238238 }
239239
240−void sub2video_update(InputStream *ist, AVSubtitle *sub)
240+void sub2video_update(InputStream *ist, int64_t heartbeat_pts, AVSubtitle *sub)
241241 {
242242 AVFrame *frame = ist->sub2video.frame;
243243 int8_t *dst;
@@ -254,7 +254,12 @@ void sub2video_update(InputStream *ist, AVSubtitle *sub) · 10 unmodified lines
254254 AV_TIME_BASE_Q, ist->st->time_base);
255255 num_rects = sub->num_rects;
256256 } else {
257− pts = ist->sub2video.end_pts;
257+ /* If we are initializing the system, utilize current heartbeat
258+ PTS as the start time, and show until the following subpicture
259+ is received. Otherwise, utilize the previous subpicture's end time
260+ as the fall-back value. */
261+ pts = ist->sub2video.initialize ?
262+ heartbeat_pts : ist->sub2video.end_pts;
258263 end_pts = INT64_MAX;
259264 num_rects = 0;
260265 }
@@ -269,6 +274,7 @@ void sub2video_update(InputStream *ist, AVSubtitle *sub) · 8 unmodified lines
269274 sub2video_copy_rect(dst, dst_linesize, frame->width, frame->height, sub->rects[i]);
270275 sub2video_push_ref(ist, pts);
271276 ist->sub2video.end_pts = end_pts;
277+ ist->sub2video.initialize = 0;
272278 }
273279
274280 static void sub2video_heartbeat(InputStream *ist, int64_t pts)
@@ -291,9 +297,11 @@ static void sub2video_heartbeat(InputStream *ist, int64_t pts) · 16 unmodified lines
291297 /* do not send the heartbeat frame if the subtitle is already ahead */
292298 if (pts2 <= ist2->sub2video.last_pts)
293299 continue;
294− if (pts2 >= ist2->sub2video.end_pts ||
295− (!ist2->sub2video.frame->data[0] && ist2->sub2video.end_pts < INT64_MAX))
296− sub2video_update(ist2, NULL);
300+ if (pts2 >= ist2->sub2video.end_pts || ist2->sub2video.initialize)
301+ /* if we have hit the end of the current displayed subpicture,
302+ or if we need to initialize the system, update the
303+ overlayed subpicture and its start/end times */
304+ sub2video_update(ist2, pts2 + 1, NULL);
297305 for (j = 0, nb_reqs = 0; j < ist2->nb_filters; j++)
298306 nb_reqs += av_buffersrc_get_nb_failed_requests(ist2->filters[j]->filter);
299307 if (nb_reqs)
@@ -307,7 +315,7 @@ static void sub2video_flush(InputStream *ist) · 7 unmodified lines
307315 int ret;
308316
309317 if (ist->sub2video.end_pts < INT64_MAX)
310− sub2video_update(ist, NULL);
318+ sub2video_update(ist, INT64_MAX, NULL);
311319 for (i = 0; i < ist->nb_filters; i++) {
312320 ret = av_buffersrc_add_frame(ist->filters[i]->filter, NULL);
313321 if (ret != AVERROR_EOF && ret < 0)
@@ -2507,7 +2515,7 @@ static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output, · 2193 unmodified lines
25072515 return ret;
25082516
25092517 if (ist->sub2video.frame) {
2510− sub2video_update(ist, &subtitle);
2518+ sub2video_update(ist, INT64_MIN, &subtitle);
25112519 } else if (ist->nb_filters) {
25122520 if (!ist->sub2video.sub_queue)
25132521 ist->sub2video.sub_queue = av_fifo_alloc(8 * sizeof(AVSubtitle));
fftools/ffmpeg.h+2−1
@@ -348,6 +348,7 @@ typedef struct InputStream { · 347 unmodified lines
348348 AVFifoBuffer *sub_queue; ///< queue of AVSubtitle* before filter init
349349 AVFrame *frame;
350350 int w, h;
351+ unsigned int initialize; ///< marks if sub2video_update should force an initialization
351352 } sub2video;
352353
353354 int dr1;
@@ -645,7 +646,7 @@ int filtergraph_is_simple(FilterGraph *fg); · 291 unmodified lines
645646 int init_simple_filtergraph(InputStream *ist, OutputStream *ost);
646647 int init_complex_filtergraph(FilterGraph *fg);
647648
648−void sub2video_update(InputStream *ist, AVSubtitle *sub);
649+void sub2video_update(InputStream *ist, int64_t heartbeat_pts, AVSubtitle *sub);
649650
650651 int ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *frame);
651652
fftools/ffmpeg_filter.c+7−1
@@ -740,6 +740,12 @@ static int sub2video_prepare(InputStream *ist, InputFilter *ifilter) · 739 unmodified lines
740740 return AVERROR(ENOMEM);
741741 ist->sub2video.last_pts = INT64_MIN;
742742 ist->sub2video.end_pts = INT64_MIN;
743+
744+ /* sub2video structure has been (re-)initialized.
745+ Mark it as such so that the system will be
746+ initialized with the first received heartbeat. */
747+ ist->sub2video.initialize = 1;
748+
743749 return 0;
744750 }
745751
@@ -1168,7 +1174,7 @@ int configure_filtergraph(FilterGraph *fg) · 422 unmodified lines
11681174 while (av_fifo_size(ist->sub2video.sub_queue)) {
11691175 AVSubtitle tmp;
11701176 av_fifo_generic_read(ist->sub2video.sub_queue, &tmp, sizeof(tmp), NULL);
1171− sub2video_update(ist, &tmp);
1177+ sub2video_update(ist, INT64_MIN, &tmp);
11721178 avsubtitle_free(&tmp);
11731179 }
11741180 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t