rtpdec_h263.c 3.4 KB

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