PageRenderTime 42ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/libavformat/format.c

https://github.com/sorcererq/FFmpeg
C | 184 lines | 136 code | 20 blank | 28 comment | 46 complexity | 1286c318723db54117ceddaa696bcf91 MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0, LGPL-2.1, LGPL-3.0, CC-BY-SA-3.0
  1. /*
  2. * Format register and lookup
  3. * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
  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 "avformat.h"
  22. #include "internal.h"
  23. #include "libavutil/atomic.h"
  24. #include "libavutil/avstring.h"
  25. /**
  26. * @file
  27. * Format register and lookup
  28. */
  29. /** head of registered input format linked list */
  30. static AVInputFormat *first_iformat = NULL;
  31. /** head of registered output format linked list */
  32. static AVOutputFormat *first_oformat = NULL;
  33. AVInputFormat *av_iformat_next(AVInputFormat *f)
  34. {
  35. if (f)
  36. return f->next;
  37. else
  38. return first_iformat;
  39. }
  40. AVOutputFormat *av_oformat_next(AVOutputFormat *f)
  41. {
  42. if (f)
  43. return f->next;
  44. else
  45. return first_oformat;
  46. }
  47. void av_register_input_format(AVInputFormat *format)
  48. {
  49. AVInputFormat **p = &first_iformat;
  50. format->next = NULL;
  51. while(avpriv_atomic_ptr_cas((void * volatile *)p, NULL, format))
  52. p = &(*p)->next;
  53. }
  54. void av_register_output_format(AVOutputFormat *format)
  55. {
  56. AVOutputFormat **p = &first_oformat;
  57. format->next = NULL;
  58. while(avpriv_atomic_ptr_cas((void * volatile *)p, NULL, format))
  59. p = &(*p)->next;
  60. }
  61. int av_match_ext(const char *filename, const char *extensions)
  62. {
  63. const char *ext, *p;
  64. char ext1[32], *q;
  65. if (!filename)
  66. return 0;
  67. ext = strrchr(filename, '.');
  68. if (ext) {
  69. ext++;
  70. p = extensions;
  71. for (;;) {
  72. q = ext1;
  73. while (*p != '\0' && *p != ',' && q - ext1 < sizeof(ext1) - 1)
  74. *q++ = *p++;
  75. *q = '\0';
  76. if (!av_strcasecmp(ext1, ext))
  77. return 1;
  78. if (*p == '\0')
  79. break;
  80. p++;
  81. }
  82. }
  83. return 0;
  84. }
  85. static int match_format(const char *name, const char *names)
  86. {
  87. const char *p;
  88. int len, namelen;
  89. if (!name || !names)
  90. return 0;
  91. namelen = strlen(name);
  92. while ((p = strchr(names, ','))) {
  93. len = FFMAX(p - names, namelen);
  94. if (!av_strncasecmp(name, names, len))
  95. return 1;
  96. names = p + 1;
  97. }
  98. return !av_strcasecmp(name, names);
  99. }
  100. AVOutputFormat *av_guess_format(const char *short_name, const char *filename,
  101. const char *mime_type)
  102. {
  103. AVOutputFormat *fmt = NULL, *fmt_found;
  104. int score_max, score;
  105. /* specific test for image sequences */
  106. #if CONFIG_IMAGE2_MUXER
  107. if (!short_name && filename &&
  108. av_filename_number_test(filename) &&
  109. ff_guess_image2_codec(filename) != AV_CODEC_ID_NONE) {
  110. return av_guess_format("image2", NULL, NULL);
  111. }
  112. #endif
  113. /* Find the proper file type. */
  114. fmt_found = NULL;
  115. score_max = 0;
  116. while ((fmt = av_oformat_next(fmt))) {
  117. score = 0;
  118. if (fmt->name && short_name && match_format(short_name, fmt->name))
  119. score += 100;
  120. if (fmt->mime_type && mime_type && !strcmp(fmt->mime_type, mime_type))
  121. score += 10;
  122. if (filename && fmt->extensions &&
  123. av_match_ext(filename, fmt->extensions)) {
  124. score += 5;
  125. }
  126. if (score > score_max) {
  127. score_max = score;
  128. fmt_found = fmt;
  129. }
  130. }
  131. return fmt_found;
  132. }
  133. enum AVCodecID av_guess_codec(AVOutputFormat *fmt, const char *short_name,
  134. const char *filename, const char *mime_type,
  135. enum AVMediaType type)
  136. {
  137. if (!strcmp(fmt->name, "segment") || !strcmp(fmt->name, "ssegment")) {
  138. fmt = av_guess_format(NULL, filename, NULL);
  139. }
  140. if (type == AVMEDIA_TYPE_VIDEO) {
  141. enum AVCodecID codec_id = AV_CODEC_ID_NONE;
  142. #if CONFIG_IMAGE2_MUXER
  143. if (!strcmp(fmt->name, "image2") || !strcmp(fmt->name, "image2pipe")) {
  144. codec_id = ff_guess_image2_codec(filename);
  145. }
  146. #endif
  147. if (codec_id == AV_CODEC_ID_NONE)
  148. codec_id = fmt->video_codec;
  149. return codec_id;
  150. } else if (type == AVMEDIA_TYPE_AUDIO)
  151. return fmt->audio_codec;
  152. else if (type == AVMEDIA_TYPE_SUBTITLE)
  153. return fmt->subtitle_codec;
  154. else
  155. return AV_CODEC_ID_NONE;
  156. }
  157. AVInputFormat *av_find_input_format(const char *short_name)
  158. {
  159. AVInputFormat *fmt = NULL;
  160. while ((fmt = av_iformat_next(fmt)))
  161. if (match_format(short_name, fmt->name))
  162. return fmt;
  163. return NULL;
  164. }