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.

192 lines
6.8KB

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