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.

1337 lines
39KB

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