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.

310 lines
9.2KB

  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, has_sps = 0, has_vps = 0, 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. if (ctx->par_in->codec_id == AV_CODEC_ID_HEVC) {
  75. if (nal->type == HEVC_NAL_SPS) has_sps = 1;
  76. if (nal->type == HEVC_NAL_VPS) has_vps = 1;
  77. } else {
  78. if (nal->type == H264_NAL_SPS) has_sps = 1;
  79. }
  80. }
  81. }
  82. if (extradata_size &&
  83. ((ctx->par_in->codec_id == AV_CODEC_ID_HEVC && has_sps && has_vps) ||
  84. (ctx->par_in->codec_id == AV_CODEC_ID_H264 && has_sps))) {
  85. AVBufferRef *filtered_buf;
  86. uint8_t *extradata, *filtered_data;
  87. if (s->remove) {
  88. filtered_buf = av_buffer_alloc(pkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
  89. if (!filtered_buf)
  90. goto fail;
  91. filtered_data = filtered_buf->data;
  92. }
  93. extradata = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  94. if (!extradata) {
  95. av_buffer_unref(&filtered_buf);
  96. goto fail;
  97. }
  98. *data = extradata;
  99. *size = extradata_size;
  100. for (i = 0; i < h2645_pkt.nb_nals; i++) {
  101. H2645NAL *nal = &h2645_pkt.nals[i];
  102. if (val_in_array(extradata_nal_types, nb_extradata_nal_types,
  103. nal->type)) {
  104. AV_WB24(extradata, 1); // startcode
  105. memcpy(extradata + 3, nal->raw_data, nal->raw_size);
  106. extradata += 3 + nal->raw_size;
  107. } else if (s->remove) {
  108. AV_WB24(filtered_data, 1); // startcode
  109. memcpy(filtered_data + 3, nal->raw_data, nal->raw_size);
  110. filtered_data += 3 + nal->raw_size;
  111. }
  112. }
  113. if (s->remove) {
  114. av_buffer_unref(&pkt->buf);
  115. pkt->buf = filtered_buf;
  116. pkt->data = filtered_buf->data;
  117. pkt->size = filtered_data - filtered_buf->data;
  118. }
  119. }
  120. fail:
  121. ff_h2645_packet_uninit(&h2645_pkt);
  122. return ret;
  123. }
  124. static int extract_extradata_vc1(AVBSFContext *ctx, AVPacket *pkt,
  125. uint8_t **data, int *size)
  126. {
  127. ExtractExtradataContext *s = ctx->priv_data;
  128. uint32_t state = UINT32_MAX;
  129. int has_extradata = 0, extradata_size = 0;
  130. int i;
  131. for (i = 0; i < pkt->size; i++) {
  132. state = (state << 8) | pkt->data[i];
  133. if (IS_MARKER(state)) {
  134. if (state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT) {
  135. has_extradata = 1;
  136. } else if (has_extradata) {
  137. extradata_size = i - 3;
  138. break;
  139. }
  140. }
  141. }
  142. if (extradata_size) {
  143. *data = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  144. if (!*data)
  145. return AVERROR(ENOMEM);
  146. memcpy(*data, pkt->data, extradata_size);
  147. *size = extradata_size;
  148. if (s->remove) {
  149. pkt->data += extradata_size;
  150. pkt->size -= extradata_size;
  151. }
  152. }
  153. return 0;
  154. }
  155. static int extract_extradata_mpeg124(AVBSFContext *ctx, AVPacket *pkt,
  156. uint8_t **data, int *size)
  157. {
  158. ExtractExtradataContext *s = ctx->priv_data;
  159. int is_mpeg12 = ctx->par_in->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
  160. ctx->par_in->codec_id == AV_CODEC_ID_MPEG2VIDEO;
  161. uint32_t state = UINT32_MAX;
  162. int i;
  163. for (i = 0; i < pkt->size; i++) {
  164. state = (state << 8) | pkt->data[i];
  165. if ((is_mpeg12 && state != 0x1B3 && state != 0x1B5 && state < 0x200 && state >= 0x100) ||
  166. (!is_mpeg12 && (state == 0x1B3 || state == 0x1B6))) {
  167. if (i > 3) {
  168. *size = i - 3;
  169. *data = av_malloc(*size + AV_INPUT_BUFFER_PADDING_SIZE);
  170. if (!*data)
  171. return AVERROR(ENOMEM);
  172. memcpy(*data, pkt->data, *size);
  173. if (s->remove) {
  174. pkt->data += *size;
  175. pkt->size -= *size;
  176. }
  177. }
  178. break;
  179. }
  180. }
  181. return 0;
  182. }
  183. static const struct {
  184. enum AVCodecID id;
  185. int (*extract)(AVBSFContext *ctx, AVPacket *pkt,
  186. uint8_t **data, int *size);
  187. } extract_tab[] = {
  188. { AV_CODEC_ID_CAVS, extract_extradata_mpeg124 },
  189. { AV_CODEC_ID_H264, extract_extradata_h2645 },
  190. { AV_CODEC_ID_HEVC, extract_extradata_h2645 },
  191. { AV_CODEC_ID_MPEG1VIDEO, extract_extradata_mpeg124 },
  192. { AV_CODEC_ID_MPEG2VIDEO, extract_extradata_mpeg124 },
  193. { AV_CODEC_ID_MPEG4, extract_extradata_mpeg124 },
  194. { AV_CODEC_ID_VC1, extract_extradata_vc1 },
  195. };
  196. static int extract_extradata_init(AVBSFContext *ctx)
  197. {
  198. ExtractExtradataContext *s = ctx->priv_data;
  199. int i;
  200. for (i = 0; i < FF_ARRAY_ELEMS(extract_tab); i++) {
  201. if (extract_tab[i].id == ctx->par_in->codec_id) {
  202. s->extract = extract_tab[i].extract;
  203. break;
  204. }
  205. }
  206. if (!s->extract)
  207. return AVERROR_BUG;
  208. return 0;
  209. }
  210. static int extract_extradata_filter(AVBSFContext *ctx, AVPacket *out)
  211. {
  212. ExtractExtradataContext *s = ctx->priv_data;
  213. AVPacket *in;
  214. uint8_t *extradata = NULL;
  215. int extradata_size;
  216. int ret = 0;
  217. ret = ff_bsf_get_packet(ctx, &in);
  218. if (ret < 0)
  219. return ret;
  220. ret = s->extract(ctx, in, &extradata, &extradata_size);
  221. if (ret < 0)
  222. goto fail;
  223. if (extradata) {
  224. ret = av_packet_add_side_data(in, AV_PKT_DATA_NEW_EXTRADATA,
  225. extradata, extradata_size);
  226. if (ret < 0) {
  227. av_freep(&extradata);
  228. goto fail;
  229. }
  230. }
  231. av_packet_move_ref(out, in);
  232. fail:
  233. av_packet_free(&in);
  234. return ret;
  235. }
  236. static const enum AVCodecID codec_ids[] = {
  237. AV_CODEC_ID_CAVS,
  238. AV_CODEC_ID_H264,
  239. AV_CODEC_ID_HEVC,
  240. AV_CODEC_ID_MPEG1VIDEO,
  241. AV_CODEC_ID_MPEG2VIDEO,
  242. AV_CODEC_ID_MPEG4,
  243. AV_CODEC_ID_VC1,
  244. AV_CODEC_ID_NONE,
  245. };
  246. #define OFFSET(x) offsetof(ExtractExtradataContext, x)
  247. static const AVOption options[] = {
  248. { "remove", "remove the extradata from the bitstream", OFFSET(remove), AV_OPT_TYPE_INT,
  249. { .i64 = 0 }, 0, 1 },
  250. { NULL },
  251. };
  252. static const AVClass extract_extradata_class = {
  253. .class_name = "extract_extradata",
  254. .item_name = av_default_item_name,
  255. .option = options,
  256. .version = LIBAVUTIL_VERSION_INT,
  257. };
  258. const AVBitStreamFilter ff_extract_extradata_bsf = {
  259. .name = "extract_extradata",
  260. .codec_ids = codec_ids,
  261. .priv_data_size = sizeof(ExtractExtradataContext),
  262. .priv_class = &extract_extradata_class,
  263. .init = extract_extradata_init,
  264. .filter = extract_extradata_filter,
  265. };