| 1 | /* |
| 2 | * Sorenson-3 (SVQ3/SV3V) payload for RTP |
| 3 | * Copyright (c) 2010 Ronald S. Bultje |
| 4 | * |
| 5 | * This file is part of FFmpeg. |
| 6 | * |
| 7 | * FFmpeg is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation; either |
| 10 | * version 2.1 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * FFmpeg is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with FFmpeg; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | /** |
| 23 | * @file |
| 24 | * @brief RTP support for the SV3V (SVQ3) payload |
| 25 | * @author Ronald S. Bultje <rbultje@ronald.bitfreak.net> |
| 26 | * @see http://wiki.multimedia.cx/index.php?title=Sorenson_Video_3#Packetization |
| 27 | */ |
| 28 | |
| 29 | #include <string.h> |
| 30 | #include "libavutil/intreadwrite.h" |
| 31 | #include "internal.h" |
| 32 | #include "rtp.h" |
| 33 | #include "rtpdec.h" |
| 34 | #include "rtpdec_formats.h" |
| 35 | |
| 36 | struct PayloadContext { |
| 37 | AVIOContext *pktbuf; |
| 38 | int64_t timestamp; |
| 39 | }; |
| 40 | |
| 41 | /** return 0 on packet, <0 on partial packet or error... */ |
| 42 | static int svq3_parse_packet (AVFormatContext *s, PayloadContext *sv, |
| 43 | AVStream *st, AVPacket *pkt, |
| 44 | uint32_t *timestamp, |
| 45 | const uint8_t *buf, int len, uint16_t seq, |
| 46 | int flags) |
| 47 | { |
| 48 | int config_packet, start_packet, end_packet; |
| 49 | |
| 50 | if (len < 2) |
| 51 | return AVERROR_INVALIDDATA; |
| 52 | |
| 53 | config_packet = buf[0] & 0x40; |
| 54 | start_packet = buf[0] & 0x20; |
| 55 | end_packet = buf[0] & 0x10; |
| 56 | buf += 2; // ignore buf[1] |
| 57 | len -= 2; |
| 58 | |
| 59 | if (config_packet) { |
| 60 | |
| 61 | av_freep(&st->codec->extradata); |
| 62 | st->codec->extradata_size = 0; |
| 63 | |
| 64 | if (len < 2 || ff_alloc_extradata(st->codec, len + 8)) |
| 65 | return AVERROR_INVALIDDATA; |
| 66 | |
| 67 | memcpy(st->codec->extradata, "SEQH", 4); |
| 68 | AV_WB32(st->codec->extradata + 4, len); |
| 69 | memcpy(st->codec->extradata + 8, buf, len); |
| 70 | |
| 71 | /* We set codec_id to AV_CODEC_ID_NONE initially to |
| 72 | * delay decoder initialization since extradata is |
| 73 | * carried within the RTP stream, not SDP. Here, |
| 74 | * by setting codec_id to AV_CODEC_ID_SVQ3, we are signalling |
| 75 | * to the decoder that it is OK to initialize. */ |
| 76 | st->codec->codec_id = AV_CODEC_ID_SVQ3; |
| 77 | |
| 78 | return AVERROR(EAGAIN); |
| 79 | } |
| 80 | |
| 81 | if (start_packet) { |
| 82 | int res; |
| 83 | |
| 84 | if (sv->pktbuf) { |
| 85 | uint8_t *tmp; |
| 86 | avio_close_dyn_buf(sv->pktbuf, &tmp); |
| 87 | av_free(tmp); |
| 88 | } |
| 89 | if ((res = avio_open_dyn_buf(&sv->pktbuf)) < 0) |
| 90 | return res; |
| 91 | sv->timestamp = *timestamp; |
| 92 | } |
| 93 | |
| 94 | if (!sv->pktbuf) |
| 95 | return AVERROR_INVALIDDATA; |
| 96 | |
| 97 | avio_write(sv->pktbuf, buf, len); |
| 98 | |
| 99 | if (end_packet) { |
| 100 | int ret = ff_rtp_finalize_packet(pkt, &sv->pktbuf, st->index); |
| 101 | if (ret < 0) |
| 102 | return ret; |
| 103 | |
| 104 | *timestamp = sv->timestamp; |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | return AVERROR(EAGAIN); |
| 109 | } |
| 110 | |
| 111 | static PayloadContext *svq3_extradata_new(void) |
| 112 | { |
| 113 | return av_mallocz(sizeof(PayloadContext)); |
| 114 | } |
| 115 | |
| 116 | static void svq3_extradata_free(PayloadContext *sv) |
| 117 | { |
| 118 | if (sv->pktbuf) { |
| 119 | uint8_t *buf; |
| 120 | avio_close_dyn_buf(sv->pktbuf, &buf); |
| 121 | av_free(buf); |
| 122 | } |
| 123 | av_free(sv); |
| 124 | } |
| 125 | |
| 126 | RTPDynamicProtocolHandler ff_svq3_dynamic_handler = { |
| 127 | .enc_name = "X-SV3V-ES", |
| 128 | .codec_type = AVMEDIA_TYPE_VIDEO, |
| 129 | .codec_id = AV_CODEC_ID_NONE, // see if (config_packet) above |
| 130 | .alloc = svq3_extradata_new, |
| 131 | .free = svq3_extradata_free, |
| 132 | .parse_packet = svq3_parse_packet, |
| 133 | }; |