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.

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