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.

105 lines
3.2KB

  1. /*
  2. * RTP packetization for MPEG video
  3. * Copyright (c) 2002 Fabrice Bellard.
  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 "rtp_internal.h"
  23. #include "mpegvideo.h"
  24. /* NOTE: a single frame must be passed with sequence header if
  25. needed. XXX: use slices. */
  26. void ff_rtp_send_mpegvideo(AVFormatContext *s1, const uint8_t *buf1, int size)
  27. {
  28. RTPDemuxContext *s = s1->priv_data;
  29. AVStream *st = s1->streams[0];
  30. int len, h, max_packet_size;
  31. uint8_t *q;
  32. int begin_of_slice, end_of_slice;
  33. max_packet_size = s->max_payload_size;
  34. begin_of_slice = 1;
  35. end_of_slice = 0;
  36. while (size > 0) {
  37. len = max_packet_size - 4;
  38. if (len >= size) {
  39. len = size;
  40. end_of_slice = 1;
  41. } else {
  42. const uint8_t *r, *r1;
  43. int start_code;
  44. r1 = buf1;
  45. while (1) {
  46. start_code = -1;
  47. r = ff_find_start_code(r1, buf1 + size, &start_code);
  48. if((start_code & 0xFFFFFF00) == 0x100) {
  49. /* New start code found */
  50. if (r - buf1 < len) {
  51. /* The current slice fits in the packet */
  52. if (begin_of_slice == 0) {
  53. /* no slice at the beginning of the packet... */
  54. end_of_slice = 1;
  55. len = r - buf1 - 4;
  56. break;
  57. }
  58. r1 = r;
  59. } else {
  60. if (r - r1 < max_packet_size) {
  61. len = r1 - buf1 - 4;
  62. end_of_slice = 1;
  63. }
  64. break;
  65. }
  66. } else {
  67. break;
  68. }
  69. }
  70. }
  71. h = 0;
  72. h |= begin_of_slice << 12;
  73. h |= end_of_slice << 11;
  74. q = s->buf;
  75. *q++ = h >> 24;
  76. *q++ = h >> 16;
  77. *q++ = h >> 8;
  78. *q++ = h;
  79. memcpy(q, buf1, len);
  80. q += len;
  81. /* 90 KHz time stamp */
  82. s->timestamp = s->base_timestamp +
  83. av_rescale((int64_t)s->cur_timestamp * st->codec->time_base.num, 90000, st->codec->time_base.den); //FIXME pass timestamps
  84. ff_rtp_send_data(s1, s->buf, q - s->buf, (len == size));
  85. buf1 += len;
  86. size -= len;
  87. begin_of_slice = end_of_slice;
  88. end_of_slice = 0;
  89. }
  90. s->cur_timestamp++;
  91. }