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.

117 lines
3.6KB

  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
  23. * DNxHD/VC-3 parser
  24. */
  25. #include "parser.h"
  26. #define DNXHD_HEADER_PREFIX 0x000002800100
  27. typedef struct {
  28. ParseContext pc;
  29. int interlaced;
  30. int cur_field; /* first field is 0, second is 1 */
  31. } DNXHDParserContext;
  32. static int dnxhd_find_frame_end(DNXHDParserContext *dctx,
  33. const uint8_t *buf, int buf_size)
  34. {
  35. ParseContext *pc = &dctx->pc;
  36. uint64_t state = pc->state64;
  37. int pic_found = pc->frame_start_found;
  38. int i = 0;
  39. int interlaced = dctx->interlaced;
  40. int cur_field = dctx->cur_field;
  41. if (!pic_found) {
  42. for (i = 0; i < buf_size; i++) {
  43. state = (state<<8) | buf[i];
  44. if ((state & 0xffffffffff00LL) == DNXHD_HEADER_PREFIX) {
  45. i++;
  46. pic_found = 1;
  47. interlaced = (state&2)>>1; /* byte following the 5-byte header prefix */
  48. cur_field = state&1;
  49. break;
  50. }
  51. }
  52. }
  53. if (pic_found) {
  54. if (!buf_size) /* EOF considered as end of frame */
  55. return 0;
  56. for (; i < buf_size; i++) {
  57. state = (state<<8) | buf[i];
  58. if ((state & 0xffffffffff00LL) == DNXHD_HEADER_PREFIX) {
  59. if (!interlaced || dctx->cur_field) {
  60. pc->frame_start_found = 0;
  61. pc->state64 = -1;
  62. dctx->interlaced = interlaced;
  63. dctx->cur_field = 0;
  64. return i-5;
  65. } else {
  66. /* continue, to get the second field */
  67. dctx->interlaced = interlaced = (state&2)>>1;
  68. dctx->cur_field = cur_field = state&1;
  69. }
  70. }
  71. }
  72. }
  73. pc->frame_start_found = pic_found;
  74. pc->state64 = state;
  75. dctx->interlaced = interlaced;
  76. dctx->cur_field = cur_field;
  77. return END_NOT_FOUND;
  78. }
  79. static int dnxhd_parse(AVCodecParserContext *s,
  80. AVCodecContext *avctx,
  81. const uint8_t **poutbuf, int *poutbuf_size,
  82. const uint8_t *buf, int buf_size)
  83. {
  84. DNXHDParserContext *dctx = s->priv_data;
  85. ParseContext *pc = &dctx->pc;
  86. int next;
  87. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  88. next = buf_size;
  89. } else {
  90. next = dnxhd_find_frame_end(dctx, buf, buf_size);
  91. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  92. *poutbuf = NULL;
  93. *poutbuf_size = 0;
  94. return buf_size;
  95. }
  96. }
  97. *poutbuf = buf;
  98. *poutbuf_size = buf_size;
  99. return next;
  100. }
  101. AVCodecParser ff_dnxhd_parser = {
  102. .codec_ids = { AV_CODEC_ID_DNXHD },
  103. .priv_data_size = sizeof(DNXHDParserContext),
  104. .parser_parse = dnxhd_parse,
  105. .parser_close = ff_parse_close,
  106. };