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.

217 lines
6.7KB

  1. /*
  2. * RTP H.263 Depacketizer, RFC 2190
  3. * Copyright (c) 2012 Martin Storsjo
  4. * Based on the GStreamer H.263 Depayloder:
  5. * Copyright 2005 Wim Taymans
  6. * Copyright 2007 Edward Hervey
  7. * Copyright 2007 Nokia Corporation
  8. * Copyright 2007 Collabora Ltd, Philippe Kalaf
  9. * Copyright 2010 Mark Nauwelaerts
  10. *
  11. * This file is part of FFmpeg.
  12. *
  13. * FFmpeg is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU Lesser General Public
  15. * License as published by the Free Software Foundation; either
  16. * version 2.1 of the License, or (at your option) any later version.
  17. *
  18. * FFmpeg is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. * Lesser General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Lesser General Public
  24. * License along with FFmpeg; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  26. */
  27. #include "avformat.h"
  28. #include "rtpdec_formats.h"
  29. #include "libavutil/attributes.h"
  30. #include "libavutil/intreadwrite.h"
  31. #include "libavcodec/get_bits.h"
  32. struct PayloadContext {
  33. AVIOContext *buf;
  34. uint8_t endbyte;
  35. int endbyte_bits;
  36. uint32_t timestamp;
  37. int newformat;
  38. };
  39. static PayloadContext *h263_new_context(void)
  40. {
  41. return av_mallocz(sizeof(PayloadContext));
  42. }
  43. static void h263_free_context(PayloadContext *data)
  44. {
  45. if (!data)
  46. return;
  47. if (data->buf) {
  48. uint8_t *p;
  49. avio_close_dyn_buf(data->buf, &p);
  50. av_free(p);
  51. }
  52. av_free(data);
  53. }
  54. static av_cold int h263_init(AVFormatContext *ctx, int st_index, PayloadContext *data)
  55. {
  56. if (st_index < 0)
  57. return 0;
  58. ctx->streams[st_index]->need_parsing = AVSTREAM_PARSE_FULL;
  59. return 0;
  60. }
  61. static int h263_handle_packet(AVFormatContext *ctx, PayloadContext *data,
  62. AVStream *st, AVPacket *pkt, uint32_t *timestamp,
  63. const uint8_t *buf, int len, uint16_t seq,
  64. int flags)
  65. {
  66. /* Corresponding to header fields in the RFC */
  67. int f, p, i, sbit, ebit, src, r;
  68. int header_size, ret;
  69. if (data->newformat)
  70. return ff_h263_handle_packet(ctx, data, st, pkt, timestamp, buf, len,
  71. seq, flags);
  72. if (data->buf && data->timestamp != *timestamp) {
  73. /* Dropping old buffered, unfinished data */
  74. uint8_t *p;
  75. avio_close_dyn_buf(data->buf, &p);
  76. av_free(p);
  77. data->buf = NULL;
  78. data->endbyte_bits = 0;
  79. }
  80. if (len < 4) {
  81. av_log(ctx, AV_LOG_ERROR, "Too short H.263 RTP packet: %d\n", len);
  82. return AVERROR_INVALIDDATA;
  83. }
  84. f = buf[0] & 0x80;
  85. p = buf[0] & 0x40;
  86. if (!f) {
  87. /* Mode A */
  88. header_size = 4;
  89. i = buf[1] & 0x10;
  90. r = ((buf[1] & 0x01) << 3) | ((buf[2] & 0xe0) >> 5);
  91. } else if (!p) {
  92. /* Mode B */
  93. header_size = 8;
  94. if (len < header_size) {
  95. av_log(ctx, AV_LOG_ERROR,
  96. "Too short H.263 RTP packet: %d bytes, %d header bytes\n",
  97. len, header_size);
  98. return AVERROR_INVALIDDATA;
  99. }
  100. r = buf[3] & 0x03;
  101. i = buf[4] & 0x80;
  102. } else {
  103. /* Mode C */
  104. header_size = 12;
  105. if (len < header_size) {
  106. av_log(ctx, AV_LOG_ERROR,
  107. "Too short H.263 RTP packet: %d bytes, %d header bytes\n",
  108. len, header_size);
  109. return AVERROR_INVALIDDATA;
  110. }
  111. r = buf[3] & 0x03;
  112. i = buf[4] & 0x80;
  113. }
  114. sbit = (buf[0] >> 3) & 0x7;
  115. ebit = buf[0] & 0x7;
  116. src = (buf[1] & 0xe0) >> 5;
  117. if (!(buf[0] & 0xf8)) { /* Reserved bits in RFC 2429/4629 are zero */
  118. if ((src == 0 || src >= 6) && r) {
  119. /* Invalid src for this format, and bits that should be zero
  120. * according to RFC 2190 aren't zero. */
  121. av_log(ctx, AV_LOG_WARNING,
  122. "Interpreting H263 RTP data as RFC 2429/4629 even though "
  123. "signalled with a static payload type.\n");
  124. data->newformat = 1;
  125. return ff_h263_handle_packet(ctx, data, st, pkt, timestamp, buf,
  126. len, seq, flags);
  127. }
  128. }
  129. buf += header_size;
  130. len -= header_size;
  131. if (!data->buf) {
  132. /* Check the picture start code, only start buffering a new frame
  133. * if this is correct */
  134. if (len > 4 && AV_RB32(buf) >> 10 == 0x20) {
  135. ret = avio_open_dyn_buf(&data->buf);
  136. if (ret < 0)
  137. return ret;
  138. data->timestamp = *timestamp;
  139. } else {
  140. /* Frame not started yet, skipping */
  141. return AVERROR(EAGAIN);
  142. }
  143. }
  144. if (data->endbyte_bits || sbit) {
  145. if (data->endbyte_bits == sbit) {
  146. data->endbyte |= buf[0] & (0xff >> sbit);
  147. data->endbyte_bits = 0;
  148. buf++;
  149. len--;
  150. avio_w8(data->buf, data->endbyte);
  151. } else {
  152. /* Start/end skip bits not matching - missed packets? */
  153. GetBitContext gb;
  154. init_get_bits(&gb, buf, len*8 - ebit);
  155. skip_bits(&gb, sbit);
  156. if (data->endbyte_bits) {
  157. data->endbyte |= get_bits(&gb, 8 - data->endbyte_bits);
  158. avio_w8(data->buf, data->endbyte);
  159. }
  160. while (get_bits_left(&gb) >= 8)
  161. avio_w8(data->buf, get_bits(&gb, 8));
  162. data->endbyte_bits = get_bits_left(&gb);
  163. if (data->endbyte_bits)
  164. data->endbyte = get_bits(&gb, data->endbyte_bits) <<
  165. (8 - data->endbyte_bits);
  166. ebit = 0;
  167. len = 0;
  168. }
  169. }
  170. if (ebit) {
  171. if (len > 0)
  172. avio_write(data->buf, buf, len - 1);
  173. data->endbyte_bits = 8 - ebit;
  174. data->endbyte = buf[len - 1] & (0xff << ebit);
  175. } else {
  176. avio_write(data->buf, buf, len);
  177. }
  178. if (!(flags & RTP_FLAG_MARKER))
  179. return AVERROR(EAGAIN);
  180. if (data->endbyte_bits)
  181. avio_w8(data->buf, data->endbyte);
  182. data->endbyte_bits = 0;
  183. ret = ff_rtp_finalize_packet(pkt, &data->buf, st->index);
  184. if (ret < 0)
  185. return ret;
  186. if (!i)
  187. pkt->flags |= AV_PKT_FLAG_KEY;
  188. return 0;
  189. }
  190. RTPDynamicProtocolHandler ff_h263_rfc2190_dynamic_handler = {
  191. .codec_type = AVMEDIA_TYPE_VIDEO,
  192. .codec_id = AV_CODEC_ID_H263,
  193. .init = h263_init,
  194. .parse_packet = h263_handle_packet,
  195. .alloc = h263_new_context,
  196. .free = h263_free_context,
  197. .static_payload_id = 34,
  198. };