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.

1351 lines
40KB

  1. /*
  2. * utils for libavcodec
  3. * Copyright (c) 2001 Fabrice Bellard
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * utils.
  25. */
  26. #include "libavutil/avstring.h"
  27. #include "libavutil/integer.h"
  28. #include "libavutil/crc.h"
  29. #include "libavutil/pixdesc.h"
  30. #include "libavutil/audioconvert.h"
  31. #include "libavutil/imgutils.h"
  32. #include "libavutil/samplefmt.h"
  33. #include "avcodec.h"
  34. #include "dsputil.h"
  35. #include "libavutil/opt.h"
  36. #include "imgconvert.h"
  37. #include "thread.h"
  38. #include "audioconvert.h"
  39. #include "internal.h"
  40. #include <stdlib.h>
  41. #include <stdarg.h>
  42. #include <limits.h>
  43. #include <float.h>
  44. static int volatile entangled_thread_counter=0;
  45. static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  46. static void *codec_mutex;
  47. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
  48. {
  49. if(min_size < *size)
  50. return ptr;
  51. min_size= FFMAX(17*min_size/16 + 32, min_size);
  52. ptr= av_realloc(ptr, min_size);
  53. if(!ptr) //we could set this to the unmodified min_size but this is safer if the user lost the ptr and uses NULL now
  54. min_size= 0;
  55. *size= min_size;
  56. return ptr;
  57. }
  58. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
  59. {
  60. void **p = ptr;
  61. if (min_size < *size)
  62. return;
  63. min_size= FFMAX(17*min_size/16 + 32, min_size);
  64. av_free(*p);
  65. *p = av_malloc(min_size);
  66. if (!*p) min_size = 0;
  67. *size= min_size;
  68. }
  69. /* encoder management */
  70. static AVCodec *first_avcodec = NULL;
  71. AVCodec *av_codec_next(AVCodec *c){
  72. if(c) return c->next;
  73. else return first_avcodec;
  74. }
  75. void avcodec_register(AVCodec *codec)
  76. {
  77. AVCodec **p;
  78. avcodec_init();
  79. p = &first_avcodec;
  80. while (*p != NULL) p = &(*p)->next;
  81. *p = codec;
  82. codec->next = NULL;
  83. }
  84. unsigned avcodec_get_edge_width(void)
  85. {
  86. return EDGE_WIDTH;
  87. }
  88. void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
  89. s->coded_width = width;
  90. s->coded_height= height;
  91. s->width = -((-width )>>s->lowres);
  92. s->height= -((-height)>>s->lowres);
  93. }
  94. typedef struct InternalBuffer{
  95. int last_pic_num;
  96. uint8_t *base[4];
  97. uint8_t *data[4];
  98. int linesize[4];
  99. int width, height;
  100. enum PixelFormat pix_fmt;
  101. }InternalBuffer;
  102. #define INTERNAL_BUFFER_SIZE (32+1)
  103. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height, int linesize_align[4]){
  104. int w_align= 1;
  105. int h_align= 1;
  106. switch(s->pix_fmt){
  107. case PIX_FMT_YUV420P:
  108. case PIX_FMT_YUYV422:
  109. case PIX_FMT_UYVY422:
  110. case PIX_FMT_YUV422P:
  111. case PIX_FMT_YUV440P:
  112. case PIX_FMT_YUV444P:
  113. case PIX_FMT_GRAY8:
  114. case PIX_FMT_GRAY16BE:
  115. case PIX_FMT_GRAY16LE:
  116. case PIX_FMT_YUVJ420P:
  117. case PIX_FMT_YUVJ422P:
  118. case PIX_FMT_YUVJ440P:
  119. case PIX_FMT_YUVJ444P:
  120. case PIX_FMT_YUVA420P:
  121. case PIX_FMT_YUV420P9LE:
  122. case PIX_FMT_YUV420P9BE:
  123. case PIX_FMT_YUV420P10LE:
  124. case PIX_FMT_YUV420P10BE:
  125. case PIX_FMT_YUV422P10LE:
  126. case PIX_FMT_YUV422P10BE:
  127. w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
  128. h_align= 16;
  129. if(s->codec_id == CODEC_ID_MPEG2VIDEO || s->codec_id == CODEC_ID_MJPEG || s->codec_id == CODEC_ID_AMV || s->codec_id == CODEC_ID_THP || s->codec_id == CODEC_ID_H264)
  130. h_align= 32; // interlaced is rounded up to 2 MBs
  131. break;
  132. case PIX_FMT_YUV411P:
  133. case PIX_FMT_UYYVYY411:
  134. w_align=32;
  135. h_align=8;
  136. break;
  137. case PIX_FMT_YUV410P:
  138. if(s->codec_id == CODEC_ID_SVQ1){
  139. w_align=64;
  140. h_align=64;
  141. }
  142. case PIX_FMT_RGB555:
  143. if(s->codec_id == CODEC_ID_RPZA){
  144. w_align=4;
  145. h_align=4;
  146. }
  147. case PIX_FMT_PAL8:
  148. case PIX_FMT_BGR8:
  149. case PIX_FMT_RGB8:
  150. if(s->codec_id == CODEC_ID_SMC){
  151. w_align=4;
  152. h_align=4;
  153. }
  154. break;
  155. case PIX_FMT_BGR24:
  156. if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
  157. w_align=4;
  158. h_align=4;
  159. }
  160. break;
  161. default:
  162. w_align= 1;
  163. h_align= 1;
  164. break;
  165. }
  166. *width = FFALIGN(*width , w_align);
  167. *height= FFALIGN(*height, h_align);
  168. if(s->codec_id == CODEC_ID_H264 || s->lowres)
  169. *height+=2; // some of the optimized chroma MC reads one line too much
  170. // which is also done in mpeg decoders with lowres > 0
  171. linesize_align[0] =
  172. linesize_align[1] =
  173. linesize_align[2] =
  174. linesize_align[3] = STRIDE_ALIGN;
  175. //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
  176. //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
  177. //picture size unneccessarily in some cases. The solution here is not
  178. //pretty and better ideas are welcome!
  179. #if HAVE_MMX
  180. if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
  181. s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
  182. s->codec_id == CODEC_ID_VP6A) {
  183. linesize_align[0] =
  184. linesize_align[1] =
  185. linesize_align[2] = 16;
  186. }
  187. #endif
  188. }
  189. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
  190. int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
  191. int linesize_align[4];
  192. int align;
  193. avcodec_align_dimensions2(s, width, height, linesize_align);
  194. align = FFMAX(linesize_align[0], linesize_align[3]);
  195. linesize_align[1] <<= chroma_shift;
  196. linesize_align[2] <<= chroma_shift;
  197. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  198. *width=FFALIGN(*width, align);
  199. }
  200. int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
  201. int i;
  202. int w= s->width;
  203. int h= s->height;
  204. InternalBuffer *buf;
  205. int *picture_number;
  206. if(pic->data[0]!=NULL) {
  207. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  208. return -1;
  209. }
  210. if(s->internal_buffer_count >= INTERNAL_BUFFER_SIZE) {
  211. av_log(s, AV_LOG_ERROR, "internal_buffer_count overflow (missing release_buffer?)\n");
  212. return -1;
  213. }
  214. if(av_image_check_size(w, h, 0, s))
  215. return -1;
  216. if(s->internal_buffer==NULL){
  217. s->internal_buffer= av_mallocz((INTERNAL_BUFFER_SIZE+1)*sizeof(InternalBuffer));
  218. }
  219. #if 0
  220. s->internal_buffer= av_fast_realloc(
  221. s->internal_buffer,
  222. &s->internal_buffer_size,
  223. sizeof(InternalBuffer)*FFMAX(99, s->internal_buffer_count+1)/*FIXME*/
  224. );
  225. #endif
  226. buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  227. picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack
  228. (*picture_number)++;
  229. if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
  230. if(s->active_thread_type&FF_THREAD_FRAME) {
  231. av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
  232. return -1;
  233. }
  234. for(i=0; i<4; i++){
  235. av_freep(&buf->base[i]);
  236. buf->data[i]= NULL;
  237. }
  238. }
  239. if(buf->base[0]){
  240. pic->age= *picture_number - buf->last_pic_num;
  241. buf->last_pic_num= *picture_number;
  242. }else{
  243. int h_chroma_shift, v_chroma_shift;
  244. int size[4] = {0};
  245. int tmpsize;
  246. int unaligned;
  247. AVPicture picture;
  248. int stride_align[4];
  249. const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
  250. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  251. avcodec_align_dimensions2(s, &w, &h, stride_align);
  252. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  253. w+= EDGE_WIDTH*2;
  254. h+= EDGE_WIDTH*2;
  255. }
  256. do {
  257. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  258. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  259. av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
  260. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  261. w += w & ~(w-1);
  262. unaligned = 0;
  263. for (i=0; i<4; i++){
  264. unaligned |= picture.linesize[i] % stride_align[i];
  265. }
  266. } while (unaligned);
  267. tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
  268. if (tmpsize < 0)
  269. return -1;
  270. for (i=0; i<3 && picture.data[i+1]; i++)
  271. size[i] = picture.data[i+1] - picture.data[i];
  272. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  273. buf->last_pic_num= -256*256*256*64;
  274. memset(buf->base, 0, sizeof(buf->base));
  275. memset(buf->data, 0, sizeof(buf->data));
  276. for(i=0; i<4 && size[i]; i++){
  277. const int h_shift= i==0 ? 0 : h_chroma_shift;
  278. const int v_shift= i==0 ? 0 : v_chroma_shift;
  279. buf->linesize[i]= picture.linesize[i];
  280. buf->base[i]= av_malloc(size[i]+16); //FIXME 16
  281. if(buf->base[i]==NULL) return -1;
  282. memset(buf->base[i], 128, size[i]);
  283. // no edge if EDGE EMU or not planar YUV
  284. if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
  285. buf->data[i] = buf->base[i];
  286. else
  287. buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
  288. }
  289. if(size[1] && !size[2])
  290. ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
  291. buf->width = s->width;
  292. buf->height = s->height;
  293. buf->pix_fmt= s->pix_fmt;
  294. pic->age= 256*256*256*64;
  295. }
  296. pic->type= FF_BUFFER_TYPE_INTERNAL;
  297. for(i=0; i<4; i++){
  298. pic->base[i]= buf->base[i];
  299. pic->data[i]= buf->data[i];
  300. pic->linesize[i]= buf->linesize[i];
  301. }
  302. s->internal_buffer_count++;
  303. if (s->pkt) {
  304. pic->pkt_pts = s->pkt->pts;
  305. pic->pkt_pos = s->pkt->pos;
  306. } else {
  307. pic->pkt_pts = AV_NOPTS_VALUE;
  308. pic->pkt_pos = -1;
  309. }
  310. pic->reordered_opaque= s->reordered_opaque;
  311. pic->sample_aspect_ratio = s->sample_aspect_ratio;
  312. pic->width = s->width;
  313. pic->height = s->height;
  314. pic->format = s->pix_fmt;
  315. if(s->debug&FF_DEBUG_BUFFERS)
  316. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
  317. return 0;
  318. }
  319. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  320. int i;
  321. InternalBuffer *buf, *last;
  322. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  323. assert(s->internal_buffer_count);
  324. if(s->internal_buffer){
  325. buf = NULL; /* avoids warning */
  326. for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
  327. buf= &((InternalBuffer*)s->internal_buffer)[i];
  328. if(buf->data[0] == pic->data[0])
  329. break;
  330. }
  331. assert(i < s->internal_buffer_count);
  332. s->internal_buffer_count--;
  333. last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  334. FFSWAP(InternalBuffer, *buf, *last);
  335. }
  336. for(i=0; i<4; i++){
  337. pic->data[i]=NULL;
  338. // pic->base[i]=NULL;
  339. }
  340. //printf("R%X\n", pic->opaque);
  341. if(s->debug&FF_DEBUG_BUFFERS)
  342. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
  343. }
  344. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
  345. AVFrame temp_pic;
  346. int i;
  347. /* If no picture return a new buffer */
  348. if(pic->data[0] == NULL) {
  349. /* We will copy from buffer, so must be readable */
  350. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  351. return s->get_buffer(s, pic);
  352. }
  353. /* If internal buffer type return the same buffer */
  354. if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
  355. if(s->pkt) pic->pkt_pts= s->pkt->pts;
  356. else pic->pkt_pts= AV_NOPTS_VALUE;
  357. pic->reordered_opaque= s->reordered_opaque;
  358. return 0;
  359. }
  360. /*
  361. * Not internal type and reget_buffer not overridden, emulate cr buffer
  362. */
  363. temp_pic = *pic;
  364. for(i = 0; i < 4; i++)
  365. pic->data[i] = pic->base[i] = NULL;
  366. pic->opaque = NULL;
  367. /* Allocate new frame */
  368. if (s->get_buffer(s, pic))
  369. return -1;
  370. /* Copy image data from old buffer to new buffer */
  371. av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
  372. s->height);
  373. s->release_buffer(s, &temp_pic); // Release old frame
  374. return 0;
  375. }
  376. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
  377. int i;
  378. for(i=0; i<count; i++){
  379. int r= func(c, (char*)arg + i*size);
  380. if(ret) ret[i]= r;
  381. }
  382. return 0;
  383. }
  384. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
  385. int i;
  386. for(i=0; i<count; i++){
  387. int r= func(c, arg, i, 0);
  388. if(ret) ret[i]= r;
  389. }
  390. return 0;
  391. }
  392. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
  393. while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
  394. ++fmt;
  395. return fmt[0];
  396. }
  397. void avcodec_get_frame_defaults(AVFrame *pic){
  398. memset(pic, 0, sizeof(AVFrame));
  399. pic->pts = pic->best_effort_timestamp = AV_NOPTS_VALUE;
  400. pic->pkt_pos = -1;
  401. pic->key_frame= 1;
  402. pic->sample_aspect_ratio = (AVRational){0, 1};
  403. pic->format = -1; /* unknown */
  404. }
  405. AVFrame *avcodec_alloc_frame(void){
  406. AVFrame *pic= av_malloc(sizeof(AVFrame));
  407. if(pic==NULL) return NULL;
  408. avcodec_get_frame_defaults(pic);
  409. return pic;
  410. }
  411. static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
  412. {
  413. memset(sub, 0, sizeof(*sub));
  414. sub->pts = AV_NOPTS_VALUE;
  415. }
  416. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  417. {
  418. int ret = 0;
  419. /* If there is a user-supplied mutex locking routine, call it. */
  420. if (ff_lockmgr_cb) {
  421. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  422. return -1;
  423. }
  424. entangled_thread_counter++;
  425. if(entangled_thread_counter != 1){
  426. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  427. ret = -1;
  428. goto end;
  429. }
  430. if(avctx->codec || !codec) {
  431. ret = AVERROR(EINVAL);
  432. goto end;
  433. }
  434. if (codec->priv_data_size > 0) {
  435. if(!avctx->priv_data){
  436. avctx->priv_data = av_mallocz(codec->priv_data_size);
  437. if (!avctx->priv_data) {
  438. ret = AVERROR(ENOMEM);
  439. goto end;
  440. }
  441. if(codec->priv_class){ //this can be droped once all user apps use avcodec_get_context_defaults3()
  442. *(AVClass**)avctx->priv_data= codec->priv_class;
  443. av_opt_set_defaults(avctx->priv_data);
  444. }
  445. }
  446. } else {
  447. avctx->priv_data = NULL;
  448. }
  449. if(avctx->coded_width && avctx->coded_height)
  450. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  451. else if(avctx->width && avctx->height)
  452. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  453. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  454. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  455. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  456. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  457. avcodec_set_dimensions(avctx, 0, 0);
  458. }
  459. /* if the decoder init function was already called previously,
  460. free the already allocated subtitle_header before overwriting it */
  461. if (codec->decode)
  462. av_freep(&avctx->subtitle_header);
  463. #define SANE_NB_CHANNELS 128U
  464. if (avctx->channels > SANE_NB_CHANNELS) {
  465. ret = AVERROR(EINVAL);
  466. goto free_and_end;
  467. }
  468. avctx->codec = codec;
  469. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  470. avctx->codec_id == CODEC_ID_NONE) {
  471. avctx->codec_type = codec->type;
  472. avctx->codec_id = codec->id;
  473. }
  474. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  475. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  476. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  477. ret = AVERROR(EINVAL);
  478. goto free_and_end;
  479. }
  480. avctx->frame_number = 0;
  481. if (HAVE_THREADS && !avctx->thread_opaque) {
  482. ret = ff_thread_init(avctx);
  483. if (ret < 0) {
  484. goto free_and_end;
  485. }
  486. }
  487. if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
  488. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  489. avctx->codec->max_lowres);
  490. ret = AVERROR(EINVAL);
  491. goto free_and_end;
  492. }
  493. if (avctx->codec->encode) {
  494. int i;
  495. if (avctx->codec->sample_fmts) {
  496. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
  497. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  498. break;
  499. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  500. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  501. ret = AVERROR(EINVAL);
  502. goto free_and_end;
  503. }
  504. }
  505. if (avctx->codec->supported_samplerates) {
  506. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  507. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  508. break;
  509. if (avctx->codec->supported_samplerates[i] == 0) {
  510. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  511. ret = AVERROR(EINVAL);
  512. goto free_and_end;
  513. }
  514. }
  515. if (avctx->codec->channel_layouts) {
  516. if (!avctx->channel_layout) {
  517. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  518. } else {
  519. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  520. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  521. break;
  522. if (avctx->codec->channel_layouts[i] == 0) {
  523. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  524. ret = AVERROR(EINVAL);
  525. goto free_and_end;
  526. }
  527. }
  528. }
  529. if (avctx->channel_layout && avctx->channels) {
  530. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  531. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  532. ret = AVERROR(EINVAL);
  533. goto free_and_end;
  534. }
  535. } else if (avctx->channel_layout) {
  536. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  537. }
  538. }
  539. avctx->pts_correction_num_faulty_pts =
  540. avctx->pts_correction_num_faulty_dts = 0;
  541. avctx->pts_correction_last_pts =
  542. avctx->pts_correction_last_dts = INT64_MIN;
  543. if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
  544. ret = avctx->codec->init(avctx);
  545. if (ret < 0) {
  546. goto free_and_end;
  547. }
  548. }
  549. ret=0;
  550. end:
  551. entangled_thread_counter--;
  552. /* Release any user-supplied mutex. */
  553. if (ff_lockmgr_cb) {
  554. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  555. }
  556. return ret;
  557. free_and_end:
  558. av_freep(&avctx->priv_data);
  559. avctx->codec= NULL;
  560. goto end;
  561. }
  562. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  563. const short *samples)
  564. {
  565. if(buf_size < FF_MIN_BUFFER_SIZE && 0){
  566. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  567. return -1;
  568. }
  569. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
  570. int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
  571. avctx->frame_number++;
  572. return ret;
  573. }else
  574. return 0;
  575. }
  576. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  577. const AVFrame *pict)
  578. {
  579. if(buf_size < FF_MIN_BUFFER_SIZE){
  580. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  581. return -1;
  582. }
  583. if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
  584. return -1;
  585. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
  586. int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
  587. avctx->frame_number++;
  588. emms_c(); //needed to avoid an emms_c() call before every return;
  589. return ret;
  590. }else
  591. return 0;
  592. }
  593. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  594. const AVSubtitle *sub)
  595. {
  596. int ret;
  597. if(sub->start_display_time) {
  598. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  599. return -1;
  600. }
  601. ret = avctx->codec->encode(avctx, buf, buf_size, sub);
  602. avctx->frame_number++;
  603. return ret;
  604. }
  605. /**
  606. * Attempt to guess proper monotonic timestamps for decoded video frames
  607. * which might have incorrect times. Input timestamps may wrap around, in
  608. * which case the output will as well.
  609. *
  610. * @param pts the pts field of the decoded AVPacket, as passed through
  611. * AVFrame.pkt_pts
  612. * @param dts the dts field of the decoded AVPacket
  613. * @return one of the input values, may be AV_NOPTS_VALUE
  614. */
  615. static int64_t guess_correct_pts(AVCodecContext *ctx,
  616. int64_t reordered_pts, int64_t dts)
  617. {
  618. int64_t pts = AV_NOPTS_VALUE;
  619. if (dts != AV_NOPTS_VALUE) {
  620. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  621. ctx->pts_correction_last_dts = dts;
  622. }
  623. if (reordered_pts != AV_NOPTS_VALUE) {
  624. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  625. ctx->pts_correction_last_pts = reordered_pts;
  626. }
  627. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  628. && reordered_pts != AV_NOPTS_VALUE)
  629. pts = reordered_pts;
  630. else
  631. pts = dts;
  632. return pts;
  633. }
  634. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  635. int *got_picture_ptr,
  636. AVPacket *avpkt)
  637. {
  638. int ret;
  639. *got_picture_ptr= 0;
  640. if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  641. return -1;
  642. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
  643. av_packet_split_side_data(avpkt);
  644. avctx->pkt = avpkt;
  645. if (HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  646. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  647. avpkt);
  648. else {
  649. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  650. avpkt);
  651. picture->pkt_dts= avpkt->dts;
  652. if(!avctx->has_b_frames){
  653. picture->pkt_pos= avpkt->pos;
  654. }
  655. //FIXME these should be under if(!avctx->has_b_frames)
  656. if (!picture->sample_aspect_ratio.num)
  657. picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  658. if (!picture->width)
  659. picture->width = avctx->width;
  660. if (!picture->height)
  661. picture->height = avctx->height;
  662. if (picture->format == PIX_FMT_NONE)
  663. picture->format = avctx->pix_fmt;
  664. }
  665. emms_c(); //needed to avoid an emms_c() call before every return;
  666. if (*got_picture_ptr){
  667. avctx->frame_number++;
  668. picture->best_effort_timestamp = guess_correct_pts(avctx,
  669. picture->pkt_pts,
  670. picture->pkt_dts);
  671. }
  672. }else
  673. ret= 0;
  674. return ret;
  675. }
  676. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  677. int *frame_size_ptr,
  678. AVPacket *avpkt)
  679. {
  680. int ret;
  681. avctx->pkt = avpkt;
  682. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
  683. //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
  684. if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
  685. av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
  686. return -1;
  687. }
  688. if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
  689. *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
  690. av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
  691. return -1;
  692. }
  693. ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
  694. avctx->frame_number++;
  695. }else{
  696. ret= 0;
  697. *frame_size_ptr=0;
  698. }
  699. return ret;
  700. }
  701. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  702. int *got_sub_ptr,
  703. AVPacket *avpkt)
  704. {
  705. int ret;
  706. avctx->pkt = avpkt;
  707. *got_sub_ptr = 0;
  708. avcodec_get_subtitle_defaults(sub);
  709. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  710. if (*got_sub_ptr)
  711. avctx->frame_number++;
  712. return ret;
  713. }
  714. void avsubtitle_free(AVSubtitle *sub)
  715. {
  716. int i;
  717. for (i = 0; i < sub->num_rects; i++)
  718. {
  719. av_freep(&sub->rects[i]->pict.data[0]);
  720. av_freep(&sub->rects[i]->pict.data[1]);
  721. av_freep(&sub->rects[i]->pict.data[2]);
  722. av_freep(&sub->rects[i]->pict.data[3]);
  723. av_freep(&sub->rects[i]->text);
  724. av_freep(&sub->rects[i]->ass);
  725. av_freep(&sub->rects[i]);
  726. }
  727. av_freep(&sub->rects);
  728. memset(sub, 0, sizeof(AVSubtitle));
  729. }
  730. av_cold int avcodec_close(AVCodecContext *avctx)
  731. {
  732. /* If there is a user-supplied mutex locking routine, call it. */
  733. if (ff_lockmgr_cb) {
  734. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  735. return -1;
  736. }
  737. entangled_thread_counter++;
  738. if(entangled_thread_counter != 1){
  739. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  740. entangled_thread_counter--;
  741. return -1;
  742. }
  743. if (HAVE_THREADS && avctx->thread_opaque)
  744. ff_thread_free(avctx);
  745. if (avctx->codec && avctx->codec->close)
  746. avctx->codec->close(avctx);
  747. avcodec_default_free_buffers(avctx);
  748. avctx->coded_frame = NULL;
  749. av_freep(&avctx->priv_data);
  750. if(avctx->codec && avctx->codec->encode)
  751. av_freep(&avctx->extradata);
  752. avctx->codec = NULL;
  753. avctx->active_thread_type = 0;
  754. entangled_thread_counter--;
  755. /* Release any user-supplied mutex. */
  756. if (ff_lockmgr_cb) {
  757. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  758. }
  759. return 0;
  760. }
  761. AVCodec *avcodec_find_encoder(enum CodecID id)
  762. {
  763. AVCodec *p, *experimental=NULL;
  764. p = first_avcodec;
  765. while (p) {
  766. if (p->encode != NULL && p->id == id) {
  767. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  768. experimental = p;
  769. } else
  770. return p;
  771. }
  772. p = p->next;
  773. }
  774. return experimental;
  775. }
  776. AVCodec *avcodec_find_encoder_by_name(const char *name)
  777. {
  778. AVCodec *p;
  779. if (!name)
  780. return NULL;
  781. p = first_avcodec;
  782. while (p) {
  783. if (p->encode != NULL && strcmp(name,p->name) == 0)
  784. return p;
  785. p = p->next;
  786. }
  787. return NULL;
  788. }
  789. AVCodec *avcodec_find_decoder(enum CodecID id)
  790. {
  791. AVCodec *p, *experimental=NULL;
  792. p = first_avcodec;
  793. while (p) {
  794. if (p->decode != NULL && p->id == id) {
  795. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  796. experimental = p;
  797. } else
  798. return p;
  799. }
  800. p = p->next;
  801. }
  802. return experimental;
  803. }
  804. AVCodec *avcodec_find_decoder_by_name(const char *name)
  805. {
  806. AVCodec *p;
  807. if (!name)
  808. return NULL;
  809. p = first_avcodec;
  810. while (p) {
  811. if (p->decode != NULL && strcmp(name,p->name) == 0)
  812. return p;
  813. p = p->next;
  814. }
  815. return NULL;
  816. }
  817. static int get_bit_rate(AVCodecContext *ctx)
  818. {
  819. int bit_rate;
  820. int bits_per_sample;
  821. switch(ctx->codec_type) {
  822. case AVMEDIA_TYPE_VIDEO:
  823. case AVMEDIA_TYPE_DATA:
  824. case AVMEDIA_TYPE_SUBTITLE:
  825. case AVMEDIA_TYPE_ATTACHMENT:
  826. bit_rate = ctx->bit_rate;
  827. break;
  828. case AVMEDIA_TYPE_AUDIO:
  829. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  830. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  831. break;
  832. default:
  833. bit_rate = 0;
  834. break;
  835. }
  836. return bit_rate;
  837. }
  838. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  839. {
  840. int i, len, ret = 0;
  841. for (i = 0; i < 4; i++) {
  842. len = snprintf(buf, buf_size,
  843. isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  844. buf += len;
  845. buf_size = buf_size > len ? buf_size - len : 0;
  846. ret += len;
  847. codec_tag>>=8;
  848. }
  849. return ret;
  850. }
  851. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  852. {
  853. const char *codec_name;
  854. const char *profile = NULL;
  855. AVCodec *p;
  856. char buf1[32];
  857. int bitrate;
  858. AVRational display_aspect_ratio;
  859. if (encode)
  860. p = avcodec_find_encoder(enc->codec_id);
  861. else
  862. p = avcodec_find_decoder(enc->codec_id);
  863. if (p) {
  864. codec_name = p->name;
  865. profile = av_get_profile_name(p, enc->profile);
  866. } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
  867. /* fake mpeg2 transport stream codec (currently not
  868. registered) */
  869. codec_name = "mpeg2ts";
  870. } else if (enc->codec_name[0] != '\0') {
  871. codec_name = enc->codec_name;
  872. } else {
  873. /* output avi tags */
  874. char tag_buf[32];
  875. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  876. snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
  877. codec_name = buf1;
  878. }
  879. switch(enc->codec_type) {
  880. case AVMEDIA_TYPE_VIDEO:
  881. snprintf(buf, buf_size,
  882. "Video: %s%s",
  883. codec_name, enc->mb_decision ? " (hq)" : "");
  884. if (profile)
  885. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  886. " (%s)", profile);
  887. if (enc->pix_fmt != PIX_FMT_NONE) {
  888. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  889. ", %s",
  890. avcodec_get_pix_fmt_name(enc->pix_fmt));
  891. }
  892. if (enc->width) {
  893. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  894. ", %dx%d",
  895. enc->width, enc->height);
  896. if (enc->sample_aspect_ratio.num) {
  897. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  898. enc->width*enc->sample_aspect_ratio.num,
  899. enc->height*enc->sample_aspect_ratio.den,
  900. 1024*1024);
  901. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  902. " [PAR %d:%d DAR %d:%d]",
  903. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  904. display_aspect_ratio.num, display_aspect_ratio.den);
  905. }
  906. if(av_log_get_level() >= AV_LOG_DEBUG){
  907. int g= av_gcd(enc->time_base.num, enc->time_base.den);
  908. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  909. ", %d/%d",
  910. enc->time_base.num/g, enc->time_base.den/g);
  911. }
  912. }
  913. if (encode) {
  914. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  915. ", q=%d-%d", enc->qmin, enc->qmax);
  916. }
  917. break;
  918. case AVMEDIA_TYPE_AUDIO:
  919. snprintf(buf, buf_size,
  920. "Audio: %s",
  921. codec_name);
  922. if (profile)
  923. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  924. " (%s)", profile);
  925. if (enc->sample_rate) {
  926. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  927. ", %d Hz", enc->sample_rate);
  928. }
  929. av_strlcat(buf, ", ", buf_size);
  930. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  931. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  932. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  933. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  934. }
  935. break;
  936. case AVMEDIA_TYPE_DATA:
  937. snprintf(buf, buf_size, "Data: %s", codec_name);
  938. break;
  939. case AVMEDIA_TYPE_SUBTITLE:
  940. snprintf(buf, buf_size, "Subtitle: %s", codec_name);
  941. break;
  942. case AVMEDIA_TYPE_ATTACHMENT:
  943. snprintf(buf, buf_size, "Attachment: %s", codec_name);
  944. break;
  945. default:
  946. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  947. return;
  948. }
  949. if (encode) {
  950. if (enc->flags & CODEC_FLAG_PASS1)
  951. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  952. ", pass 1");
  953. if (enc->flags & CODEC_FLAG_PASS2)
  954. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  955. ", pass 2");
  956. }
  957. bitrate = get_bit_rate(enc);
  958. if (bitrate != 0) {
  959. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  960. ", %d kb/s", bitrate / 1000);
  961. }
  962. }
  963. const char *av_get_profile_name(const AVCodec *codec, int profile)
  964. {
  965. const AVProfile *p;
  966. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  967. return NULL;
  968. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  969. if (p->profile == profile)
  970. return p->name;
  971. return NULL;
  972. }
  973. unsigned avcodec_version( void )
  974. {
  975. return LIBAVCODEC_VERSION_INT;
  976. }
  977. const char *avcodec_configuration(void)
  978. {
  979. return FFMPEG_CONFIGURATION;
  980. }
  981. const char *avcodec_license(void)
  982. {
  983. #define LICENSE_PREFIX "libavcodec license: "
  984. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  985. }
  986. void avcodec_init(void)
  987. {
  988. static int initialized = 0;
  989. if (initialized != 0)
  990. return;
  991. initialized = 1;
  992. dsputil_static_init();
  993. }
  994. void avcodec_flush_buffers(AVCodecContext *avctx)
  995. {
  996. if(HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  997. ff_thread_flush(avctx);
  998. if(avctx->codec->flush)
  999. avctx->codec->flush(avctx);
  1000. }
  1001. void avcodec_default_free_buffers(AVCodecContext *s){
  1002. int i, j;
  1003. if(s->internal_buffer==NULL) return;
  1004. if (s->internal_buffer_count)
  1005. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count);
  1006. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  1007. InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
  1008. for(j=0; j<4; j++){
  1009. av_freep(&buf->base[j]);
  1010. buf->data[j]= NULL;
  1011. }
  1012. }
  1013. av_freep(&s->internal_buffer);
  1014. s->internal_buffer_count=0;
  1015. }
  1016. #if FF_API_OLD_FF_PICT_TYPES
  1017. char av_get_pict_type_char(int pict_type){
  1018. return av_get_picture_type_char(pict_type);
  1019. }
  1020. #endif
  1021. int av_get_bits_per_sample(enum CodecID codec_id){
  1022. switch(codec_id){
  1023. case CODEC_ID_ADPCM_SBPRO_2:
  1024. return 2;
  1025. case CODEC_ID_ADPCM_SBPRO_3:
  1026. return 3;
  1027. case CODEC_ID_ADPCM_SBPRO_4:
  1028. case CODEC_ID_ADPCM_CT:
  1029. case CODEC_ID_ADPCM_IMA_WAV:
  1030. case CODEC_ID_ADPCM_MS:
  1031. case CODEC_ID_ADPCM_YAMAHA:
  1032. return 4;
  1033. case CODEC_ID_ADPCM_G722:
  1034. case CODEC_ID_PCM_ALAW:
  1035. case CODEC_ID_PCM_MULAW:
  1036. case CODEC_ID_PCM_S8:
  1037. case CODEC_ID_PCM_U8:
  1038. case CODEC_ID_PCM_ZORK:
  1039. return 8;
  1040. case CODEC_ID_PCM_S16BE:
  1041. case CODEC_ID_PCM_S16LE:
  1042. case CODEC_ID_PCM_S16LE_PLANAR:
  1043. case CODEC_ID_PCM_U16BE:
  1044. case CODEC_ID_PCM_U16LE:
  1045. return 16;
  1046. case CODEC_ID_PCM_S24DAUD:
  1047. case CODEC_ID_PCM_S24BE:
  1048. case CODEC_ID_PCM_S24LE:
  1049. case CODEC_ID_PCM_U24BE:
  1050. case CODEC_ID_PCM_U24LE:
  1051. return 24;
  1052. case CODEC_ID_PCM_S32BE:
  1053. case CODEC_ID_PCM_S32LE:
  1054. case CODEC_ID_PCM_U32BE:
  1055. case CODEC_ID_PCM_U32LE:
  1056. case CODEC_ID_PCM_F32BE:
  1057. case CODEC_ID_PCM_F32LE:
  1058. return 32;
  1059. case CODEC_ID_PCM_F64BE:
  1060. case CODEC_ID_PCM_F64LE:
  1061. return 64;
  1062. default:
  1063. return 0;
  1064. }
  1065. }
  1066. #if FF_API_OLD_SAMPLE_FMT
  1067. int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
  1068. return av_get_bits_per_sample_fmt(sample_fmt);
  1069. }
  1070. #endif
  1071. #if !HAVE_THREADS
  1072. int ff_thread_init(AVCodecContext *s){
  1073. return -1;
  1074. }
  1075. #endif
  1076. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1077. {
  1078. unsigned int n = 0;
  1079. while(v >= 0xff) {
  1080. *s++ = 0xff;
  1081. v -= 0xff;
  1082. n++;
  1083. }
  1084. *s = v;
  1085. n++;
  1086. return n;
  1087. }
  1088. int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
  1089. int i;
  1090. for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
  1091. return i;
  1092. }
  1093. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  1094. {
  1095. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
  1096. "version to the newest one from Git. If the problem still "
  1097. "occurs, it means that your file has a feature which has not "
  1098. "been implemented.\n", feature);
  1099. if(want_sample)
  1100. av_log_ask_for_sample(avc, NULL);
  1101. }
  1102. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  1103. {
  1104. va_list argument_list;
  1105. va_start(argument_list, msg);
  1106. if (msg)
  1107. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  1108. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  1109. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  1110. "and contact the ffmpeg-devel mailing list.\n");
  1111. va_end(argument_list);
  1112. }
  1113. static AVHWAccel *first_hwaccel = NULL;
  1114. void av_register_hwaccel(AVHWAccel *hwaccel)
  1115. {
  1116. AVHWAccel **p = &first_hwaccel;
  1117. while (*p)
  1118. p = &(*p)->next;
  1119. *p = hwaccel;
  1120. hwaccel->next = NULL;
  1121. }
  1122. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1123. {
  1124. return hwaccel ? hwaccel->next : first_hwaccel;
  1125. }
  1126. AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
  1127. {
  1128. AVHWAccel *hwaccel=NULL;
  1129. while((hwaccel= av_hwaccel_next(hwaccel))){
  1130. if ( hwaccel->id == codec_id
  1131. && hwaccel->pix_fmt == pix_fmt)
  1132. return hwaccel;
  1133. }
  1134. return NULL;
  1135. }
  1136. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1137. {
  1138. if (ff_lockmgr_cb) {
  1139. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1140. return -1;
  1141. }
  1142. ff_lockmgr_cb = cb;
  1143. if (ff_lockmgr_cb) {
  1144. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1145. return -1;
  1146. }
  1147. return 0;
  1148. }
  1149. unsigned int ff_toupper4(unsigned int x)
  1150. {
  1151. return toupper( x &0xFF)
  1152. + (toupper((x>>8 )&0xFF)<<8 )
  1153. + (toupper((x>>16)&0xFF)<<16)
  1154. + (toupper((x>>24)&0xFF)<<24);
  1155. }
  1156. #if !HAVE_PTHREADS
  1157. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  1158. {
  1159. f->owner = avctx;
  1160. return avctx->get_buffer(avctx, f);
  1161. }
  1162. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  1163. {
  1164. f->owner->release_buffer(f->owner, f);
  1165. }
  1166. void ff_thread_finish_setup(AVCodecContext *avctx)
  1167. {
  1168. }
  1169. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  1170. {
  1171. }
  1172. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  1173. {
  1174. }
  1175. #endif
  1176. #if FF_API_THREAD_INIT
  1177. int avcodec_thread_init(AVCodecContext *s, int thread_count)
  1178. {
  1179. s->thread_count = thread_count;
  1180. return ff_thread_init(s);
  1181. }
  1182. #endif