libavformat/rtpdec_svq3.c
4449df6b
 /*
  * Sorenson-3 (SVQ3/SV3V) payload for RTP
  * Copyright (c) 2010 Ronald S. Bultje
  *
  * This file is part of FFmpeg.
  *
  * 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.
  *
  * 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 FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 /**
  * @file
  * @brief RTP support for the SV3V (SVQ3) payload
  * @author Ronald S. Bultje <rbultje@ronald.bitfreak.net>
ad4cd0c2
  * @see http://wiki.multimedia.cx/index.php?title=Sorenson_Video_3#Packetization
4449df6b
  */
 
 #include <string.h>
752ff3ff
 #include "libavutil/intreadwrite.h"
a807c682
 #include "internal.h"
4449df6b
 #include "rtp.h"
 #include "rtpdec.h"
965a3ddb
 #include "rtpdec_formats.h"
4449df6b
 
 struct PayloadContext {
471fe57e
     AVIOContext *pktbuf;
4449df6b
     int64_t        timestamp;
 };
 
 /** return 0 on packet, <0 on partial packet or error... */
 static int svq3_parse_packet (AVFormatContext *s, PayloadContext *sv,
                               AVStream *st, AVPacket *pkt,
                               uint32_t *timestamp,
90c784cc
                               const uint8_t *buf, int len, uint16_t seq,
                               int flags)
4449df6b
 {
     int config_packet, start_packet, end_packet;
 
     if (len < 2)
         return AVERROR_INVALIDDATA;
 
     config_packet = buf[0] & 0x40;
     start_packet  = buf[0] & 0x20;
     end_packet    = buf[0] & 0x10;
     buf += 2;     // ignore buf[1]
     len -= 2;
 
     if (config_packet) {
 
         av_freep(&st->codec->extradata);
         st->codec->extradata_size = 0;
 
a807c682
         if (len < 2 || ff_alloc_extradata(st->codec, len + 8))
4449df6b
             return AVERROR_INVALIDDATA;
 
         memcpy(st->codec->extradata, "SEQH", 4);
         AV_WB32(st->codec->extradata + 4, len);
         memcpy(st->codec->extradata + 8, buf, len);
 
36ef5369
         /* We set codec_id to AV_CODEC_ID_NONE initially to
4449df6b
          * delay decoder initialization since extradata is
          * carried within the RTP stream, not SDP. Here,
36ef5369
          * by setting codec_id to AV_CODEC_ID_SVQ3, we are signalling
4449df6b
          * to the decoder that it is OK to initialize. */
36ef5369
         st->codec->codec_id = AV_CODEC_ID_SVQ3;
4449df6b
 
         return AVERROR(EAGAIN);
     }
 
     if (start_packet) {
         int res;
 
         if (sv->pktbuf) {
             uint8_t *tmp;
6dc7d80d
             avio_close_dyn_buf(sv->pktbuf, &tmp);
4449df6b
             av_free(tmp);
         }
b92c5452
         if ((res = avio_open_dyn_buf(&sv->pktbuf)) < 0)
4449df6b
             return res;
         sv->timestamp   = *timestamp;
     }
 
     if (!sv->pktbuf)
         return AVERROR_INVALIDDATA;
 
e9eb8d0b
     avio_write(sv->pktbuf, buf, len);
4449df6b
 
     if (end_packet) {
179a5c37
         int ret = ff_rtp_finalize_packet(pkt, &sv->pktbuf, st->index);
         if (ret < 0)
             return ret;
 
5590cc78
         *timestamp        = sv->timestamp;
4449df6b
         return 0;
     }
 
     return AVERROR(EAGAIN);
 }
 
 static PayloadContext *svq3_extradata_new(void)
 {
     return av_mallocz(sizeof(PayloadContext));
 }
 
 static void svq3_extradata_free(PayloadContext *sv)
 {
     if (sv->pktbuf) {
         uint8_t *buf;
6dc7d80d
         avio_close_dyn_buf(sv->pktbuf, &buf);
4449df6b
         av_free(buf);
     }
     av_free(sv);
 }
 
 RTPDynamicProtocolHandler ff_svq3_dynamic_handler = {
fe70a95e
     .enc_name         = "X-SV3V-ES",
     .codec_type       = AVMEDIA_TYPE_VIDEO,
36ef5369
     .codec_id         = AV_CODEC_ID_NONE,      // see if (config_packet) above
9261e6cf
     .alloc            = svq3_extradata_new,
     .free             = svq3_extradata_free,
fe70a95e
     .parse_packet     = svq3_parse_packet,
4449df6b
 };