]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/vp9_superframe_split_bsf.c
avcodec/avcodec: Remove unnecessary forward declaration
[ffmpeg] / libavcodec / vp9_superframe_split_bsf.c
index 0d311238742b5770a52d14f94af7784f0f4bd9c8..ed0444561acd4a9e28248075e4a3c6f70b338211 100644 (file)
@@ -1,18 +1,18 @@
 /*
- * This file is part of Libav.
+ * This file is part of FFmpeg.
  *
- * Libav is free software; you can redistribute it and/or
+ * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
- * Libav is distributed in the hope that it will be useful,
+ * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with Libav; if not, write to the Free Software
+ * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 
 #include <stddef.h>
 
-#include "avcodec.h"
 #include "bsf.h"
-#include "bitstream.h"
+#include "bsf_internal.h"
 #include "bytestream.h"
+#include "get_bits.h"
 
 typedef struct VP9SFSplitContext {
     AVPacket *buffer_pkt;
@@ -43,10 +43,10 @@ static int vp9_superframe_split_filter(AVBSFContext *ctx, AVPacket *out)
     VP9SFSplitContext *s = ctx->priv_data;
     AVPacket *in;
     int i, j, ret, marker;
-    int is_superframe = !!s->buffer_pkt;
+    int is_superframe = !!s->buffer_pkt->data;
 
-    if (!s->buffer_pkt) {
-        ret = ff_bsf_get_packet(ctx, &s->buffer_pkt);
+    if (!s->buffer_pkt->data) {
+        ret = ff_bsf_get_packet_ref(ctx, s->buffer_pkt);
         if (ret < 0)
             return ret;
         in = s->buffer_pkt;
@@ -59,7 +59,7 @@ static int vp9_superframe_split_filter(AVBSFContext *ctx, AVPacket *out)
 
             if (in->size >= idx_size && in->data[in->size - idx_size] == marker) {
                 GetByteContext bc;
-                int total_size = 0;
+                int64_t total_size = 0;
 
                 bytestream2_init(&bc, in->data + in->size + 1 - idx_size,
                                  nb_frames * length_size);
@@ -70,7 +70,7 @@ static int vp9_superframe_split_filter(AVBSFContext *ctx, AVPacket *out)
                         frame_size |= bytestream2_get_byte(&bc) << (j * 8);
 
                     total_size += frame_size;
-                    if (total_size > in->size - idx_size) {
+                    if (frame_size < 0 || total_size > in->size - idx_size) {
                         av_log(ctx, AV_LOG_ERROR,
                                "Invalid frame size in a superframe: %d\n", frame_size);
                         ret = AVERROR(EINVAL);
@@ -87,7 +87,7 @@ static int vp9_superframe_split_filter(AVBSFContext *ctx, AVPacket *out)
     }
 
     if (is_superframe) {
-        BitstreamContext bc;
+        GetBitContext gb;
         int profile, invisible = 0;
 
         ret = av_packet_ref(out, s->buffer_pkt);
@@ -101,20 +101,20 @@ static int vp9_superframe_split_filter(AVBSFContext *ctx, AVPacket *out)
         s->next_frame++;
 
         if (s->next_frame >= s->nb_frames)
-            av_packet_free(&s->buffer_pkt);
+            av_packet_unref(s->buffer_pkt);
 
-        ret = bitstream_init8(&bc, out->data, out->size);
+        ret = init_get_bits8(&gb, out->data, out->size);
         if (ret < 0)
             goto fail;
 
-        bitstream_read(&bc, 2); // frame_marker
-        profile  = bitstream_read(&bc, 1);
-        profile |= bitstream_read(&bc, 1) << 1;
+        get_bits(&gb, 2); // frame_marker
+        profile  = get_bits1(&gb);
+        profile |= get_bits1(&gb) << 1;
         if (profile == 3)
-            bitstream_read(&bc, 1);
-        if (!bitstream_read(&bc, 1)) {
-            bitstream_read(&bc, 1);
-            invisible = !bitstream_read(&bc, 1);
+            get_bits1(&gb);
+        if (!get_bits1(&gb)) {
+            get_bits1(&gb);
+            invisible = !get_bits1(&gb);
         }
 
         if (invisible)
@@ -122,19 +122,31 @@ static int vp9_superframe_split_filter(AVBSFContext *ctx, AVPacket *out)
 
     } else {
         av_packet_move_ref(out, s->buffer_pkt);
-        av_packet_free(&s->buffer_pkt);
     }
 
     return 0;
 fail:
-    av_packet_free(&s->buffer_pkt);
+    if (ret < 0)
+        av_packet_unref(out);
+    av_packet_unref(s->buffer_pkt);
     return ret;
 }
 
+static int vp9_superframe_split_init(AVBSFContext *ctx)
+{
+    VP9SFSplitContext *s = ctx->priv_data;
+
+    s->buffer_pkt = av_packet_alloc();
+    if (!s->buffer_pkt)
+        return AVERROR(ENOMEM);
+
+    return 0;
+}
+
 static void vp9_superframe_split_flush(AVBSFContext *ctx)
 {
     VP9SFSplitContext *s = ctx->priv_data;
-    av_packet_free(&s->buffer_pkt);
+    av_packet_unref(s->buffer_pkt);
 }
 
 static void vp9_superframe_split_uninit(AVBSFContext *ctx)
@@ -146,6 +158,7 @@ static void vp9_superframe_split_uninit(AVBSFContext *ctx)
 const AVBitStreamFilter ff_vp9_superframe_split_bsf = {
     .name = "vp9_superframe_split",
     .priv_data_size = sizeof(VP9SFSplitContext),
+    .init           = vp9_superframe_split_init,
     .flush          = vp9_superframe_split_flush,
     .close          = vp9_superframe_split_uninit,
     .filter         = vp9_superframe_split_filter,