rtpdec_svq3.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. * @file
  23. * @brief RTP support for the SV3V (SVQ3) payload
  24. * @author Ronald S. Bultje <rbultje@ronald.bitfreak.net>
  25. * @see http://wiki.multimedia.cx/index.php?title=Sorenson_Video_3#Packetization
  26. */
  27. #include <string.h>
  28. #include "libavutil/intreadwrite.h"
  29. #include "rtp.h"
  30. #include "rtpdec.h"
  31. #include "rtpdec_formats.h"
  32. struct PayloadContext {
  33. AVIOContext *pktbuf;
  34. int64_t timestamp;
  35. };
  36. /** return 0 on packet, <0 on partial packet or error... */
  37. static int svq3_parse_packet (AVFormatContext *s, PayloadContext *sv,
  38. AVStream *st, AVPacket *pkt,
  39. uint32_t *timestamp,
  40. const uint8_t *buf, int len, int flags)
  41. {
  42. int config_packet, start_packet, end_packet;
  43. if (len < 2)
  44. return AVERROR_INVALIDDATA;
  45. config_packet = buf[0] & 0x40;
  46. start_packet = buf[0] & 0x20;
  47. end_packet = buf[0] & 0x10;
  48. buf += 2; // ignore buf[1]
  49. len -= 2;
  50. if (config_packet) {
  51. av_freep(&st->codec->extradata);
  52. st->codec->extradata_size = 0;
  53. if (len < 2 || !(st->codec->extradata =
  54. av_malloc(len + 8 + FF_INPUT_BUFFER_PADDING_SIZE)))
  55. return AVERROR_INVALIDDATA;
  56. st->codec->extradata_size = len + 8;
  57. memcpy(st->codec->extradata, "SEQH", 4);
  58. AV_WB32(st->codec->extradata + 4, len);
  59. memcpy(st->codec->extradata + 8, buf, len);
  60. /* We set codec_id to CODEC_ID_NONE initially to
  61. * delay decoder initialization since extradata is
  62. * carried within the RTP stream, not SDP. Here,
  63. * by setting codec_id to CODEC_ID_SVQ3, we are signalling
  64. * to the decoder that it is OK to initialize. */
  65. st->codec->codec_id = CODEC_ID_SVQ3;
  66. return AVERROR(EAGAIN);
  67. }
  68. if (start_packet) {
  69. int res;
  70. if (sv->pktbuf) {
  71. uint8_t *tmp;
  72. avio_close_dyn_buf(sv->pktbuf, &tmp);
  73. av_free(tmp);
  74. }
  75. if ((res = avio_open_dyn_buf(&sv->pktbuf)) < 0)
  76. return res;
  77. sv->timestamp = *timestamp;
  78. }
  79. if (!sv->pktbuf)
  80. return AVERROR_INVALIDDATA;
  81. avio_write(sv->pktbuf, buf, len);
  82. if (end_packet) {
  83. av_init_packet(pkt);
  84. pkt->stream_index = st->index;
  85. *timestamp = sv->timestamp;
  86. pkt->size = avio_close_dyn_buf(sv->pktbuf, &pkt->data);
  87. pkt->destruct = av_destruct_packet;
  88. sv->pktbuf = NULL;
  89. return 0;
  90. }
  91. return AVERROR(EAGAIN);
  92. }
  93. static PayloadContext *svq3_extradata_new(void)
  94. {
  95. return av_mallocz(sizeof(PayloadContext));
  96. }
  97. static void svq3_extradata_free(PayloadContext *sv)
  98. {
  99. if (sv->pktbuf) {
  100. uint8_t *buf;
  101. avio_close_dyn_buf(sv->pktbuf, &buf);
  102. av_free(buf);
  103. }
  104. av_free(sv);
  105. }
  106. RTPDynamicProtocolHandler ff_svq3_dynamic_handler = {
  107. .enc_name = "X-SV3V-ES",
  108. .codec_type = AVMEDIA_TYPE_VIDEO,
  109. .codec_id = CODEC_ID_NONE, // see if (config_packet) above
  110. .alloc = svq3_extradata_new,
  111. .free = svq3_extradata_free,
  112. .parse_packet = svq3_parse_packet,
  113. };