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.

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