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.

544 lines
13KB

  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 <string.h>
  19. #include "libavutil/log.h"
  20. #include "libavutil/mem.h"
  21. #include "libavutil/opt.h"
  22. #include "libavutil/avstring.h"
  23. #include "libavutil/bprint.h"
  24. #include "avcodec.h"
  25. #include "bsf.h"
  26. struct AVBSFInternal {
  27. AVPacket *buffer_pkt;
  28. int eof;
  29. };
  30. void av_bsf_free(AVBSFContext **pctx)
  31. {
  32. AVBSFContext *ctx;
  33. if (!pctx || !*pctx)
  34. return;
  35. ctx = *pctx;
  36. if (ctx->filter->close)
  37. ctx->filter->close(ctx);
  38. if (ctx->filter->priv_class && ctx->priv_data)
  39. av_opt_free(ctx->priv_data);
  40. av_opt_free(ctx);
  41. if (ctx->internal)
  42. av_packet_free(&ctx->internal->buffer_pkt);
  43. av_freep(&ctx->internal);
  44. av_freep(&ctx->priv_data);
  45. avcodec_parameters_free(&ctx->par_in);
  46. avcodec_parameters_free(&ctx->par_out);
  47. av_freep(pctx);
  48. }
  49. static void *bsf_child_next(void *obj, void *prev)
  50. {
  51. AVBSFContext *ctx = obj;
  52. if (!prev && ctx->filter->priv_class)
  53. return ctx->priv_data;
  54. return NULL;
  55. }
  56. static const AVClass bsf_class = {
  57. .class_name = "AVBSFContext",
  58. .item_name = av_default_item_name,
  59. .version = LIBAVUTIL_VERSION_INT,
  60. .child_next = bsf_child_next,
  61. .child_class_next = ff_bsf_child_class_next,
  62. };
  63. const AVClass *av_bsf_get_class(void)
  64. {
  65. return &bsf_class;
  66. }
  67. int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx)
  68. {
  69. AVBSFContext *ctx;
  70. int ret;
  71. ctx = av_mallocz(sizeof(*ctx));
  72. if (!ctx)
  73. return AVERROR(ENOMEM);
  74. ctx->av_class = &bsf_class;
  75. ctx->filter = filter;
  76. ctx->par_in = avcodec_parameters_alloc();
  77. ctx->par_out = avcodec_parameters_alloc();
  78. if (!ctx->par_in || !ctx->par_out) {
  79. ret = AVERROR(ENOMEM);
  80. goto fail;
  81. }
  82. ctx->internal = av_mallocz(sizeof(*ctx->internal));
  83. if (!ctx->internal) {
  84. ret = AVERROR(ENOMEM);
  85. goto fail;
  86. }
  87. ctx->internal->buffer_pkt = av_packet_alloc();
  88. if (!ctx->internal->buffer_pkt) {
  89. ret = AVERROR(ENOMEM);
  90. goto fail;
  91. }
  92. av_opt_set_defaults(ctx);
  93. /* allocate priv data and init private options */
  94. if (filter->priv_data_size) {
  95. ctx->priv_data = av_mallocz(filter->priv_data_size);
  96. if (!ctx->priv_data) {
  97. ret = AVERROR(ENOMEM);
  98. goto fail;
  99. }
  100. if (filter->priv_class) {
  101. *(const AVClass **)ctx->priv_data = filter->priv_class;
  102. av_opt_set_defaults(ctx->priv_data);
  103. }
  104. }
  105. *pctx = ctx;
  106. return 0;
  107. fail:
  108. av_bsf_free(&ctx);
  109. return ret;
  110. }
  111. int av_bsf_init(AVBSFContext *ctx)
  112. {
  113. int ret, i;
  114. /* check that the codec is supported */
  115. if (ctx->filter->codec_ids) {
  116. for (i = 0; ctx->filter->codec_ids[i] != AV_CODEC_ID_NONE; i++)
  117. if (ctx->par_in->codec_id == ctx->filter->codec_ids[i])
  118. break;
  119. if (ctx->filter->codec_ids[i] == AV_CODEC_ID_NONE) {
  120. const AVCodecDescriptor *desc = avcodec_descriptor_get(ctx->par_in->codec_id);
  121. av_log(ctx, AV_LOG_ERROR, "Codec '%s' (%d) is not supported by the "
  122. "bitstream filter '%s'. Supported codecs are: ",
  123. desc ? desc->name : "unknown", ctx->par_in->codec_id, ctx->filter->name);
  124. for (i = 0; ctx->filter->codec_ids[i] != AV_CODEC_ID_NONE; i++) {
  125. desc = avcodec_descriptor_get(ctx->filter->codec_ids[i]);
  126. av_log(ctx, AV_LOG_ERROR, "%s (%d) ",
  127. desc ? desc->name : "unknown", ctx->filter->codec_ids[i]);
  128. }
  129. av_log(ctx, AV_LOG_ERROR, "\n");
  130. return AVERROR(EINVAL);
  131. }
  132. }
  133. /* initialize output parameters to be the same as input
  134. * init below might overwrite that */
  135. ret = avcodec_parameters_copy(ctx->par_out, ctx->par_in);
  136. if (ret < 0)
  137. return ret;
  138. ctx->time_base_out = ctx->time_base_in;
  139. if (ctx->filter->init) {
  140. ret = ctx->filter->init(ctx);
  141. if (ret < 0)
  142. return ret;
  143. }
  144. return 0;
  145. }
  146. int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
  147. {
  148. if (!pkt || (!pkt->data && !pkt->side_data_elems)) {
  149. ctx->internal->eof = 1;
  150. return 0;
  151. }
  152. if (ctx->internal->eof) {
  153. av_log(ctx, AV_LOG_ERROR, "A non-NULL packet sent after an EOF.\n");
  154. return AVERROR(EINVAL);
  155. }
  156. if (ctx->internal->buffer_pkt->data ||
  157. ctx->internal->buffer_pkt->side_data_elems)
  158. return AVERROR(EAGAIN);
  159. av_packet_move_ref(ctx->internal->buffer_pkt, pkt);
  160. return 0;
  161. }
  162. int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
  163. {
  164. return ctx->filter->filter(ctx, pkt);
  165. }
  166. int ff_bsf_get_packet(AVBSFContext *ctx, AVPacket **pkt)
  167. {
  168. AVBSFInternal *in = ctx->internal;
  169. AVPacket *tmp_pkt;
  170. if (in->eof)
  171. return AVERROR_EOF;
  172. if (!ctx->internal->buffer_pkt->data &&
  173. !ctx->internal->buffer_pkt->side_data_elems)
  174. return AVERROR(EAGAIN);
  175. tmp_pkt = av_packet_alloc();
  176. if (!tmp_pkt)
  177. return AVERROR(ENOMEM);
  178. *pkt = ctx->internal->buffer_pkt;
  179. ctx->internal->buffer_pkt = tmp_pkt;
  180. return 0;
  181. }
  182. int ff_bsf_get_packet_ref(AVBSFContext *ctx, AVPacket *pkt)
  183. {
  184. AVBSFInternal *in = ctx->internal;
  185. if (in->eof)
  186. return AVERROR_EOF;
  187. if (!ctx->internal->buffer_pkt->data &&
  188. !ctx->internal->buffer_pkt->side_data_elems)
  189. return AVERROR(EAGAIN);
  190. av_packet_move_ref(pkt, ctx->internal->buffer_pkt);
  191. return 0;
  192. }
  193. typedef struct BSFListContext {
  194. const AVClass *class;
  195. AVBSFContext **bsfs;
  196. int nb_bsfs;
  197. unsigned idx; // index of currently processed BSF
  198. unsigned flushed_idx; // index of BSF being flushed
  199. char * item_name;
  200. } BSFListContext;
  201. static int bsf_list_init(AVBSFContext *bsf)
  202. {
  203. BSFListContext *lst = bsf->priv_data;
  204. int ret, i;
  205. const AVCodecParameters *cod_par = bsf->par_in;
  206. AVRational tb = bsf->time_base_in;
  207. for (i = 0; i < lst->nb_bsfs; ++i) {
  208. ret = avcodec_parameters_copy(lst->bsfs[i]->par_in, cod_par);
  209. if (ret < 0)
  210. goto fail;
  211. lst->bsfs[i]->time_base_in = tb;
  212. ret = av_bsf_init(lst->bsfs[i]);
  213. if (ret < 0)
  214. goto fail;
  215. cod_par = lst->bsfs[i]->par_out;
  216. tb = lst->bsfs[i]->time_base_out;
  217. }
  218. bsf->time_base_out = tb;
  219. ret = avcodec_parameters_copy(bsf->par_out, cod_par);
  220. fail:
  221. return ret;
  222. }
  223. static int bsf_list_filter(AVBSFContext *bsf, AVPacket *out)
  224. {
  225. BSFListContext *lst = bsf->priv_data;
  226. int ret;
  227. if (!lst->nb_bsfs)
  228. return ff_bsf_get_packet_ref(bsf, out);
  229. while (1) {
  230. if (lst->idx > lst->flushed_idx) {
  231. ret = av_bsf_receive_packet(lst->bsfs[lst->idx-1], out);
  232. if (ret == AVERROR(EAGAIN)) {
  233. /* no more packets from idx-1, try with previous */
  234. ret = 0;
  235. lst->idx--;
  236. continue;
  237. } else if (ret == AVERROR_EOF) {
  238. /* filter idx-1 is done, continue with idx...nb_bsfs */
  239. lst->flushed_idx = lst->idx;
  240. continue;
  241. }else if (ret < 0) {
  242. /* filtering error */
  243. break;
  244. }
  245. } else {
  246. ret = ff_bsf_get_packet_ref(bsf, out);
  247. if (ret == AVERROR_EOF) {
  248. lst->idx = lst->flushed_idx;
  249. } else if (ret < 0)
  250. break;
  251. }
  252. if (lst->idx < lst->nb_bsfs) {
  253. AVPacket *pkt;
  254. if (ret == AVERROR_EOF && lst->idx == lst->flushed_idx) {
  255. /* ff_bsf_get_packet_ref returned EOF and idx is first
  256. * filter of yet not flushed filter chain */
  257. pkt = NULL;
  258. } else {
  259. pkt = out;
  260. }
  261. ret = av_bsf_send_packet(lst->bsfs[lst->idx], pkt);
  262. if (ret < 0)
  263. break;
  264. lst->idx++;
  265. } else {
  266. /* The end of filter chain, break to return result */
  267. break;
  268. }
  269. }
  270. if (ret < 0)
  271. av_packet_unref(out);
  272. return ret;
  273. }
  274. static void bsf_list_close(AVBSFContext *bsf)
  275. {
  276. BSFListContext *lst = bsf->priv_data;
  277. int i;
  278. for (i = 0; i < lst->nb_bsfs; ++i)
  279. av_bsf_free(&lst->bsfs[i]);
  280. av_freep(&lst->bsfs);
  281. av_freep(&lst->item_name);
  282. }
  283. static const char *bsf_list_item_name(void *ctx)
  284. {
  285. static const char *null_filter_name = "null";
  286. AVBSFContext *bsf_ctx = ctx;
  287. BSFListContext *lst = bsf_ctx->priv_data;
  288. if (!lst->nb_bsfs)
  289. return null_filter_name;
  290. if (!lst->item_name) {
  291. int i;
  292. AVBPrint bp;
  293. av_bprint_init(&bp, 16, 128);
  294. av_bprintf(&bp, "bsf_list(");
  295. for (i = 0; i < lst->nb_bsfs; i++)
  296. av_bprintf(&bp, i ? ",%s" : "%s", lst->bsfs[i]->filter->name);
  297. av_bprintf(&bp, ")");
  298. av_bprint_finalize(&bp, &lst->item_name);
  299. }
  300. return lst->item_name;
  301. }
  302. static const AVClass bsf_list_class = {
  303. .class_name = "bsf_list",
  304. .item_name = bsf_list_item_name,
  305. .version = LIBAVUTIL_VERSION_INT,
  306. };
  307. const AVBitStreamFilter ff_list_bsf = {
  308. .name = "bsf_list",
  309. .priv_data_size = sizeof(BSFListContext),
  310. .priv_class = &bsf_list_class,
  311. .init = bsf_list_init,
  312. .filter = bsf_list_filter,
  313. .close = bsf_list_close,
  314. };
  315. struct AVBSFList {
  316. AVBSFContext **bsfs;
  317. int nb_bsfs;
  318. };
  319. AVBSFList *av_bsf_list_alloc(void)
  320. {
  321. return av_mallocz(sizeof(AVBSFList));
  322. }
  323. void av_bsf_list_free(AVBSFList **lst)
  324. {
  325. int i;
  326. if (!*lst)
  327. return;
  328. for (i = 0; i < (*lst)->nb_bsfs; ++i)
  329. av_bsf_free(&(*lst)->bsfs[i]);
  330. av_free((*lst)->bsfs);
  331. av_freep(lst);
  332. }
  333. int av_bsf_list_append(AVBSFList *lst, AVBSFContext *bsf)
  334. {
  335. return av_dynarray_add_nofree(&lst->bsfs, &lst->nb_bsfs, bsf);
  336. }
  337. int av_bsf_list_append2(AVBSFList *lst, const char *bsf_name, AVDictionary ** options)
  338. {
  339. int ret;
  340. const AVBitStreamFilter *filter;
  341. AVBSFContext *bsf;
  342. filter = av_bsf_get_by_name(bsf_name);
  343. if (!filter)
  344. return AVERROR_BSF_NOT_FOUND;
  345. ret = av_bsf_alloc(filter, &bsf);
  346. if (ret < 0)
  347. return ret;
  348. if (options) {
  349. ret = av_opt_set_dict2(bsf, options, AV_OPT_SEARCH_CHILDREN);
  350. if (ret < 0)
  351. goto end;
  352. }
  353. ret = av_bsf_list_append(lst, bsf);
  354. end:
  355. if (ret < 0)
  356. av_bsf_free(&bsf);
  357. return ret;
  358. }
  359. int av_bsf_list_finalize(AVBSFList **lst, AVBSFContext **bsf)
  360. {
  361. int ret = 0;
  362. BSFListContext *ctx;
  363. if ((*lst)->nb_bsfs == 1) {
  364. *bsf = (*lst)->bsfs[0];
  365. av_freep(&(*lst)->bsfs);
  366. (*lst)->nb_bsfs = 0;
  367. goto end;
  368. }
  369. ret = av_bsf_alloc(&ff_list_bsf, bsf);
  370. if (ret < 0)
  371. return ret;
  372. ctx = (*bsf)->priv_data;
  373. ctx->bsfs = (*lst)->bsfs;
  374. ctx->nb_bsfs = (*lst)->nb_bsfs;
  375. end:
  376. av_freep(lst);
  377. return ret;
  378. }
  379. static int bsf_parse_single(const char *str, AVBSFList *bsf_lst)
  380. {
  381. char *bsf_name, *bsf_options_str, *buf;
  382. AVDictionary *bsf_options = NULL;
  383. int ret = 0;
  384. if (!(buf = av_strdup(str)))
  385. return AVERROR(ENOMEM);
  386. bsf_name = av_strtok(buf, "=", &bsf_options_str);
  387. if (!bsf_name) {
  388. ret = AVERROR(EINVAL);
  389. goto end;
  390. }
  391. if (bsf_options_str) {
  392. ret = av_dict_parse_string(&bsf_options, bsf_options_str, "=", ":", 0);
  393. if (ret < 0)
  394. goto end;
  395. }
  396. ret = av_bsf_list_append2(bsf_lst, bsf_name, &bsf_options);
  397. av_dict_free(&bsf_options);
  398. end:
  399. av_free(buf);
  400. return ret;
  401. }
  402. int av_bsf_list_parse_str(const char *str, AVBSFContext **bsf_lst)
  403. {
  404. AVBSFList *lst;
  405. char *bsf_str, *buf, *dup, *saveptr;
  406. int ret;
  407. if (!str)
  408. return av_bsf_get_null_filter(bsf_lst);
  409. lst = av_bsf_list_alloc();
  410. if (!lst)
  411. return AVERROR(ENOMEM);
  412. if (!(dup = buf = av_strdup(str))) {
  413. ret = AVERROR(ENOMEM);
  414. goto end;
  415. }
  416. while (1) {
  417. bsf_str = av_strtok(buf, ",", &saveptr);
  418. if (!bsf_str)
  419. break;
  420. ret = bsf_parse_single(bsf_str, lst);
  421. if (ret < 0)
  422. goto end;
  423. buf = NULL;
  424. }
  425. ret = av_bsf_list_finalize(&lst, bsf_lst);
  426. end:
  427. if (ret < 0)
  428. av_bsf_list_free(&lst);
  429. av_free(dup);
  430. return ret;
  431. }
  432. int av_bsf_get_null_filter(AVBSFContext **bsf)
  433. {
  434. return av_bsf_alloc(&ff_list_bsf, bsf);
  435. }