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.

176 lines
6.5KB

  1. /*
  2. * RTP parser for H.261 payload format (RFC 4587)
  3. * Copyright (c) 2014 Thomas Volkert <thomas@homer-conferencing.com>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavcodec/bitstream.h"
  22. #include "avformat.h"
  23. #include "avio_internal.h"
  24. #include "rtpdec_formats.h"
  25. #define RTP_H261_PAYLOAD_HEADER_SIZE 4
  26. struct PayloadContext {
  27. AVIOContext *buf;
  28. uint8_t endbyte;
  29. int endbyte_bits;
  30. uint32_t timestamp;
  31. };
  32. static av_cold void h261_close_context(PayloadContext *pl_ctx)
  33. {
  34. /* return if context is invalid */
  35. if (!pl_ctx)
  36. return;
  37. /* free buffer if it is valid */
  38. ffio_free_dyn_buf(&pl_ctx->buf);
  39. }
  40. static int h261_handle_packet(AVFormatContext *ctx, PayloadContext *rtp_h261_ctx,
  41. AVStream *st, AVPacket *pkt, uint32_t *timestamp,
  42. const uint8_t *buf, int len, uint16_t seq,
  43. int flags)
  44. {
  45. int sbit, ebit, gobn, mbap, quant;
  46. int res;
  47. /* drop data of previous packets in case of non-continuous (lossy) packet stream */
  48. if (rtp_h261_ctx->buf && rtp_h261_ctx->timestamp != *timestamp) {
  49. ffio_free_dyn_buf(&rtp_h261_ctx->buf);
  50. rtp_h261_ctx->endbyte_bits = 0;
  51. }
  52. /* sanity check for size of input packet: 1 byte payload at least */
  53. if (len < RTP_H261_PAYLOAD_HEADER_SIZE + 1) {
  54. av_log(ctx, AV_LOG_ERROR, "Too short RTP/H.261 packet, got %d bytes\n", len);
  55. return AVERROR_INVALIDDATA;
  56. }
  57. /*
  58. * decode the H.261 payload header according to section 4.1 of RFC 4587:
  59. * (uses 4 bytes between RTP header and H.261 stream per packet)
  60. *
  61. * 0 1 2 3
  62. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  63. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  64. * |SBIT |EBIT |I|V| GOBN | MBAP | QUANT | HMVD | VMVD |
  65. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  66. *
  67. * Start bit position (SBIT): 3 bits
  68. * End bit position (EBIT): 3 bits
  69. * INTRA-frame encoded data (I): 1 bit
  70. * Motion Vector flag (V): 1 bit
  71. * GOB number (GOBN): 4 bits
  72. * Macroblock address predictor (MBAP): 5 bits
  73. * Quantizer (QUANT): 5 bits
  74. * Horizontal motion vector data (HMVD): 5 bits
  75. * Vertical motion vector data (VMVD): 5 bits
  76. */
  77. sbit = (buf[0] >> 5) & 0x07;
  78. ebit = (buf[0] >> 2) & 0x07;
  79. gobn = (buf[1] >> 4) & 0x0f;
  80. mbap = ((buf[1] << 1) & 0x1e) | ((buf[2] >> 7) & 0x01);
  81. quant = (buf[2] >> 2) & 0x1f;
  82. /* pass the H.261 payload header and continue with the actual payload */
  83. buf += RTP_H261_PAYLOAD_HEADER_SIZE;
  84. len -= RTP_H261_PAYLOAD_HEADER_SIZE;
  85. /* start frame buffering with new dynamic buffer */
  86. if (!rtp_h261_ctx->buf) {
  87. /* sanity check: a new frame starts with gobn=0, sbit=0, mbap=0, quant=0 */
  88. if (!gobn && !sbit && !mbap && !quant) {
  89. res = avio_open_dyn_buf(&rtp_h261_ctx->buf);
  90. if (res < 0)
  91. return res;
  92. /* update the timestamp in the frame packet with the one from the RTP packet */
  93. rtp_h261_ctx->timestamp = *timestamp;
  94. } else {
  95. /* frame not started yet, need more packets */
  96. return AVERROR(EAGAIN);
  97. }
  98. }
  99. /* do the "byte merging" at the boundaries of two consecutive frame fragments */
  100. if (rtp_h261_ctx->endbyte_bits || sbit) {
  101. if (rtp_h261_ctx->endbyte_bits == sbit) {
  102. rtp_h261_ctx->endbyte |= buf[0] & (0xff >> sbit);
  103. rtp_h261_ctx->endbyte_bits = 0;
  104. buf++;
  105. len--;
  106. avio_w8(rtp_h261_ctx->buf, rtp_h261_ctx->endbyte);
  107. } else {
  108. /* ebit/sbit values inconsistent, assuming packet loss */
  109. BitstreamContext bc;
  110. bitstream_init(&bc, buf, len * 8 - ebit);
  111. bitstream_skip(&bc, sbit);
  112. if (rtp_h261_ctx->endbyte_bits) {
  113. rtp_h261_ctx->endbyte |= bitstream_read(&bc, 8 - rtp_h261_ctx->endbyte_bits);
  114. avio_w8(rtp_h261_ctx->buf, rtp_h261_ctx->endbyte);
  115. }
  116. while (bitstream_bits_left(&bc) >= 8)
  117. avio_w8(rtp_h261_ctx->buf, bitstream_read(&bc, 8));
  118. rtp_h261_ctx->endbyte_bits = bitstream_bits_left(&bc);
  119. if (rtp_h261_ctx->endbyte_bits)
  120. rtp_h261_ctx->endbyte = bitstream_read(&bc, rtp_h261_ctx->endbyte_bits) <<
  121. (8 - rtp_h261_ctx->endbyte_bits);
  122. ebit = 0;
  123. len = 0;
  124. }
  125. }
  126. if (ebit) {
  127. if (len > 0)
  128. avio_write(rtp_h261_ctx->buf, buf, len - 1);
  129. rtp_h261_ctx->endbyte_bits = 8 - ebit;
  130. rtp_h261_ctx->endbyte = buf[len - 1] & (0xff << ebit);
  131. } else {
  132. avio_write(rtp_h261_ctx->buf, buf, len);
  133. }
  134. /* RTP marker bit means: last fragment of current frame was received;
  135. otherwise, an additional fragment is needed for the current frame */
  136. if (!(flags & RTP_FLAG_MARKER))
  137. return AVERROR(EAGAIN);
  138. /* write the completed last byte from the "byte merging" */
  139. if (rtp_h261_ctx->endbyte_bits)
  140. avio_w8(rtp_h261_ctx->buf, rtp_h261_ctx->endbyte);
  141. rtp_h261_ctx->endbyte_bits = 0;
  142. /* close frame buffering and create resulting A/V packet */
  143. res = ff_rtp_finalize_packet(pkt, &rtp_h261_ctx->buf, st->index);
  144. if (res < 0)
  145. return res;
  146. return 0;
  147. }
  148. RTPDynamicProtocolHandler ff_h261_dynamic_handler = {
  149. .enc_name = "H261",
  150. .codec_type = AVMEDIA_TYPE_VIDEO,
  151. .codec_id = AV_CODEC_ID_H261,
  152. .need_parsing = AVSTREAM_PARSE_FULL,
  153. .priv_data_size = sizeof(PayloadContext),
  154. .close = h261_close_context,
  155. .parse_packet = h261_handle_packet,
  156. .static_payload_id = 31,
  157. };