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.

229 lines
6.8KB

  1. /*
  2. * AV1 parser
  3. *
  4. * Copyright (C) 2018 James Almer <jamrial@gmail.com>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "av1_parse.h"
  23. #include "cbs.h"
  24. #include "cbs_av1.h"
  25. #include "parser.h"
  26. typedef struct AV1ParseContext {
  27. CodedBitstreamContext *cbc;
  28. CodedBitstreamFragment temporal_unit;
  29. int parsed_extradata;
  30. } AV1ParseContext;
  31. static const enum AVPixelFormat pix_fmts_8bit[2][2] = {
  32. { AV_PIX_FMT_YUV444P, AV_PIX_FMT_NONE },
  33. { AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P },
  34. };
  35. static const enum AVPixelFormat pix_fmts_10bit[2][2] = {
  36. { AV_PIX_FMT_YUV444P10, AV_PIX_FMT_NONE },
  37. { AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV420P10 },
  38. };
  39. static const enum AVPixelFormat pix_fmts_12bit[2][2] = {
  40. { AV_PIX_FMT_YUV444P12, AV_PIX_FMT_NONE },
  41. { AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12 },
  42. };
  43. static int av1_parser_parse(AVCodecParserContext *ctx,
  44. AVCodecContext *avctx,
  45. const uint8_t **out_data, int *out_size,
  46. const uint8_t *data, int size)
  47. {
  48. AV1ParseContext *s = ctx->priv_data;
  49. CodedBitstreamFragment *td = &s->temporal_unit;
  50. CodedBitstreamAV1Context *av1 = s->cbc->priv_data;
  51. int ret;
  52. *out_data = data;
  53. *out_size = size;
  54. ctx->key_frame = -1;
  55. ctx->pict_type = AV_PICTURE_TYPE_NONE;
  56. ctx->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
  57. s->cbc->log_ctx = avctx;
  58. if (avctx->extradata_size && !s->parsed_extradata) {
  59. s->parsed_extradata = 1;
  60. ret = ff_cbs_read(s->cbc, td, avctx->extradata, avctx->extradata_size);
  61. if (ret < 0) {
  62. av_log(avctx, AV_LOG_WARNING, "Failed to parse extradata.\n");
  63. }
  64. ff_cbs_fragment_reset(s->cbc, td);
  65. }
  66. ret = ff_cbs_read(s->cbc, td, data, size);
  67. if (ret < 0) {
  68. av_log(avctx, AV_LOG_ERROR, "Failed to parse temporal unit.\n");
  69. goto end;
  70. }
  71. if (!av1->sequence_header) {
  72. av_log(avctx, AV_LOG_ERROR, "No sequence header available\n");
  73. goto end;
  74. }
  75. for (int i = 0; i < td->nb_units; i++) {
  76. CodedBitstreamUnit *unit = &td->units[i];
  77. AV1RawOBU *obu = unit->content;
  78. AV1RawSequenceHeader *seq = av1->sequence_header;
  79. AV1RawColorConfig *color = &seq->color_config;
  80. AV1RawFrameHeader *frame;
  81. int frame_type;
  82. if (unit->type == AV1_OBU_FRAME)
  83. frame = &obu->obu.frame.header;
  84. else if (unit->type == AV1_OBU_FRAME_HEADER)
  85. frame = &obu->obu.frame_header;
  86. else
  87. continue;
  88. if (frame->show_existing_frame) {
  89. AV1ReferenceFrameState *ref = &av1->ref[frame->frame_to_show_map_idx];
  90. if (!ref->valid) {
  91. av_log(avctx, AV_LOG_ERROR, "Invalid reference frame\n");
  92. goto end;
  93. }
  94. ctx->width = ref->frame_width;
  95. ctx->height = ref->frame_height;
  96. frame_type = ref->frame_type;
  97. ctx->key_frame = 0;
  98. } else if (!frame->show_frame) {
  99. continue;
  100. } else {
  101. ctx->width = av1->frame_width;
  102. ctx->height = av1->frame_height;
  103. frame_type = frame->frame_type;
  104. ctx->key_frame = frame_type == AV1_FRAME_KEY;
  105. }
  106. avctx->profile = seq->seq_profile;
  107. avctx->level = seq->seq_level_idx[0];
  108. switch (frame_type) {
  109. case AV1_FRAME_KEY:
  110. case AV1_FRAME_INTRA_ONLY:
  111. ctx->pict_type = AV_PICTURE_TYPE_I;
  112. break;
  113. case AV1_FRAME_INTER:
  114. ctx->pict_type = AV_PICTURE_TYPE_P;
  115. break;
  116. case AV1_FRAME_SWITCH:
  117. ctx->pict_type = AV_PICTURE_TYPE_SP;
  118. break;
  119. }
  120. ctx->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
  121. switch (av1->bit_depth) {
  122. case 8:
  123. ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY8
  124. : pix_fmts_8bit [color->subsampling_x][color->subsampling_y];
  125. break;
  126. case 10:
  127. ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY10
  128. : pix_fmts_10bit[color->subsampling_x][color->subsampling_y];
  129. break;
  130. case 12:
  131. ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY12
  132. : pix_fmts_12bit[color->subsampling_x][color->subsampling_y];
  133. break;
  134. }
  135. av_assert2(ctx->format != AV_PIX_FMT_NONE);
  136. }
  137. end:
  138. ff_cbs_fragment_reset(s->cbc, td);
  139. s->cbc->log_ctx = NULL;
  140. return size;
  141. }
  142. static const CodedBitstreamUnitType decompose_unit_types[] = {
  143. AV1_OBU_TEMPORAL_DELIMITER,
  144. AV1_OBU_SEQUENCE_HEADER,
  145. AV1_OBU_FRAME_HEADER,
  146. AV1_OBU_TILE_GROUP,
  147. AV1_OBU_FRAME,
  148. };
  149. static av_cold int av1_parser_init(AVCodecParserContext *ctx)
  150. {
  151. AV1ParseContext *s = ctx->priv_data;
  152. int ret;
  153. ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_AV1, NULL);
  154. if (ret < 0)
  155. return ret;
  156. s->cbc->decompose_unit_types = (CodedBitstreamUnitType *)decompose_unit_types;
  157. s->cbc->nb_decompose_unit_types = FF_ARRAY_ELEMS(decompose_unit_types);
  158. return 0;
  159. }
  160. static void av1_parser_close(AVCodecParserContext *ctx)
  161. {
  162. AV1ParseContext *s = ctx->priv_data;
  163. ff_cbs_fragment_free(s->cbc, &s->temporal_unit);
  164. ff_cbs_close(&s->cbc);
  165. }
  166. static int av1_parser_split(AVCodecContext *avctx,
  167. const uint8_t *buf, int buf_size)
  168. {
  169. AV1OBU obu;
  170. const uint8_t *ptr = buf, *end = buf + buf_size;
  171. while (ptr < end) {
  172. int len = ff_av1_extract_obu(&obu, ptr, buf_size, avctx);
  173. if (len < 0)
  174. break;
  175. if (obu.type == AV1_OBU_FRAME_HEADER ||
  176. obu.type == AV1_OBU_FRAME) {
  177. return ptr - buf;
  178. }
  179. ptr += len;
  180. buf_size -= len;
  181. }
  182. return 0;
  183. }
  184. AVCodecParser ff_av1_parser = {
  185. .codec_ids = { AV_CODEC_ID_AV1 },
  186. .priv_data_size = sizeof(AV1ParseContext),
  187. .parser_init = av1_parser_init,
  188. .parser_close = av1_parser_close,
  189. .parser_parse = av1_parser_parse,
  190. .split = av1_parser_split,
  191. };