changeset 4578:c05d167a4fe2 libavformat

Use a shared function to validate FLAC extradata.
author jbr
date Thu, 26 Feb 2009 02:29:24 +0000
parents 1cc2041c2e03
children e5fca5c56cb2
files flacenc.c matroskaenc.c oggenc.c
diffstat 3 files changed, 33 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/flacenc.c	Thu Feb 26 02:21:43 2009 +0000
+++ b/flacenc.c	Thu Feb 26 02:29:24 2009 +0000
@@ -19,6 +19,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavcodec/flac.h"
 #include "avformat.h"
 
 static int flac_write_header(struct AVFormatContext *s)
@@ -26,9 +27,15 @@
     static const uint8_t header[8] = {
         0x66, 0x4C, 0x61, 0x43, 0x80, 0x00, 0x00, 0x22
     };
-    uint8_t *streaminfo = s->streams[0]->codec->extradata;
+    AVCodecContext *codec = s->streams[0]->codec;
+    uint8_t *streaminfo;
     int len = s->streams[0]->codec->extradata_size;
-    if(streaminfo != NULL && len > 0) {
+    enum FLACExtradataFormat format;
+
+    if (!ff_flac_is_extradata_valid(codec, &format, &streaminfo))
+        return -1;
+
+    if (format == FLAC_EXTRADATA_FORMAT_STREAMINFO) {
         put_buffer(s->pb, header, 8);
         put_buffer(s->pb, streaminfo, len);
     }
@@ -38,16 +45,22 @@
 static int flac_write_trailer(struct AVFormatContext *s)
 {
     ByteIOContext *pb = s->pb;
-    uint8_t *streaminfo = s->streams[0]->codec->extradata;
-    int len = s->streams[0]->codec->extradata_size;
+    uint8_t *streaminfo;
+    enum FLACExtradataFormat format;
     int64_t file_size;
 
-    if (streaminfo && len > 0 && !url_is_streamed(s->pb)) {
+    if (!ff_flac_is_extradata_valid(s->streams[0]->codec, &format, &streaminfo))
+        return -1;
+
+    if (!url_is_streamed(pb)) {
+        /* rewrite the STREAMINFO header block data */
         file_size = url_ftell(pb);
         url_fseek(pb, 8, SEEK_SET);
-        put_buffer(pb, streaminfo, len);
+        put_buffer(pb, streaminfo, FLAC_STREAMINFO_SIZE);
         url_fseek(pb, file_size, SEEK_SET);
         put_flush_packet(pb);
+    } else {
+        av_log(s, AV_LOG_WARNING, "unable to rewrite FLAC header.\n");
     }
     return 0;
 }
--- a/matroskaenc.c	Thu Feb 26 02:21:43 2009 +0000
+++ b/matroskaenc.c	Thu Feb 26 02:29:24 2009 +0000
@@ -28,6 +28,7 @@
 #include "libavutil/md5.h"
 #include "libavcodec/xiph.h"
 #include "libavcodec/mpeg4audio.h"
+#include "libavcodec/flac.h"
 
 typedef struct ebml_master {
     int64_t         pos;                ///< absolute offset in the file where the master's elements start
@@ -420,23 +421,20 @@
     return 0;
 }
 
-#define FLAC_STREAMINFO_SIZE 34
-
 static int put_flac_codecpriv(AVFormatContext *s, ByteIOContext *pb, AVCodecContext *codec)
 {
-    // if the extradata_size is greater than FLAC_STREAMINFO_SIZE,
-    // assume that it's in Matroska format already
-    if (codec->extradata_size < FLAC_STREAMINFO_SIZE) {
+    uint8_t *streaminfo;
+    enum FLACExtradataFormat format;
+
+    if (!ff_flac_is_extradata_valid(codec, &format, &streaminfo)) {
         av_log(s, AV_LOG_ERROR, "Invalid FLAC extradata\n");
         return -1;
-    } else if (codec->extradata_size == FLAC_STREAMINFO_SIZE) {
+    }
+    if (format == FLAC_EXTRADATA_FORMAT_STREAMINFO) {
         // only the streaminfo packet
         put_buffer(pb, "fLaC", 4);
         put_byte(pb, 0x80);
         put_be24(pb, FLAC_STREAMINFO_SIZE);
-    } else if(memcmp("fLaC", codec->extradata, 4)) {
-        av_log(s, AV_LOG_ERROR, "Invalid FLAC extradata\n");
-        return -1;
     }
     put_buffer(pb, codec->extradata, codec->extradata_size);
     return 0;
--- a/oggenc.c	Thu Feb 26 02:21:43 2009 +0000
+++ b/oggenc.c	Thu Feb 26 02:29:24 2009 +0000
@@ -22,6 +22,7 @@
 #include "libavutil/crc.h"
 #include "libavcodec/xiph.h"
 #include "libavcodec/bytestream.h"
+#include "libavcodec/flac.h"
 #include "avformat.h"
 #include "internal.h"
 
@@ -82,12 +83,14 @@
     return size;
 }
 
-static int ogg_build_flac_headers(const uint8_t *extradata, int extradata_size,
+static int ogg_build_flac_headers(AVCodecContext *avctx,
                                   OGGStreamContext *oggstream, int bitexact)
 {
     const char *vendor = bitexact ? "ffmpeg" : LIBAVFORMAT_IDENT;
+    enum FLACExtradataFormat format;
+    uint8_t *streaminfo;
     uint8_t *p;
-    if (extradata_size != 34)
+    if (!ff_flac_is_extradata_valid(avctx, &format, &streaminfo))
         return -1;
     oggstream->header_len[0] = 51;
     oggstream->header[0] = av_mallocz(51); // per ogg flac specs
@@ -100,7 +103,7 @@
     bytestream_put_buffer(&p, "fLaC", 4);
     bytestream_put_byte(&p, 0x00); // streaminfo
     bytestream_put_be24(&p, 34);
-    bytestream_put_buffer(&p, extradata, 34);
+    bytestream_put_buffer(&p, streaminfo, FLAC_STREAMINFO_SIZE);
     oggstream->header_len[1] = 1+3+4+strlen(vendor)+4;
     oggstream->header[1] = av_mallocz(oggstream->header_len[1]);
     p = oggstream->header[1];
@@ -136,7 +139,7 @@
         oggstream = av_mallocz(sizeof(*oggstream));
         st->priv_data = oggstream;
         if (st->codec->codec_id == CODEC_ID_FLAC) {
-            if (ogg_build_flac_headers(st->codec->extradata, st->codec->extradata_size,
+            if (ogg_build_flac_headers(st->codec,
                                        oggstream, st->codec->flags & CODEC_FLAG_BITEXACT) < 0) {
                 av_log(s, AV_LOG_ERROR, "Extradata corrupted\n");
                 av_freep(&st->priv_data);