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.

107 lines
3.5KB

  1. /*
  2. * RTP H.263 Depacketizer, RFC 4629
  3. * Copyright (c) 2010 Martin Storsjo
  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 "rtpdec_formats.h"
  23. #include "libavutil/attributes.h"
  24. #include "libavutil/intreadwrite.h"
  25. int ff_h263_handle_packet(AVFormatContext *ctx, PayloadContext *data,
  26. AVStream *st, AVPacket *pkt, uint32_t *timestamp,
  27. const uint8_t *buf, int len, uint16_t seq, int flags)
  28. {
  29. uint8_t *ptr;
  30. uint16_t header;
  31. int startcode, vrc, picture_header;
  32. if (len < 2) {
  33. av_log(ctx, AV_LOG_ERROR, "Too short H.263 RTP packet\n");
  34. return AVERROR_INVALIDDATA;
  35. }
  36. /* Decode the 16 bit H.263+ payload header, as described in section
  37. * 5.1 of RFC 4629. The fields of this header are:
  38. * - 5 reserved bits, should be ignored.
  39. * - One bit (P, startcode), indicating a picture start, picture segment
  40. * start or video sequence end. If set, two zero bytes should be
  41. * prepended to the payload.
  42. * - One bit (V, vrc), indicating the presence of an 8 bit Video
  43. * Redundancy Coding field after this 16 bit header.
  44. * - 6 bits (PLEN, picture_header), the length (in bytes) of an extra
  45. * picture header, following the VRC field.
  46. * - 3 bits (PEBIT), the number of bits to ignore of the last byte
  47. * of the extra picture header. (Not used at the moment.)
  48. */
  49. header = AV_RB16(buf);
  50. startcode = (header & 0x0400) >> 9;
  51. vrc = header & 0x0200;
  52. picture_header = (header & 0x01f8) >> 3;
  53. buf += 2;
  54. len -= 2;
  55. if (vrc) {
  56. /* Skip VRC header if present, not used at the moment. */
  57. buf += 1;
  58. len -= 1;
  59. }
  60. if (picture_header) {
  61. /* Skip extra picture header if present, not used at the moment. */
  62. buf += picture_header;
  63. len -= picture_header;
  64. }
  65. if (len < 0) {
  66. av_log(ctx, AV_LOG_ERROR, "Too short H.263 RTP packet\n");
  67. return AVERROR_INVALIDDATA;
  68. }
  69. if (av_new_packet(pkt, len + startcode)) {
  70. av_log(ctx, AV_LOG_ERROR, "Out of memory\n");
  71. return AVERROR(ENOMEM);
  72. }
  73. pkt->stream_index = st->index;
  74. ptr = pkt->data;
  75. if (startcode) {
  76. *ptr++ = 0;
  77. *ptr++ = 0;
  78. }
  79. memcpy(ptr, buf, len);
  80. return 0;
  81. }
  82. RTPDynamicProtocolHandler ff_h263_1998_dynamic_handler = {
  83. .enc_name = "H263-1998",
  84. .codec_type = AVMEDIA_TYPE_VIDEO,
  85. .codec_id = AV_CODEC_ID_H263,
  86. .need_parsing = AVSTREAM_PARSE_FULL,
  87. .parse_packet = ff_h263_handle_packet,
  88. };
  89. RTPDynamicProtocolHandler ff_h263_2000_dynamic_handler = {
  90. .enc_name = "H263-2000",
  91. .codec_type = AVMEDIA_TYPE_VIDEO,
  92. .codec_id = AV_CODEC_ID_H263,
  93. .need_parsing = AVSTREAM_PARSE_FULL,
  94. .parse_packet = ff_h263_handle_packet,
  95. };