Replace all remaining occurrences of AVERROR_NOMEM with
[ffmpeg.git] / libavformat / oggenc.c
index 85ec628..36ea21c 100644 (file)
 #include "libavutil/crc.h"
 #include "libavcodec/xiph.h"
 #include "libavcodec/bytestream.h"
+#include "libavcodec/flac.h"
 #include "avformat.h"
+#include "internal.h"
+#include "vorbiscomment.h"
 
 typedef struct {
     int64_t duration;
@@ -34,11 +37,12 @@ typedef struct {
     int64_t last_kf_pts;
     int vrev;
     int eos;
+    unsigned packet_count; ///< number of packet buffered
 } OGGStreamContext;
 
-static void ogg_update_checksum(AVFormatContext *s, offset_t crc_offset)
+static void ogg_update_checksum(AVFormatContext *s, int64_t crc_offset)
 {
-    offset_t pos = url_ftell(s->pb);
+    int64_t pos = url_ftell(s->pb);
     uint32_t checksum = get_checksum(s->pb);
     url_fseek(s->pb, crc_offset, SEEK_SET);
     put_be32(s->pb, checksum);
@@ -49,7 +53,7 @@ static int ogg_write_page(AVFormatContext *s, const uint8_t *data, int size,
                           int64_t granule, int stream_index, int flags)
 {
     OGGStreamContext *oggstream = s->streams[stream_index]->priv_data;
-    offset_t crc_offset;
+    int64_t crc_offset;
     int page_segments, i;
 
     if (size >= 255*255) {
@@ -58,7 +62,7 @@ static int ogg_write_page(AVFormatContext *s, const uint8_t *data, int size,
     } else if (oggstream->eos)
         flags |= 4;
 
-    page_segments = FFMIN((size/255)+!!size, 255);
+    page_segments = FFMIN(size/255 + 1, 255);
 
     init_checksum(s->pb, ff_crc04C11DB7_update, 0);
     put_tag(s->pb, "OggS");
@@ -72,25 +76,52 @@ static int ogg_write_page(AVFormatContext *s, const uint8_t *data, int size,
     put_byte(s->pb, page_segments);
     for (i = 0; i < page_segments-1; i++)
         put_byte(s->pb, 255);
-    if (size) {
-        put_byte(s->pb, size - (page_segments-1)*255);
-        put_buffer(s->pb, data, size);
-    }
+    put_byte(s->pb, size - (page_segments-1)*255);
+    put_buffer(s->pb, data, size);
+
     ogg_update_checksum(s, crc_offset);
     put_flush_packet(s->pb);
     return size;
 }
 
-static int ogg_build_flac_headers(const uint8_t *extradata, int extradata_size,
-                                  OGGStreamContext *oggstream, int bitexact)
+static uint8_t *ogg_write_vorbiscomment(int offset, int bitexact,
+                                        int *header_len, AVMetadata *m)
 {
     const char *vendor = bitexact ? "ffmpeg" : LIBAVFORMAT_IDENT;
+    int size;
+    uint8_t *p, *p0;
+    unsigned int count;
+
+    size = offset + ff_vorbiscomment_length(m, vendor, &count);
+    p = av_mallocz(size);
+    if (!p)
+        return NULL;
+    p0 = p;
+
+    p += offset;
+    ff_vorbiscomment_write(&p, m, vendor, count);
+
+    *header_len = size;
+    return p0;
+}
+
+static int ogg_build_flac_headers(AVCodecContext *avctx,
+                                  OGGStreamContext *oggstream, int bitexact,
+                                  AVMetadata *m)
+{
+    enum FLACExtradataFormat format;
+    uint8_t *streaminfo;
     uint8_t *p;
-    if (extradata_size != 34)
+
+    if (!ff_flac_is_extradata_valid(avctx, &format, &streaminfo))
         return -1;
+
+    // first packet: STREAMINFO
     oggstream->header_len[0] = 51;
     oggstream->header[0] = av_mallocz(51); // per ogg flac specs
     p = oggstream->header[0];
+    if (!p)
+        return AVERROR(ENOMEM);
     bytestream_put_byte(&p, 0x7F);
     bytestream_put_buffer(&p, "FLAC", 4);
     bytestream_put_byte(&p, 1); // major version
@@ -99,15 +130,45 @@ static int ogg_build_flac_headers(const uint8_t *extradata, int extradata_size,
     bytestream_put_buffer(&p, "fLaC", 4);
     bytestream_put_byte(&p, 0x00); // streaminfo
     bytestream_put_be24(&p, 34);
-    bytestream_put_buffer(&p, extradata, 34);
-    oggstream->header_len[1] = 1+3+4+strlen(vendor)+4;
-    oggstream->header[1] = av_mallocz(oggstream->header_len[1]);
-    p = oggstream->header[1];
+    bytestream_put_buffer(&p, streaminfo, FLAC_STREAMINFO_SIZE);
+
+    // second packet: VorbisComment
+    p = ogg_write_vorbiscomment(4, bitexact, &oggstream->header_len[1], m);
+    if (!p)
+        return AVERROR(ENOMEM);
+    oggstream->header[1] = p;
     bytestream_put_byte(&p, 0x84); // last metadata block and vorbis comment
     bytestream_put_be24(&p, oggstream->header_len[1] - 4);
-    bytestream_put_le32(&p, strlen(vendor));
-    bytestream_put_buffer(&p, vendor, strlen(vendor));
-    bytestream_put_le32(&p, 0); // user comment list length
+
+    return 0;
+}
+
+#define SPEEX_HEADER_SIZE 80
+
+static int ogg_build_speex_headers(AVCodecContext *avctx,
+                                   OGGStreamContext *oggstream, int bitexact,
+                                   AVMetadata *m)
+{
+    uint8_t *p;
+
+    if (avctx->extradata_size < SPEEX_HEADER_SIZE)
+        return -1;
+
+    // first packet: Speex header
+    p = av_mallocz(SPEEX_HEADER_SIZE);
+    if (!p)
+        return AVERROR(ENOMEM);
+    oggstream->header[0] = p;
+    oggstream->header_len[0] = SPEEX_HEADER_SIZE;
+    bytestream_put_buffer(&p, avctx->extradata, SPEEX_HEADER_SIZE);
+    AV_WL32(&oggstream->header[0][68], 0);  // set extra_headers to 0
+
+    // second packet: VorbisComment
+    p = ogg_write_vorbiscomment(0, bitexact, &oggstream->header_len[1], m);
+    if (!p)
+        return AVERROR(ENOMEM);
+    oggstream->header[1] = p;
+
     return 0;
 }
 
@@ -117,12 +178,13 @@ static int ogg_write_header(AVFormatContext *s)
     int i, j;
     for (i = 0; i < s->nb_streams; i++) {
         AVStream *st = s->streams[i];
-        if (st->codec->codec_type == CODEC_TYPE_AUDIO)
+        if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
             av_set_pts_info(st, 64, 1, st->codec->sample_rate);
-        else if (st->codec->codec_type == CODEC_TYPE_VIDEO)
+        else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
             av_set_pts_info(st, 64, st->codec->time_base.num, st->codec->time_base.den);
         if (st->codec->codec_id != CODEC_ID_VORBIS &&
             st->codec->codec_id != CODEC_ID_THEORA &&
+            st->codec->codec_id != CODEC_ID_SPEEX  &&
             st->codec->codec_id != CODEC_ID_FLAC) {
             av_log(s, AV_LOG_ERROR, "Unsupported codec id in stream %d\n", i);
             return -1;
@@ -135,10 +197,22 @@ static int ogg_write_header(AVFormatContext *s)
         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,
-                                       oggstream, st->codec->flags & CODEC_FLAG_BITEXACT) < 0) {
-                av_log(s, AV_LOG_ERROR, "Extradata corrupted\n");
+            int err = ogg_build_flac_headers(st->codec, oggstream,
+                                             st->codec->flags & CODEC_FLAG_BITEXACT,
+                                             s->metadata);
+            if (err) {
+                av_log(s, AV_LOG_ERROR, "Error writing FLAC headers\n");
+                av_freep(&st->priv_data);
+                return err;
+            }
+        } else if (st->codec->codec_id == CODEC_ID_SPEEX) {
+            int err = ogg_build_speex_headers(st->codec, oggstream,
+                                              st->codec->flags & CODEC_FLAG_BITEXACT,
+                                              s->metadata);
+            if (err) {
+                av_log(s, AV_LOG_ERROR, "Error writing Speex headers\n");
                 av_freep(&st->priv_data);
+                return err;
             }
         } else {
             if (ff_split_xiph_headers(st->codec->extradata, st->codec->extradata_size,
@@ -182,7 +256,7 @@ static int ogg_write_packet(AVFormatContext *s, AVPacket *pkt)
     if (st->codec->codec_id == CODEC_ID_THEORA) {
         int64_t pts = oggstream->vrev < 1 ? pkt->pts : pkt->pts + pkt->duration;
         int pframe_count;
-        if (pkt->flags & PKT_FLAG_KEY)
+        if (pkt->flags & AV_PKT_FLAG_KEY)
             oggstream->last_kf_pts = pts;
         pframe_count = pts - oggstream->last_kf_pts;
         // prevent frame count from overflow if key frame flag is not set
@@ -202,58 +276,53 @@ static int ogg_write_packet(AVFormatContext *s, AVPacket *pkt)
     return 0;
 }
 
-int ogg_interleave_per_granule(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
+static int ogg_compare_granule(AVFormatContext *s, AVPacket *next, AVPacket *pkt)
+{
+    AVStream *st2 = s->streams[next->stream_index];
+    AVStream *st  = s->streams[pkt ->stream_index];
+
+    int64_t next_granule = av_rescale_q(next->pts + next->duration,
+                                        st2->time_base, AV_TIME_BASE_Q);
+    int64_t cur_granule  = av_rescale_q(pkt ->pts + pkt ->duration,
+                                        st ->time_base, AV_TIME_BASE_Q);
+    return next_granule > cur_granule;
+}
+
+static int ogg_interleave_per_granule(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
 {
-    AVPacketList *pktl, **next_point, *this_pktl;
-    int stream_count = 0;
-    int streams[MAX_STREAMS] = {0};
+    OGGStreamContext *ogg;
+    int i, stream_count = 0;
     int interleaved = 0;
 
     if (pkt) {
-        AVStream *st = s->streams[pkt->stream_index];
-        this_pktl = av_mallocz(sizeof(AVPacketList));
-        this_pktl->pkt = *pkt;
-        if (pkt->destruct == av_destruct_packet)
-            pkt->destruct = NULL; // not shared -> must keep original from being freed
-        else
-            av_dup_packet(&this_pktl->pkt); // shared -> must dup
-        next_point = &s->packet_buffer;
-        while (*next_point) {
-            AVStream *st2 = s->streams[(*next_point)->pkt.stream_index];
-            AVPacket *next_pkt = &(*next_point)->pkt;
-            int64_t cur_granule, next_granule;
-            next_granule = av_rescale_q(next_pkt->pts + next_pkt->duration,
-                                        st2->time_base, AV_TIME_BASE_Q);
-            cur_granule = av_rescale_q(pkt->pts + pkt->duration,
-                                       st->time_base, AV_TIME_BASE_Q);
-            if (next_granule > cur_granule)
-                break;
-            next_point= &(*next_point)->next;
-        }
-        this_pktl->next= *next_point;
-        *next_point= this_pktl;
+        ff_interleave_add_packet(s, pkt, ogg_compare_granule);
+        ogg = s->streams[pkt->stream_index]->priv_data;
+        ogg->packet_count++;
     }
 
-    pktl = s->packet_buffer;
-    while (pktl) {
-        if (streams[pktl->pkt.stream_index] == 0)
-            stream_count++;
-        streams[pktl->pkt.stream_index]++;
-        // need to buffer at least one packet to set eos flag
-        if (streams[pktl->pkt.stream_index] == 2)
-            interleaved++;
-        pktl = pktl->next;
+    for (i = 0; i < s->nb_streams; i++) {
+        ogg = s->streams[i]->priv_data;
+        stream_count += !!ogg->packet_count;
+        interleaved += ogg->packet_count > 1;
     }
 
     if ((s->nb_streams == stream_count && interleaved == stream_count) ||
         (flush && stream_count)) {
-        pktl= s->packet_buffer;
+        AVPacketList *pktl= s->packet_buffer;
         *out= pktl->pkt;
         s->packet_buffer = pktl->next;
-        if (flush && streams[out->stream_index] == 1) {
-            OGGStreamContext *ogg = s->streams[out->stream_index]->priv_data;
+
+        ogg = s->streams[out->stream_index]->priv_data;
+        if (flush && ogg->packet_count == 1)
             ogg->eos = 1;
-        }
+        ogg->packet_count--;
+
+        if(!s->packet_buffer)
+            s->packet_buffer_end= NULL;
+
+        if(s->streams[out->stream_index]->last_in_packet_buffer == pktl)
+            s->streams[out->stream_index]->last_in_packet_buffer= NULL;
+
         av_freep(&pktl);
         return 1;
     } else {
@@ -268,7 +337,8 @@ static int ogg_write_trailer(AVFormatContext *s)
     for (i = 0; i < s->nb_streams; i++) {
         AVStream *st = s->streams[i];
         OGGStreamContext *oggstream = st->priv_data;
-        if (st->codec->codec_id == CODEC_ID_FLAC) {
+        if (st->codec->codec_id == CODEC_ID_FLAC ||
+            st->codec->codec_id == CODEC_ID_SPEEX) {
             av_free(oggstream->header[0]);
             av_free(oggstream->header[1]);
         }
@@ -281,7 +351,7 @@ AVOutputFormat ogg_muxer = {
     "ogg",
     NULL_IF_CONFIG_SMALL("Ogg"),
     "application/ogg",
-    "ogg",
+    "ogg,ogv,spx",
     0,
     CODEC_ID_FLAC,
     CODEC_ID_THEORA,
@@ -289,4 +359,5 @@ AVOutputFormat ogg_muxer = {
     ogg_write_packet,
     ogg_write_trailer,
     .interleave_packet = ogg_interleave_per_granule,
+    .metadata_conv = ff_vorbiscomment_metadata_conv,
 };