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.

146 lines
4.0KB

  1. /*
  2. * RTP VP8 Depacketizer
  3. * Copyright (c) 2010 Josh Allmann
  4. * Copyright (c) 2012 Martin Storsjo
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * @brief RTP support for the VP8 payload
  25. * @author Josh Allmann <joshua.allmann@gmail.com>
  26. * @see http://tools.ietf.org/html/draft-ietf-payload-vp8-05
  27. */
  28. #include "libavcodec/bytestream.h"
  29. #include "rtpdec_formats.h"
  30. struct PayloadContext {
  31. AVIOContext *data;
  32. uint32_t timestamp;
  33. };
  34. static int vp8_handle_packet(AVFormatContext *ctx,
  35. PayloadContext *vp8,
  36. AVStream *st,
  37. AVPacket *pkt,
  38. uint32_t *timestamp,
  39. const uint8_t *buf,
  40. int len, int flags)
  41. {
  42. int start_partition, end_packet;
  43. int extended_bits, part_id;
  44. int pictureid_present = 0, tl0picidx_present = 0, tid_present = 0,
  45. keyidx_present = 0;
  46. if (len < 1)
  47. return AVERROR_INVALIDDATA;
  48. extended_bits = buf[0] & 0x80;
  49. start_partition = buf[0] & 0x10;
  50. part_id = buf[0] & 0x0f;
  51. end_packet = flags & RTP_FLAG_MARKER;
  52. buf++;
  53. len--;
  54. if (extended_bits) {
  55. if (len < 1)
  56. return AVERROR_INVALIDDATA;
  57. pictureid_present = buf[0] & 0x80;
  58. tl0picidx_present = buf[0] & 0x40;
  59. tid_present = buf[0] & 0x20;
  60. keyidx_present = buf[0] & 0x10;
  61. buf++;
  62. len--;
  63. }
  64. if (pictureid_present) {
  65. int size;
  66. if (len < 1)
  67. return AVERROR_INVALIDDATA;
  68. size = buf[0] & 0x80 ? 2 : 1;
  69. buf += size;
  70. len -= size;
  71. }
  72. if (tl0picidx_present) {
  73. // Ignoring temporal level zero index
  74. buf++;
  75. len--;
  76. }
  77. if (tid_present || keyidx_present) {
  78. // Ignoring temporal layer index, layer sync bit and keyframe index
  79. buf++;
  80. len--;
  81. }
  82. if (len < 1)
  83. return AVERROR_INVALIDDATA;
  84. if (start_partition && part_id == 0) {
  85. int res;
  86. if (vp8->data) {
  87. uint8_t *tmp;
  88. avio_close_dyn_buf(vp8->data, &tmp);
  89. av_free(tmp);
  90. vp8->data = NULL;
  91. }
  92. if ((res = avio_open_dyn_buf(&vp8->data)) < 0)
  93. return res;
  94. vp8->timestamp = *timestamp;
  95. }
  96. if (!vp8->data || vp8->timestamp != *timestamp) {
  97. av_log(ctx, AV_LOG_WARNING,
  98. "Received no start marker; dropping frame\n");
  99. return AVERROR(EAGAIN);
  100. }
  101. avio_write(vp8->data, buf, len);
  102. if (end_packet) {
  103. int ret = ff_rtp_finalize_packet(pkt, &vp8->data, st->index);
  104. if (ret < 0)
  105. return ret;
  106. return 0;
  107. }
  108. return AVERROR(EAGAIN);
  109. }
  110. static PayloadContext *vp8_new_context(void)
  111. {
  112. return av_mallocz(sizeof(PayloadContext));
  113. }
  114. static void vp8_free_context(PayloadContext *vp8)
  115. {
  116. if (vp8->data) {
  117. uint8_t *tmp;
  118. avio_close_dyn_buf(vp8->data, &tmp);
  119. av_free(tmp);
  120. }
  121. av_free(vp8);
  122. }
  123. RTPDynamicProtocolHandler ff_vp8_dynamic_handler = {
  124. .enc_name = "VP8",
  125. .codec_type = AVMEDIA_TYPE_VIDEO,
  126. .codec_id = AV_CODEC_ID_VP8,
  127. .alloc = vp8_new_context,
  128. .free = vp8_free_context,
  129. .parse_packet = vp8_handle_packet,
  130. };