]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/rtpdec_vp8.c
hlsenc: Support recovery from an already present playlist
[ffmpeg] / libavformat / rtpdec_vp8.c
index c1bffaac0dd7c64b460e06a0a2e11aaa67123ec7..b7f9939d05436b430ded941ae763b5aafbb069d1 100644 (file)
@@ -29,6 +29,7 @@
 
 #include "libavcodec/bytestream.h"
 
+#include "avio_internal.h"
 #include "rtpdec_formats.h"
 
 struct PayloadContext {
@@ -45,29 +46,19 @@ struct PayloadContext {
     int          prev_pictureid;
     int          broken_frame;
     /* If sequence_dirty is set, we have lost some data (critical or
-     * non-critical) and decoding will have some sort of artefacts, and
+     * non-critical) and decoding will have some sort of artifacts, and
      * we thus should request a new keyframe.
      */
     int          sequence_dirty;
     int          got_keyframe;
 };
 
-static void vp8_free_buffer(PayloadContext *vp8)
-{
-    uint8_t *tmp;
-    if (!vp8->data)
-        return;
-    avio_close_dyn_buf(vp8->data, &tmp);
-    av_free(tmp);
-    vp8->data = NULL;
-}
-
 static int vp8_broken_sequence(AVFormatContext *ctx, PayloadContext *vp8,
                                const char *msg)
 {
     vp8->sequence_ok = 0;
     av_log(ctx, AV_LOG_WARNING, "%s", msg);
-    vp8_free_buffer(vp8);
+    ffio_free_dyn_buf(&vp8->data);
     return AVERROR(EAGAIN);
 }
 
@@ -82,7 +73,7 @@ static int vp8_handle_packet(AVFormatContext *ctx, PayloadContext *vp8,
         keyidx_present = 0;
     int pictureid = -1, pictureid_mask = 0;
     int returned_old_frame = 0;
-    uint32_t old_timestamp;
+    uint32_t old_timestamp = 0;
 
     if (!buf) {
         if (vp8->data) {
@@ -150,7 +141,7 @@ static int vp8_handle_packet(AVFormatContext *ctx, PayloadContext *vp8,
         int res;
         int non_key = buf[0] & 0x01;
         if (!non_key) {
-            vp8_free_buffer(vp8);
+            ffio_free_dyn_buf(&vp8->data);
             // Keyframe, decoding ok again
             vp8->sequence_ok = 1;
             vp8->sequence_dirty = 0;
@@ -200,13 +191,12 @@ static int vp8_handle_packet(AVFormatContext *ctx, PayloadContext *vp8,
                     int ret = ff_rtp_finalize_packet(pkt, &vp8->data, st->index);
                     if (ret < 0)
                         return ret;
-                    pkt->size = vp8->first_part_size;
                     pkt->flags |= AV_PKT_FLAG_CORRUPT;
                     returned_old_frame = 1;
                     old_timestamp = vp8->timestamp;
                 } else {
                     // Shouldn't happen
-                    vp8_free_buffer(vp8);
+                    ffio_free_dyn_buf(&vp8->data);
                 }
             }
         }
@@ -225,11 +215,8 @@ static int vp8_handle_packet(AVFormatContext *ctx, PayloadContext *vp8,
 
         if (vp8->timestamp != *timestamp) {
             // Missed the start of the new frame, sequence broken
-            vp8->sequence_ok = 0;
-            av_log(ctx, AV_LOG_WARNING,
-                   "Received no start marker; dropping frame\n");
-            vp8_free_buffer(vp8);
-            return AVERROR(EAGAIN);
+            return vp8_broken_sequence(ctx, vp8,
+                                       "Received no start marker; dropping frame\n");
         }
 
         if (seq != expected_seq) {
@@ -250,7 +237,8 @@ static int vp8_handle_packet(AVFormatContext *ctx, PayloadContext *vp8,
         return vp8_broken_sequence(ctx, vp8, "Received no start marker\n");
 
     vp8->prev_seq = seq;
-    avio_write(vp8->data, buf, len);
+    if (!vp8->broken_frame)
+        avio_write(vp8->data, buf, len);
 
     if (returned_old_frame) {
         *timestamp = old_timestamp;
@@ -262,29 +250,25 @@ static int vp8_handle_packet(AVFormatContext *ctx, PayloadContext *vp8,
         ret = ff_rtp_finalize_packet(pkt, &vp8->data, st->index);
         if (ret < 0)
             return ret;
-        if (vp8->broken_frame)
-            pkt->size = vp8->first_part_size;
         if (vp8->sequence_dirty)
             pkt->flags |= AV_PKT_FLAG_CORRUPT;
+        if (vp8->is_keyframe)
+            pkt->flags |= AV_PKT_FLAG_KEY;
         return 0;
     }
 
     return AVERROR(EAGAIN);
 }
 
-static PayloadContext *vp8_new_context(void)
+static av_cold int vp8_init(AVFormatContext *s, int st_index, PayloadContext *vp8)
 {
-    PayloadContext *vp8 = av_mallocz(sizeof(PayloadContext));
-    if (!vp8)
-        return NULL;
     vp8->sequence_ok = 1;
-    return vp8;
+    return 0;
 }
 
-static void vp8_free_context(PayloadContext *vp8)
+static void vp8_close_context(PayloadContext *vp8)
 {
-    vp8_free_buffer(vp8);
-    av_free(vp8);
+    ffio_free_dyn_buf(&vp8->data);
 }
 
 static int vp8_need_keyframe(PayloadContext *vp8)
@@ -296,8 +280,9 @@ RTPDynamicProtocolHandler ff_vp8_dynamic_handler = {
     .enc_name       = "VP8",
     .codec_type     = AVMEDIA_TYPE_VIDEO,
     .codec_id       = AV_CODEC_ID_VP8,
-    .alloc          = vp8_new_context,
-    .free           = vp8_free_context,
+    .priv_data_size = sizeof(PayloadContext),
+    .init           = vp8_init,
+    .close          = vp8_close_context,
     .parse_packet   = vp8_handle_packet,
     .need_keyframe  = vp8_need_keyframe,
 };