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.

301 lines
8.7KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * Libav is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with Libav; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <stdint.h>
  19. #include "libavutil/common.h"
  20. #include "libavutil/intreadwrite.h"
  21. #include "libavutil/log.h"
  22. #include "libavutil/opt.h"
  23. #include "avcodec.h"
  24. #include "bsf.h"
  25. #include "h2645_parse.h"
  26. #include "h264.h"
  27. #include "hevc.h"
  28. #include "vc1_common.h"
  29. typedef struct ExtractExtradataContext {
  30. const AVClass *class;
  31. int (*extract)(AVBSFContext *ctx, AVPacket *pkt,
  32. uint8_t **data, int *size);
  33. /* AVOptions */
  34. int remove;
  35. } ExtractExtradataContext;
  36. static int val_in_array(const int *arr, int len, int val)
  37. {
  38. int i;
  39. for (i = 0; i < len; i++)
  40. if (arr[i] == val)
  41. return 1;
  42. return 0;
  43. }
  44. static int extract_extradata_h2645(AVBSFContext *ctx, AVPacket *pkt,
  45. uint8_t **data, int *size)
  46. {
  47. static const int extradata_nal_types_hevc[] = {
  48. HEVC_NAL_VPS, HEVC_NAL_SPS, HEVC_NAL_PPS,
  49. };
  50. static const int extradata_nal_types_h264[] = {
  51. H264_NAL_SPS, H264_NAL_PPS,
  52. };
  53. ExtractExtradataContext *s = ctx->priv_data;
  54. H2645Packet h2645_pkt = { 0 };
  55. int extradata_size = 0;
  56. const int *extradata_nal_types;
  57. int nb_extradata_nal_types;
  58. int i, ret = 0;
  59. if (ctx->par_in->codec_id == AV_CODEC_ID_HEVC) {
  60. extradata_nal_types = extradata_nal_types_hevc;
  61. nb_extradata_nal_types = FF_ARRAY_ELEMS(extradata_nal_types_hevc);
  62. } else {
  63. extradata_nal_types = extradata_nal_types_h264;
  64. nb_extradata_nal_types = FF_ARRAY_ELEMS(extradata_nal_types_h264);
  65. }
  66. ret = ff_h2645_packet_split(&h2645_pkt, pkt->data, pkt->size,
  67. ctx, 0, 0, ctx->par_in->codec_id);
  68. if (ret < 0)
  69. return ret;
  70. for (i = 0; i < h2645_pkt.nb_nals; i++) {
  71. H2645NAL *nal = &h2645_pkt.nals[i];
  72. if (val_in_array(extradata_nal_types, nb_extradata_nal_types, nal->type))
  73. extradata_size += nal->raw_size + 3;
  74. }
  75. if (extradata_size) {
  76. AVBufferRef *filtered_buf;
  77. uint8_t *extradata, *filtered_data;
  78. if (s->remove) {
  79. filtered_buf = av_buffer_alloc(pkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
  80. if (!filtered_buf)
  81. goto fail;
  82. filtered_data = filtered_buf->data;
  83. }
  84. extradata = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  85. if (!extradata) {
  86. av_buffer_unref(&filtered_buf);
  87. goto fail;
  88. }
  89. *data = extradata;
  90. *size = extradata_size;
  91. for (i = 0; i < h2645_pkt.nb_nals; i++) {
  92. H2645NAL *nal = &h2645_pkt.nals[i];
  93. if (val_in_array(extradata_nal_types, nb_extradata_nal_types,
  94. nal->type)) {
  95. AV_WB24(extradata, 1); // startcode
  96. memcpy(extradata + 3, nal->raw_data, nal->raw_size);
  97. extradata += 3 + nal->raw_size;
  98. } else if (s->remove) {
  99. AV_WB24(filtered_data, 1); // startcode
  100. memcpy(filtered_data + 3, nal->raw_data, nal->raw_size);
  101. filtered_data += 3 + nal->raw_size;
  102. }
  103. }
  104. if (s->remove) {
  105. av_buffer_unref(&pkt->buf);
  106. pkt->buf = filtered_buf;
  107. pkt->data = filtered_buf->data;
  108. pkt->size = filtered_data - filtered_buf->data;
  109. }
  110. }
  111. fail:
  112. ff_h2645_packet_uninit(&h2645_pkt);
  113. return ret;
  114. }
  115. static int extract_extradata_vc1(AVBSFContext *ctx, AVPacket *pkt,
  116. uint8_t **data, int *size)
  117. {
  118. ExtractExtradataContext *s = ctx->priv_data;
  119. uint32_t state = UINT32_MAX;
  120. int has_extradata = 0, extradata_size = 0;
  121. int i;
  122. for (i = 0; i < pkt->size; i++) {
  123. state = (state << 8) | pkt->data[i];
  124. if (IS_MARKER(state)) {
  125. if (state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT) {
  126. has_extradata = 1;
  127. } else if (has_extradata) {
  128. extradata_size = i - 3;
  129. break;
  130. }
  131. }
  132. }
  133. if (extradata_size) {
  134. *data = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  135. if (!*data)
  136. return AVERROR(ENOMEM);
  137. memcpy(*data, pkt->data, extradata_size);
  138. *size = extradata_size;
  139. if (s->remove) {
  140. pkt->data += extradata_size;
  141. pkt->size -= extradata_size;
  142. }
  143. }
  144. return 0;
  145. }
  146. static int extract_extradata_mpeg124(AVBSFContext *ctx, AVPacket *pkt,
  147. uint8_t **data, int *size)
  148. {
  149. ExtractExtradataContext *s = ctx->priv_data;
  150. int is_mpeg12 = ctx->par_in->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
  151. ctx->par_in->codec_id == AV_CODEC_ID_MPEG2VIDEO;
  152. uint32_t state = UINT32_MAX;
  153. int i;
  154. for (i = 0; i < pkt->size; i++) {
  155. state = (state << 8) | pkt->data[i];
  156. if ((is_mpeg12 && state != 0x1B3 && state != 0x1B5 && state < 0x200 && state >= 0x100) ||
  157. (!is_mpeg12 && (state == 0x1B3 || state == 0x1B6))) {
  158. if (i > 3) {
  159. *size = i - 3;
  160. *data = av_malloc(*size + AV_INPUT_BUFFER_PADDING_SIZE);
  161. if (!*data)
  162. return AVERROR(ENOMEM);
  163. memcpy(*data, pkt->data, *size);
  164. if (s->remove) {
  165. pkt->data += *size;
  166. pkt->size -= *size;
  167. }
  168. }
  169. break;
  170. }
  171. }
  172. return 0;
  173. }
  174. static const struct {
  175. enum AVCodecID id;
  176. int (*extract)(AVBSFContext *ctx, AVPacket *pkt,
  177. uint8_t **data, int *size);
  178. } extract_tab[] = {
  179. { AV_CODEC_ID_CAVS, extract_extradata_mpeg124 },
  180. { AV_CODEC_ID_H264, extract_extradata_h2645 },
  181. { AV_CODEC_ID_HEVC, extract_extradata_h2645 },
  182. { AV_CODEC_ID_MPEG1VIDEO, extract_extradata_mpeg124 },
  183. { AV_CODEC_ID_MPEG2VIDEO, extract_extradata_mpeg124 },
  184. { AV_CODEC_ID_MPEG4, extract_extradata_mpeg124 },
  185. { AV_CODEC_ID_VC1, extract_extradata_vc1 },
  186. };
  187. static int extract_extradata_init(AVBSFContext *ctx)
  188. {
  189. ExtractExtradataContext *s = ctx->priv_data;
  190. int i;
  191. for (i = 0; i < FF_ARRAY_ELEMS(extract_tab); i++) {
  192. if (extract_tab[i].id == ctx->par_in->codec_id) {
  193. s->extract = extract_tab[i].extract;
  194. break;
  195. }
  196. }
  197. if (!s->extract)
  198. return AVERROR_BUG;
  199. return 0;
  200. }
  201. static int extract_extradata_filter(AVBSFContext *ctx, AVPacket *out)
  202. {
  203. ExtractExtradataContext *s = ctx->priv_data;
  204. AVPacket *in;
  205. uint8_t *extradata = NULL;
  206. int extradata_size;
  207. int ret = 0;
  208. ret = ff_bsf_get_packet(ctx, &in);
  209. if (ret < 0)
  210. return ret;
  211. ret = s->extract(ctx, in, &extradata, &extradata_size);
  212. if (ret < 0)
  213. goto fail;
  214. if (extradata) {
  215. ret = av_packet_add_side_data(in, AV_PKT_DATA_NEW_EXTRADATA,
  216. extradata, extradata_size);
  217. if (ret < 0) {
  218. av_freep(&extradata);
  219. goto fail;
  220. }
  221. }
  222. av_packet_move_ref(out, in);
  223. fail:
  224. av_packet_free(&in);
  225. return ret;
  226. }
  227. static const enum AVCodecID codec_ids[] = {
  228. AV_CODEC_ID_CAVS,
  229. AV_CODEC_ID_H264,
  230. AV_CODEC_ID_HEVC,
  231. AV_CODEC_ID_MPEG1VIDEO,
  232. AV_CODEC_ID_MPEG2VIDEO,
  233. AV_CODEC_ID_MPEG4,
  234. AV_CODEC_ID_VC1,
  235. AV_CODEC_ID_NONE,
  236. };
  237. #define OFFSET(x) offsetof(ExtractExtradataContext, x)
  238. static const AVOption options[] = {
  239. { "remove", "remove the extradata from the bitstream", OFFSET(remove), AV_OPT_TYPE_INT,
  240. { .i64 = 0 }, 0, 1 },
  241. { NULL },
  242. };
  243. static const AVClass extract_extradata_class = {
  244. .class_name = "extract_extradata",
  245. .item_name = av_default_item_name,
  246. .option = options,
  247. .version = LIBAVUTIL_VERSION_INT,
  248. };
  249. const AVBitStreamFilter ff_extract_extradata_bsf = {
  250. .name = "extract_extradata",
  251. .codec_ids = codec_ids,
  252. .priv_data_size = sizeof(ExtractExtradataContext),
  253. .priv_class = &extract_extradata_class,
  254. .init = extract_extradata_init,
  255. .filter = extract_extradata_filter,
  256. };