Imported Debian version 2.5.0~trusty1.1
[deb_ffmpeg.git] / ffmpeg / libavcodec / vorbis_parser.c
index 1e2cab3927a9cbbe9f03ece9e9b2f7702a661b4e..b99f11559897784b8e66f77d05238d099820809e 100644 (file)
  * Determines the duration for each packet.
  */
 
+#include "libavutil/log.h"
+
 #include "get_bits.h"
 #include "parser.h"
 #include "xiph.h"
-#include "vorbis_parser.h"
+#include "vorbis_parser_internal.h"
+
+static const AVClass vorbis_parser_class = {
+    .class_name = "Vorbis parser",
+    .item_name  = av_default_item_name,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
 
-static int parse_id_header(AVCodecContext *avctx, VorbisParseContext *s,
+static int parse_id_header(AVVorbisParseContext *s,
                            const uint8_t *buf, int buf_size)
 {
     /* Id header should be 30 bytes */
     if (buf_size < 30) {
-        av_log(avctx, AV_LOG_ERROR, "Id header is too short\n");
+        av_log(s, AV_LOG_ERROR, "Id header is too short\n");
         return AVERROR_INVALIDDATA;
     }
 
     /* make sure this is the Id header */
     if (buf[0] != 1) {
-        av_log(avctx, AV_LOG_ERROR, "Wrong packet type in Id header\n");
+        av_log(s, AV_LOG_ERROR, "Wrong packet type in Id header\n");
         return AVERROR_INVALIDDATA;
     }
 
     /* check for header signature */
     if (memcmp(&buf[1], "vorbis", 6)) {
-        av_log(avctx, AV_LOG_ERROR, "Invalid packet signature in Id header\n");
+        av_log(s, AV_LOG_ERROR, "Invalid packet signature in Id header\n");
         return AVERROR_INVALIDDATA;
     }
 
     if (!(buf[29] & 0x1)) {
-        av_log(avctx, AV_LOG_ERROR, "Invalid framing bit in Id header\n");
+        av_log(s, AV_LOG_ERROR, "Invalid framing bit in Id header\n");
         return AVERROR_INVALIDDATA;
     }
 
@@ -62,7 +70,7 @@ static int parse_id_header(AVCodecContext *avctx, VorbisParseContext *s,
     return 0;
 }
 
-static int parse_setup_header(AVCodecContext *avctx, VorbisParseContext *s,
+static int parse_setup_header(AVVorbisParseContext *s,
                               const uint8_t *buf, int buf_size)
 {
     GetBitContext gb, gb0;
@@ -72,25 +80,25 @@ static int parse_setup_header(AVCodecContext *avctx, VorbisParseContext *s,
 
     /* avoid overread */
     if (buf_size < 7) {
-        av_log(avctx, AV_LOG_ERROR, "Setup header is too short\n");
+        av_log(s, AV_LOG_ERROR, "Setup header is too short\n");
         return AVERROR_INVALIDDATA;
     }
 
     /* make sure this is the Setup header */
     if (buf[0] != 5) {
-        av_log(avctx, AV_LOG_ERROR, "Wrong packet type in Setup header\n");
+        av_log(s, AV_LOG_ERROR, "Wrong packet type in Setup header\n");
         return AVERROR_INVALIDDATA;
     }
 
     /* check for header signature */
     if (memcmp(&buf[1], "vorbis", 6)) {
-        av_log(avctx, AV_LOG_ERROR, "Invalid packet signature in Setup header\n");
+        av_log(s, AV_LOG_ERROR, "Invalid packet signature in Setup header\n");
         return AVERROR_INVALIDDATA;
     }
 
     /* reverse bytes so we can easily read backwards with get_bits() */
     if (!(rev_buf = av_malloc(buf_size))) {
-        av_log(avctx, AV_LOG_ERROR, "Out of memory\n");
+        av_log(s, AV_LOG_ERROR, "Out of memory\n");
         return AVERROR(ENOMEM);
     }
     for (i = 0; i < buf_size; i++)
@@ -105,7 +113,7 @@ static int parse_setup_header(AVCodecContext *avctx, VorbisParseContext *s,
         }
     }
     if (!got_framing_bit) {
-        av_log(avctx, AV_LOG_ERROR, "Invalid Setup header\n");
+        av_log(s, AV_LOG_ERROR, "Invalid Setup header\n");
         ret = AVERROR_INVALIDDATA;
         goto bad_header;
     }
@@ -132,7 +140,7 @@ static int parse_setup_header(AVCodecContext *avctx, VorbisParseContext *s,
         }
     }
     if (!got_mode_header) {
-        av_log(avctx, AV_LOG_ERROR, "Invalid Setup header\n");
+        av_log(s, AV_LOG_ERROR, "Invalid Setup header\n");
         ret = AVERROR_INVALIDDATA;
         goto bad_header;
     }
@@ -141,7 +149,7 @@ static int parse_setup_header(AVCodecContext *avctx, VorbisParseContext *s,
      * we may need to approach this the long way and parse the whole Setup
      * header, but I hope very much that it never comes to that. */
     if (last_mode_count > 2) {
-        avpriv_request_sample(avctx,
+        avpriv_request_sample(s,
                               "%d modes (either a false positive or a "
                               "sample from an unknown encoder)",
                               last_mode_count);
@@ -149,7 +157,7 @@ static int parse_setup_header(AVCodecContext *avctx, VorbisParseContext *s,
     /* We're limiting the mode count to 63 so that we know that the previous
      * block flag will be in the first packet byte. */
     if (last_mode_count > 63) {
-        av_log(avctx, AV_LOG_ERROR, "Unsupported mode count: %d\n",
+        av_log(s, AV_LOG_ERROR, "Unsupported mode count: %d\n",
                last_mode_count);
         ret = AVERROR_INVALIDDATA;
         goto bad_header;
@@ -173,26 +181,27 @@ bad_header:
     return ret;
 }
 
-int avpriv_vorbis_parse_extradata(AVCodecContext *avctx, VorbisParseContext *s)
+static int vorbis_parse_init(AVVorbisParseContext *s,
+                             const uint8_t *extradata, int extradata_size)
 {
     uint8_t *header_start[3];
     int header_len[3];
     int ret;
 
-    s->avctx = avctx;
+    s->class = &vorbis_parser_class;
     s->extradata_parsed = 1;
 
-    if ((ret = avpriv_split_xiph_headers(avctx->extradata,
-                                         avctx->extradata_size, 30,
+    if ((ret = avpriv_split_xiph_headers(extradata,
+                                         extradata_size, 30,
                                          header_start, header_len)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "Extradata corrupt.\n");
+        av_log(s, AV_LOG_ERROR, "Extradata corrupt.\n");
         return ret;
     }
 
-    if ((ret = parse_id_header(avctx, s, header_start[0], header_len[0])) < 0)
+    if ((ret = parse_id_header(s, header_start[0], header_len[0])) < 0)
         return ret;
 
-    if ((ret = parse_setup_header(avctx, s, header_start[2], header_len[2])) < 0)
+    if ((ret = parse_setup_header(s, header_start[2], header_len[2])) < 0)
         return ret;
 
     s->valid_extradata = 1;
@@ -201,8 +210,8 @@ int avpriv_vorbis_parse_extradata(AVCodecContext *avctx, VorbisParseContext *s)
     return 0;
 }
 
-int avpriv_vorbis_parse_frame_flags(VorbisParseContext *s, const uint8_t *buf,
-                                    int buf_size, int *flags)
+int av_vorbis_parse_frame_flags(AVVorbisParseContext *s, const uint8_t *buf,
+                                int buf_size, int *flags)
 {
     int duration = 0;
 
@@ -227,7 +236,7 @@ int avpriv_vorbis_parse_frame_flags(VorbisParseContext *s, const uint8_t *buf,
             return 0;
 
 bad_packet:
-            av_log(s->avctx, AV_LOG_ERROR, "Invalid packet\n");
+            av_log(s, AV_LOG_ERROR, "Invalid packet\n");
             return AVERROR_INVALIDDATA;
         }
         if (s->mode_count == 1)
@@ -235,7 +244,7 @@ bad_packet:
         else
             mode = (buf[0] & s->mode_mask) >> 1;
         if (mode >= s->mode_count) {
-            av_log(s->avctx, AV_LOG_ERROR, "Invalid mode in packet\n");
+            av_log(s, AV_LOG_ERROR, "Invalid mode in packet\n");
             return AVERROR_INVALIDDATA;
         }
         if(s->mode_blocksize[mode]){
@@ -250,19 +259,68 @@ bad_packet:
     return duration;
 }
 
-int avpriv_vorbis_parse_frame(VorbisParseContext *s, const uint8_t *buf,
-                              int buf_size)
+int av_vorbis_parse_frame(AVVorbisParseContext *s, const uint8_t *buf,
+                          int buf_size)
 {
-    return avpriv_vorbis_parse_frame_flags(s, buf, buf_size, NULL);
+    return av_vorbis_parse_frame_flags(s, buf, buf_size, NULL);
 }
 
-void avpriv_vorbis_parse_reset(VorbisParseContext *s)
+void av_vorbis_parse_reset(AVVorbisParseContext *s)
 {
     if (s->valid_extradata)
         s->previous_blocksize = s->blocksize[0];
 }
 
+void av_vorbis_parse_free(AVVorbisParseContext **s)
+{
+    av_freep(s);
+}
+
+AVVorbisParseContext *av_vorbis_parse_init(const uint8_t *extradata,
+                                           int extradata_size)
+{
+    AVVorbisParseContext *s = av_mallocz(sizeof(*s));
+    int ret;
+
+    if (!s)
+        return NULL;
+
+    ret = vorbis_parse_init(s, extradata, extradata_size);
+    if (ret < 0) {
+        av_vorbis_parse_free(&s);
+        return NULL;
+    }
+
+    return s;
+}
+
+#if LIBAVCODEC_VERSION_MAJOR < 57
+int avpriv_vorbis_parse_extradata(AVCodecContext *avctx, AVVorbisParseContext *s)
+{
+    return vorbis_parse_init(s, avctx->extradata, avctx->extradata_size);
+}
+void avpriv_vorbis_parse_reset(AVVorbisParseContext *s)
+{
+    av_vorbis_parse_reset(s);
+}
+int avpriv_vorbis_parse_frame(AVVorbisParseContext *s, const uint8_t *buf,
+                              int buf_size)
+{
+    return av_vorbis_parse_frame(s, buf, buf_size);
+}
+int avpriv_vorbis_parse_frame_flags(AVVorbisParseContext *s, const uint8_t *buf,
+                                    int buf_size, int *flags)
+{
+    return av_vorbis_parse_frame_flags(s, buf, buf_size, flags);
+}
+#endif
+
 #if CONFIG_VORBIS_PARSER
+
+typedef struct VorbisParseContext {
+    AVVorbisParseContext *vp;
+} VorbisParseContext;
+
 static int vorbis_parse(AVCodecParserContext *s1, AVCodecContext *avctx,
                         const uint8_t **poutbuf, int *poutbuf_size,
                         const uint8_t *buf, int buf_size)
@@ -270,11 +328,13 @@ static int vorbis_parse(AVCodecParserContext *s1, AVCodecContext *avctx,
     VorbisParseContext *s = s1->priv_data;
     int duration;
 
-    if (!s->extradata_parsed && avctx->extradata && avctx->extradata_size)
-        if (avpriv_vorbis_parse_extradata(avctx, s))
-            goto end;
+    if (!s->vp && avctx->extradata && avctx->extradata_size) {
+        s->vp = av_vorbis_parse_init(avctx->extradata, avctx->extradata_size);
+    }
+    if (!s->vp)
+        goto end;
 
-    if ((duration = avpriv_vorbis_parse_frame(s, buf, buf_size)) >= 0)
+    if ((duration = av_vorbis_parse_frame(s->vp, buf, buf_size)) >= 0)
         s1->duration = duration;
 
 end:
@@ -285,9 +345,16 @@ end:
     return buf_size;
 }
 
+static void vorbis_parser_close(AVCodecParserContext *ctx)
+{
+    VorbisParseContext *s = ctx->priv_data;
+    av_vorbis_parse_free(&s->vp);
+}
+
 AVCodecParser ff_vorbis_parser = {
     .codec_ids      = { AV_CODEC_ID_VORBIS },
     .priv_data_size = sizeof(VorbisParseContext),
     .parser_parse   = vorbis_parse,
+    .parser_close   = vorbis_parser_close,
 };
 #endif /* CONFIG_VORBIS_PARSER */