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.

103 lines
4.2KB

  1. /*
  2. * RTP packetization for H.261 video (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 "avformat.h"
  22. #include "rtpenc.h"
  23. #define RTP_H261_HEADER_SIZE 4
  24. static const uint8_t *find_resync_marker_reverse(const uint8_t *restrict start,
  25. const uint8_t *restrict end)
  26. {
  27. const uint8_t *p = end - 1;
  28. start += 1; /* Make sure we never return the original start. */
  29. for (; p > start; p--) {
  30. if (p[0] == 0 && p[1] == 1)
  31. return p;
  32. }
  33. return end;
  34. }
  35. void ff_rtp_send_h261(AVFormatContext *ctx, const uint8_t *frame_buf, int frame_size)
  36. {
  37. int cur_frame_size;
  38. int last_packet_of_frame;
  39. RTPMuxContext *rtp_ctx = ctx->priv_data;
  40. /* use the default 90 KHz time stamp */
  41. rtp_ctx->timestamp = rtp_ctx->cur_timestamp;
  42. /* continue as long as not all frame data is processed */
  43. while (frame_size > 0) {
  44. /*
  45. * encode the H.261 payload header according to section 4.1 of RFC 4587:
  46. * (uses 4 bytes between RTP header and H.261 stream per packet)
  47. *
  48. * 0 1 2 3
  49. * 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
  50. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  51. * |SBIT |EBIT |I|V| GOBN | MBAP | QUANT | HMVD | VMVD |
  52. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  53. *
  54. * Start bit position (SBIT): 3 bits
  55. * End bit position (EBIT): 3 bits
  56. * INTRA-frame encoded data (I): 1 bit
  57. * Motion Vector flag (V): 1 bit
  58. * GOB number (GOBN): 4 bits
  59. * Macroblock address predictor (MBAP): 5 bits
  60. * Quantizer (QUANT): 5 bits
  61. * Horizontal motion vector data (HMVD): 5 bits
  62. * Vertical motion vector data (VMVD): 5 bits
  63. */
  64. rtp_ctx->buf[0] = 1; /* sbit=0, ebit=0, i=0, v=1 */
  65. rtp_ctx->buf[1] = 0; /* gobn=0, mbap=0 */
  66. rtp_ctx->buf[2] = 0; /* quant=0, hmvd=5 */
  67. rtp_ctx->buf[3] = 0; /* vmvd=0 */
  68. if (frame_size < 2 || frame_buf[0] != 0 || frame_buf[1] != 1) {
  69. /* A full, correct fix for this would be to make the H.261 encoder
  70. * support inserting extra GOB headers (triggered by setting e.g.
  71. * "-ps 1"), and including information about macroblock boundaries
  72. * (such as for h263_rfc2190). */
  73. av_log(ctx, AV_LOG_WARNING,
  74. "RTP/H.261 packet not cut at a GOB boundary, not signaled correctly\n");
  75. }
  76. cur_frame_size = FFMIN(rtp_ctx->max_payload_size - RTP_H261_HEADER_SIZE, frame_size);
  77. /* look for a better place to split the frame into packets */
  78. if (cur_frame_size < frame_size) {
  79. const uint8_t *packet_end = find_resync_marker_reverse(frame_buf,
  80. frame_buf + cur_frame_size);
  81. cur_frame_size = packet_end - frame_buf;
  82. }
  83. /* calculate the "marker" bit for the RTP header */
  84. last_packet_of_frame = cur_frame_size == frame_size;
  85. /* complete and send RTP packet */
  86. memcpy(&rtp_ctx->buf[RTP_H261_HEADER_SIZE], frame_buf, cur_frame_size);
  87. ff_rtp_send_data(ctx, rtp_ctx->buf, RTP_H261_HEADER_SIZE + cur_frame_size, last_packet_of_frame);
  88. frame_buf += cur_frame_size;
  89. frame_size -= cur_frame_size;
  90. }
  91. }