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.

173 lines
4.5KB

  1. /*
  2. * Filter layer - format negotiation
  3. * Copyright (c) 2007 Bobby Bingham
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/pixdesc.h"
  22. #include "avfilter.h"
  23. /**
  24. * Add all refs from a to ret and destroy a.
  25. */
  26. static void merge_ref(AVFilterFormats *ret, AVFilterFormats *a)
  27. {
  28. int i;
  29. for(i = 0; i < a->refcount; i ++) {
  30. ret->refs[ret->refcount] = a->refs[i];
  31. *ret->refs[ret->refcount++] = ret;
  32. }
  33. av_free(a->refs);
  34. av_free(a->formats);
  35. av_free(a);
  36. }
  37. AVFilterFormats *avfilter_merge_formats(AVFilterFormats *a, AVFilterFormats *b)
  38. {
  39. AVFilterFormats *ret;
  40. unsigned i, j, k = 0;
  41. ret = av_mallocz(sizeof(AVFilterFormats));
  42. /* merge list of formats */
  43. ret->formats = av_malloc(sizeof(*ret->formats) * FFMIN(a->format_count,
  44. b->format_count));
  45. for(i = 0; i < a->format_count; i ++)
  46. for(j = 0; j < b->format_count; j ++)
  47. if(a->formats[i] == b->formats[j])
  48. ret->formats[k++] = a->formats[i];
  49. ret->format_count = k;
  50. /* check that there was at least one common format */
  51. if(!ret->format_count) {
  52. av_free(ret->formats);
  53. av_free(ret);
  54. return NULL;
  55. }
  56. ret->refs = av_malloc(sizeof(AVFilterFormats**)*(a->refcount+b->refcount));
  57. merge_ref(ret, a);
  58. merge_ref(ret, b);
  59. return ret;
  60. }
  61. AVFilterFormats *avfilter_make_format_list(const int *fmts)
  62. {
  63. AVFilterFormats *formats;
  64. int count = 0;
  65. if (fmts)
  66. for (count = 0; fmts[count] != -1; count++)
  67. ;
  68. formats = av_mallocz(sizeof(AVFilterFormats));
  69. formats->format_count = count;
  70. if (count) {
  71. formats->formats = av_malloc(sizeof(*formats->formats) * count);
  72. memcpy(formats->formats, fmts, sizeof(*formats->formats) * count);
  73. }
  74. return formats;
  75. }
  76. int avfilter_add_format(AVFilterFormats **avff, int fmt)
  77. {
  78. int *fmts;
  79. if (!(*avff) && !(*avff = av_mallocz(sizeof(AVFilterFormats))))
  80. return AVERROR(ENOMEM);
  81. fmts = av_realloc((*avff)->formats,
  82. sizeof((*avff)->formats) * ((*avff)->format_count+1));
  83. if (!fmts)
  84. return AVERROR(ENOMEM);
  85. (*avff)->formats = fmts;
  86. (*avff)->formats[(*avff)->format_count++] = fmt;
  87. return 0;
  88. }
  89. AVFilterFormats *avfilter_all_formats(enum AVMediaType type)
  90. {
  91. AVFilterFormats *ret = NULL;
  92. int fmt;
  93. int num_formats = type == AVMEDIA_TYPE_VIDEO ? PIX_FMT_NB :
  94. type == AVMEDIA_TYPE_AUDIO ? AV_SAMPLE_FMT_NB : 0;
  95. for (fmt = 0; fmt < num_formats; fmt++)
  96. if ((type != AVMEDIA_TYPE_VIDEO) ||
  97. (type == AVMEDIA_TYPE_VIDEO && !(av_pix_fmt_descriptors[fmt].flags & PIX_FMT_HWACCEL)))
  98. avfilter_add_format(&ret, fmt);
  99. return ret;
  100. }
  101. void avfilter_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
  102. {
  103. *ref = f;
  104. f->refs = av_realloc(f->refs, sizeof(AVFilterFormats**) * ++f->refcount);
  105. f->refs[f->refcount-1] = ref;
  106. }
  107. static int find_ref_index(AVFilterFormats **ref)
  108. {
  109. int i;
  110. for(i = 0; i < (*ref)->refcount; i ++)
  111. if((*ref)->refs[i] == ref)
  112. return i;
  113. return -1;
  114. }
  115. void avfilter_formats_unref(AVFilterFormats **ref)
  116. {
  117. int idx;
  118. if (!*ref)
  119. return;
  120. idx = find_ref_index(ref);
  121. if(idx >= 0)
  122. memmove((*ref)->refs + idx, (*ref)->refs + idx+1,
  123. sizeof(AVFilterFormats**) * ((*ref)->refcount-idx-1));
  124. if(!--(*ref)->refcount) {
  125. av_free((*ref)->formats);
  126. av_free((*ref)->refs);
  127. av_free(*ref);
  128. }
  129. *ref = NULL;
  130. }
  131. void avfilter_formats_changeref(AVFilterFormats **oldref,
  132. AVFilterFormats **newref)
  133. {
  134. int idx = find_ref_index(oldref);
  135. if(idx >= 0) {
  136. (*oldref)->refs[idx] = newref;
  137. *newref = *oldref;
  138. *oldref = NULL;
  139. }
  140. }