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.

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