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.

339 lines
10KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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, 1);
  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. ret = AVERROR(ENOMEM);
  91. goto fail;
  92. }
  93. filtered_data = filtered_buf->data;
  94. }
  95. extradata = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  96. if (!extradata) {
  97. av_buffer_unref(&filtered_buf);
  98. ret = AVERROR(ENOMEM);
  99. goto fail;
  100. }
  101. *data = extradata;
  102. *size = extradata_size;
  103. for (i = 0; i < h2645_pkt.nb_nals; i++) {
  104. H2645NAL *nal = &h2645_pkt.nals[i];
  105. if (val_in_array(extradata_nal_types, nb_extradata_nal_types,
  106. nal->type)) {
  107. AV_WB24(extradata, 1); // startcode
  108. memcpy(extradata + 3, nal->raw_data, nal->raw_size);
  109. extradata += 3 + nal->raw_size;
  110. } else if (s->remove) {
  111. AV_WB24(filtered_data, 1); // startcode
  112. memcpy(filtered_data + 3, nal->raw_data, nal->raw_size);
  113. filtered_data += 3 + nal->raw_size;
  114. }
  115. }
  116. if (s->remove) {
  117. av_buffer_unref(&pkt->buf);
  118. pkt->buf = filtered_buf;
  119. pkt->data = filtered_buf->data;
  120. pkt->size = filtered_data - filtered_buf->data;
  121. }
  122. }
  123. fail:
  124. ff_h2645_packet_uninit(&h2645_pkt);
  125. return ret;
  126. }
  127. static int extract_extradata_vc1(AVBSFContext *ctx, AVPacket *pkt,
  128. uint8_t **data, int *size)
  129. {
  130. ExtractExtradataContext *s = ctx->priv_data;
  131. const uint8_t *ptr = pkt->data, *end = pkt->data + pkt->size;
  132. uint32_t state = UINT32_MAX;
  133. int has_extradata = 0, extradata_size = 0;
  134. while (ptr < end) {
  135. ptr = avpriv_find_start_code(ptr, end, &state);
  136. if (state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT) {
  137. has_extradata = 1;
  138. } else if (has_extradata && IS_MARKER(state)) {
  139. extradata_size = ptr - 4 - pkt->data;
  140. break;
  141. }
  142. }
  143. if (extradata_size) {
  144. *data = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  145. if (!*data)
  146. return AVERROR(ENOMEM);
  147. memcpy(*data, pkt->data, extradata_size);
  148. *size = extradata_size;
  149. if (s->remove) {
  150. pkt->data += extradata_size;
  151. pkt->size -= extradata_size;
  152. }
  153. }
  154. return 0;
  155. }
  156. static int extract_extradata_mpeg12(AVBSFContext *ctx, AVPacket *pkt,
  157. uint8_t **data, int *size)
  158. {
  159. ExtractExtradataContext *s = ctx->priv_data;
  160. uint32_t state = UINT32_MAX;
  161. int i, found = 0;
  162. for (i = 0; i < pkt->size; i++) {
  163. state = (state << 8) | pkt->data[i];
  164. if (state == 0x1B3)
  165. found = 1;
  166. else if (found && state != 0x1B5 && state < 0x200 && state >= 0x100) {
  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 int extract_extradata_mpeg4(AVBSFContext *ctx, AVPacket *pkt,
  184. uint8_t **data, int *size)
  185. {
  186. ExtractExtradataContext *s = ctx->priv_data;
  187. const uint8_t *ptr = pkt->data, *end = pkt->data + pkt->size;
  188. uint32_t state = UINT32_MAX;
  189. while (ptr < end) {
  190. ptr = avpriv_find_start_code(ptr, end, &state);
  191. if (state == 0x1B3 || state == 0x1B6) {
  192. if (ptr - pkt->data > 4) {
  193. *size = ptr - 4 - pkt->data;
  194. *data = av_malloc(*size + AV_INPUT_BUFFER_PADDING_SIZE);
  195. if (!*data)
  196. return AVERROR(ENOMEM);
  197. memcpy(*data, pkt->data, *size);
  198. if (s->remove) {
  199. pkt->data += *size;
  200. pkt->size -= *size;
  201. }
  202. }
  203. break;
  204. }
  205. }
  206. return 0;
  207. }
  208. static const struct {
  209. enum AVCodecID id;
  210. int (*extract)(AVBSFContext *ctx, AVPacket *pkt,
  211. uint8_t **data, int *size);
  212. } extract_tab[] = {
  213. { AV_CODEC_ID_CAVS, extract_extradata_mpeg4 },
  214. { AV_CODEC_ID_H264, extract_extradata_h2645 },
  215. { AV_CODEC_ID_HEVC, extract_extradata_h2645 },
  216. { AV_CODEC_ID_MPEG1VIDEO, extract_extradata_mpeg12 },
  217. { AV_CODEC_ID_MPEG2VIDEO, extract_extradata_mpeg12 },
  218. { AV_CODEC_ID_MPEG4, extract_extradata_mpeg4 },
  219. { AV_CODEC_ID_VC1, extract_extradata_vc1 },
  220. };
  221. static int extract_extradata_init(AVBSFContext *ctx)
  222. {
  223. ExtractExtradataContext *s = ctx->priv_data;
  224. int i;
  225. for (i = 0; i < FF_ARRAY_ELEMS(extract_tab); i++) {
  226. if (extract_tab[i].id == ctx->par_in->codec_id) {
  227. s->extract = extract_tab[i].extract;
  228. break;
  229. }
  230. }
  231. if (!s->extract)
  232. return AVERROR_BUG;
  233. return 0;
  234. }
  235. static int extract_extradata_filter(AVBSFContext *ctx, AVPacket *out)
  236. {
  237. ExtractExtradataContext *s = ctx->priv_data;
  238. AVPacket *in;
  239. uint8_t *extradata = NULL;
  240. int extradata_size;
  241. int ret = 0;
  242. ret = ff_bsf_get_packet(ctx, &in);
  243. if (ret < 0)
  244. return ret;
  245. ret = s->extract(ctx, in, &extradata, &extradata_size);
  246. if (ret < 0)
  247. goto fail;
  248. if (extradata) {
  249. ret = av_packet_add_side_data(in, AV_PKT_DATA_NEW_EXTRADATA,
  250. extradata, extradata_size);
  251. if (ret < 0) {
  252. av_freep(&extradata);
  253. goto fail;
  254. }
  255. }
  256. av_packet_move_ref(out, in);
  257. fail:
  258. av_packet_free(&in);
  259. return ret;
  260. }
  261. static const enum AVCodecID codec_ids[] = {
  262. AV_CODEC_ID_CAVS,
  263. AV_CODEC_ID_H264,
  264. AV_CODEC_ID_HEVC,
  265. AV_CODEC_ID_MPEG1VIDEO,
  266. AV_CODEC_ID_MPEG2VIDEO,
  267. AV_CODEC_ID_MPEG4,
  268. AV_CODEC_ID_VC1,
  269. AV_CODEC_ID_NONE,
  270. };
  271. #define OFFSET(x) offsetof(ExtractExtradataContext, x)
  272. static const AVOption options[] = {
  273. { "remove", "remove the extradata from the bitstream", OFFSET(remove), AV_OPT_TYPE_INT,
  274. { .i64 = 0 }, 0, 1 },
  275. { NULL },
  276. };
  277. static const AVClass extract_extradata_class = {
  278. .class_name = "extract_extradata",
  279. .item_name = av_default_item_name,
  280. .option = options,
  281. .version = LIBAVUTIL_VERSION_INT,
  282. };
  283. const AVBitStreamFilter ff_extract_extradata_bsf = {
  284. .name = "extract_extradata",
  285. .codec_ids = codec_ids,
  286. .priv_data_size = sizeof(ExtractExtradataContext),
  287. .priv_class = &extract_extradata_class,
  288. .init = extract_extradata_init,
  289. .filter = extract_extradata_filter,
  290. };