You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
3.9KB

  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, uint16_t seq,
  41. int flags)
  42. {
  43. int config_packet, start_packet, end_packet;
  44. if (len < 2)
  45. return AVERROR_INVALIDDATA;
  46. config_packet = buf[0] & 0x40;
  47. start_packet = buf[0] & 0x20;
  48. end_packet = buf[0] & 0x10;
  49. buf += 2; // ignore buf[1]
  50. len -= 2;
  51. if (config_packet) {
  52. av_freep(&st->codec->extradata);
  53. st->codec->extradata_size = 0;
  54. if (len < 2 || !(st->codec->extradata =
  55. av_malloc(len + 8 + FF_INPUT_BUFFER_PADDING_SIZE)))
  56. return AVERROR_INVALIDDATA;
  57. st->codec->extradata_size = len + 8;
  58. memcpy(st->codec->extradata, "SEQH", 4);
  59. AV_WB32(st->codec->extradata + 4, len);
  60. memcpy(st->codec->extradata + 8, buf, len);
  61. /* We set codec_id to AV_CODEC_ID_NONE initially to
  62. * delay decoder initialization since extradata is
  63. * carried within the RTP stream, not SDP. Here,
  64. * by setting codec_id to AV_CODEC_ID_SVQ3, we are signalling
  65. * to the decoder that it is OK to initialize. */
  66. st->codec->codec_id = AV_CODEC_ID_SVQ3;
  67. return AVERROR(EAGAIN);
  68. }
  69. if (start_packet) {
  70. int res;
  71. if (sv->pktbuf) {
  72. uint8_t *tmp;
  73. avio_close_dyn_buf(sv->pktbuf, &tmp);
  74. av_free(tmp);
  75. }
  76. if ((res = avio_open_dyn_buf(&sv->pktbuf)) < 0)
  77. return res;
  78. sv->timestamp = *timestamp;
  79. }
  80. if (!sv->pktbuf)
  81. return AVERROR_INVALIDDATA;
  82. avio_write(sv->pktbuf, buf, len);
  83. if (end_packet) {
  84. int ret = ff_rtp_finalize_packet(pkt, &sv->pktbuf, st->index);
  85. if (ret < 0)
  86. return ret;
  87. *timestamp = sv->timestamp;
  88. return 0;
  89. }
  90. return AVERROR(EAGAIN);
  91. }
  92. static PayloadContext *svq3_extradata_new(void)
  93. {
  94. return av_mallocz(sizeof(PayloadContext));
  95. }
  96. static void svq3_extradata_free(PayloadContext *sv)
  97. {
  98. if (sv->pktbuf) {
  99. uint8_t *buf;
  100. avio_close_dyn_buf(sv->pktbuf, &buf);
  101. av_free(buf);
  102. }
  103. av_free(sv);
  104. }
  105. RTPDynamicProtocolHandler ff_svq3_dynamic_handler = {
  106. .enc_name = "X-SV3V-ES",
  107. .codec_type = AVMEDIA_TYPE_VIDEO,
  108. .codec_id = AV_CODEC_ID_NONE, // see if (config_packet) above
  109. .alloc = svq3_extradata_new,
  110. .free = svq3_extradata_free,
  111. .parse_packet = svq3_parse_packet,
  112. };