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.

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