dnxhd_parser.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * DNxHD/VC-3 parser
  3. * Copyright (c) 2008 Baptiste Coudurier <baptiste.coudurier@free.fr>
  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. /**
  22. * @file libavcodec/dnxhd_parser.c
  23. * DNxHD/VC-3 parser
  24. */
  25. #include "parser.h"
  26. #define DNXHD_HEADER_PREFIX 0x0000028001
  27. static int dnxhd_find_frame_end(ParseContext *pc,
  28. const uint8_t *buf, int buf_size)
  29. {
  30. uint64_t state = pc->state64;
  31. int pic_found = pc->frame_start_found;
  32. int i = 0;
  33. if (!pic_found) {
  34. for (i = 0; i < buf_size; i++) {
  35. state = (state<<8) | buf[i];
  36. if ((state & 0xffffffffffLL) == DNXHD_HEADER_PREFIX) {
  37. i++;
  38. pic_found = 1;
  39. break;
  40. }
  41. }
  42. }
  43. if (pic_found) {
  44. if (!buf_size) /* EOF considered as end of frame */
  45. return 0;
  46. for (; i < buf_size; i++) {
  47. state = (state<<8) | buf[i];
  48. if ((state & 0xffffffffffLL) == DNXHD_HEADER_PREFIX) {
  49. pc->frame_start_found = 0;
  50. pc->state64 = -1;
  51. return i-4;
  52. }
  53. }
  54. }
  55. pc->frame_start_found = pic_found;
  56. pc->state64 = state;
  57. return END_NOT_FOUND;
  58. }
  59. static int dnxhd_parse(AVCodecParserContext *s,
  60. AVCodecContext *avctx,
  61. const uint8_t **poutbuf, int *poutbuf_size,
  62. const uint8_t *buf, int buf_size)
  63. {
  64. ParseContext *pc = s->priv_data;
  65. int next;
  66. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  67. next = buf_size;
  68. } else {
  69. next = dnxhd_find_frame_end(pc, buf, buf_size);
  70. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  71. *poutbuf = NULL;
  72. *poutbuf_size = 0;
  73. return buf_size;
  74. }
  75. }
  76. *poutbuf = buf;
  77. *poutbuf_size = buf_size;
  78. return next;
  79. }
  80. AVCodecParser dnxhd_parser = {
  81. { CODEC_ID_DNXHD },
  82. sizeof(ParseContext),
  83. NULL,
  84. dnxhd_parse,
  85. ff_parse_close,
  86. };