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.

1188 lines
34KB

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