avformat/hlsenc: closed caption tags in the master playlist
[ffmpeg.git] / libavfilter / vf_detelecine.c
index f5ae350..0d1e2f2 100644 (file)
 #include "internal.h"
 #include "video.h"
 
-typedef struct {
+typedef struct DetelecineContext {
     const AVClass *class;
     int first_field;
     char *pattern;
     int start_frame;
+    int init_len;
     unsigned int pattern_pos;
     unsigned int nskip_fields;
     int64_t start_time;
@@ -49,7 +50,7 @@ typedef struct {
     int planeheight[4];
     int stride[4];
 
-    AVFrame *frame;
+    AVFrame *frame[2];
     AVFrame *temp;
 } DetelecineContext;
 
@@ -74,6 +75,7 @@ static av_cold int init(AVFilterContext *ctx)
     DetelecineContext *s = ctx->priv;
     const char *p;
     int max = 0;
+    int sum = 0;
 
     if (!strlen(s->pattern)) {
         av_log(ctx, AV_LOG_ERROR, "No pattern provided.\n");
@@ -86,14 +88,21 @@ static av_cold int init(AVFilterContext *ctx)
             return AVERROR_INVALIDDATA;
         }
 
+        sum += *p - '0';
         max = FFMAX(*p - '0', max);
         s->pts.num += *p - '0';
         s->pts.den += 2;
     }
 
+    if (s->start_frame >= sum) {
+        av_log(ctx, AV_LOG_ERROR, "Provided start_frame is too big.\n");
+        return AVERROR_INVALIDDATA;
+    }
+
     s->nskip_fields = 0;
     s->pattern_pos = 0;
     s->start_time = AV_NOPTS_VALUE;
+    s->init_len = 0;
 
     if (s->start_frame != 0) {
         int nfields = 0;
@@ -101,7 +110,7 @@ static av_cold int init(AVFilterContext *ctx)
             nfields += *p - '0';
             s->pattern_pos++;
             if (nfields >= 2*s->start_frame) {
-                s->nskip_fields = nfields - 2*s->start_frame;
+                s->init_len = nfields - 2*s->start_frame;
                 break;
             }
         }
@@ -140,8 +149,12 @@ static int config_input(AVFilterLink *inlink)
     if (!s->temp)
         return AVERROR(ENOMEM);
 
-    s->frame = ff_get_video_buffer(inlink, inlink->w, inlink->h);
-    if (!s->frame)
+    s->frame[0] = ff_get_video_buffer(inlink, inlink->w, inlink->h);
+    if (!s->frame[0])
+        return AVERROR(ENOMEM);
+
+    s->frame[1] = ff_get_video_buffer(inlink, inlink->w, inlink->h);
+    if (!s->frame[1])
         return AVERROR(ENOMEM);
 
     if ((ret = av_image_fill_linesizes(s->stride, inlink->format, inlink->w)) < 0)
@@ -207,6 +220,8 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
     }
 
     if (s->nskip_fields == 0) {
+        len = s->init_len;
+        s->init_len = 0;
         while(!len && s->pattern[s->pattern_pos]) {
             len = s->pattern[s->pattern_pos] - '0';
             s->pattern_pos++;
@@ -220,18 +235,39 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
             return 0;
         }
 
+        if (len == 1 && s->occupied) {
+            s->occupied = 0;
+            // output THIS image as-is
+            for (i = 0; i < s->nb_planes; i++)
+                av_image_copy_plane(s->frame[out]->data[i], s->frame[out]->linesize[i],
+                                    s->temp->data[i], s->temp->linesize[i],
+                                    s->stride[i],
+                                    s->planeheight[i]);
+            len = 0;
+            while(!len && s->pattern[s->pattern_pos]) {
+                len = s->pattern[s->pattern_pos] - '0';
+                s->pattern_pos++;
+            }
+
+            if (!s->pattern[s->pattern_pos])
+                s->pattern_pos = 0;
+
+            s->occupied = 0;
+            ++out;
+        }
+
         if (s->occupied) {
             for (i = 0; i < s->nb_planes; i++) {
                 // fill in the EARLIER field from the new pic
-                av_image_copy_plane(s->frame->data[i] + s->frame->linesize[i] * s->first_field,
-                                    s->frame->linesize[i] * 2,
+                av_image_copy_plane(s->frame[out]->data[i] + s->frame[out]->linesize[i] * s->first_field,
+                                    s->frame[out]->linesize[i] * 2,
                                     inpicref->data[i] + inpicref->linesize[i] * s->first_field,
                                     inpicref->linesize[i] * 2,
                                     s->stride[i],
                                     (s->planeheight[i] - s->first_field + 1) / 2);
                 // fill in the LATER field from the buffered pic
-                av_image_copy_plane(s->frame->data[i] + s->frame->linesize[i] * !s->first_field,
-                                    s->frame->linesize[i] * 2,
+                av_image_copy_plane(s->frame[out]->data[i] + s->frame[out]->linesize[i] * !s->first_field,
+                                    s->frame[out]->linesize[i] * 2,
                                     s->temp->data[i] + s->temp->linesize[i] * !s->first_field,
                                     s->temp->linesize[i] * 2,
                                     s->stride[i],
@@ -248,34 +284,36 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
                 }
                 s->occupied = 1;
             }
-            out = 1;
+            ++out;
             len = (len >= 3) ? len - 3 : 0;
         } else {
             if (len >= 2) {
                 // output THIS image as-is
                 for (i = 0; i < s->nb_planes; i++)
-                    av_image_copy_plane(s->frame->data[i], s->frame->linesize[i],
+                    av_image_copy_plane(s->frame[out]->data[i], s->frame[out]->linesize[i],
                                         inpicref->data[i], inpicref->linesize[i],
                                         s->stride[i],
                                         s->planeheight[i]);
                 len -= 2;
-                out = 1;
+                ++out;
             } else if (len == 1) {
-                // fill in the EARLIER field from the new pic
-                for (i = 0; i < s->nb_planes; i++) {
-                    av_image_copy_plane(s->frame->data[i] +
-                                        s->frame->linesize[i] * s->first_field,
-                                        s->frame->linesize[i] * 2,
-                                        inpicref->data[i] +
-                                        inpicref->linesize[i] * s->first_field,
-                                        inpicref->linesize[i] * 2, s->stride[i],
-                                        (s->planeheight[i] - s->first_field + 1) / 2);
-                 }
+                // output THIS image as-is
+                for (i = 0; i < s->nb_planes; i++)
+                    av_image_copy_plane(s->frame[out]->data[i], s->frame[out]->linesize[i],
+                                        inpicref->data[i], inpicref->linesize[i],
+                                        s->stride[i],
+                                        s->planeheight[i]);
 
-                // TODO: not sure about the other field
+                for (i = 0; i < s->nb_planes; i++) {
+                    av_image_copy_plane(s->temp->data[i], s->temp->linesize[i],
+                                        inpicref->data[i], inpicref->linesize[i],
+                                        s->stride[i],
+                                        s->planeheight[i]);
+                }
+                s->occupied = 1;
 
                 len--;
-                out = 1;
+                ++out;
             }
         }
 
@@ -287,8 +325,8 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
     }
     s->nskip_fields = len;
 
-    if (out) {
-        AVFrame *frame = av_frame_clone(s->frame);
+    for (i = 0; i < out; ++i) {
+        AVFrame *frame = av_frame_clone(s->frame[i]);
 
         if (!frame) {
             av_frame_free(&inpicref);
@@ -297,7 +335,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
 
         av_frame_copy_props(frame, inpicref);
         frame->pts = ((s->start_time == AV_NOPTS_VALUE) ? 0 : s->start_time) +
-                     av_rescale(outlink->frame_count, s->ts_unit.num,
+                     av_rescale(outlink->frame_count_in, s->ts_unit.num,
                                 s->ts_unit.den);
         ret = ff_filter_frame(outlink, frame);
     }
@@ -312,7 +350,8 @@ static av_cold void uninit(AVFilterContext *ctx)
     DetelecineContext *s = ctx->priv;
 
     av_frame_free(&s->temp);
-    av_frame_free(&s->frame);
+    av_frame_free(&s->frame[0]);
+    av_frame_free(&s->frame[1]);
 }
 
 static const AVFilterPad detelecine_inputs[] = {