mp3_header_compress_bsf.c
Go to the documentation of this file.
1 /*
2  * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This file is part of Libav.
5  *
6  * Libav is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * Libav is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "libavutil/intreadwrite.h"
22 #include "avcodec.h"
23 #include "mpegaudiodecheader.h"
24 
25 
26 static int mp3_header_compress(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,
27  uint8_t **poutbuf, int *poutbuf_size,
28  const uint8_t *buf, int buf_size, int keyframe){
29  uint32_t header, extraheader;
30  int mode_extension, header_size;
31 
33  av_log(avctx, AV_LOG_ERROR, "not standards compliant\n");
34  return -1;
35  }
36 
37  header = AV_RB32(buf);
38  mode_extension= (header>>4)&3;
39 
40  if(ff_mpa_check_header(header) < 0 || (header&0x60000) != 0x20000){
41 output_unchanged:
42  *poutbuf= (uint8_t *) buf;
43  *poutbuf_size= buf_size;
44 
45  av_log(avctx, AV_LOG_INFO, "cannot compress %08X\n", header);
46  return 0;
47  }
48 
49  if(avctx->extradata_size == 0){
50  avctx->extradata_size=15;
51  avctx->extradata= av_malloc(avctx->extradata_size);
52  strcpy(avctx->extradata, "FFCMP3 0.0");
53  memcpy(avctx->extradata+11, buf, 4);
54  }
55  if(avctx->extradata_size != 15){
56  av_log(avctx, AV_LOG_ERROR, "Extradata invalid\n");
57  return -1;
58  }
59  extraheader = AV_RB32(avctx->extradata+11);
60  if((extraheader&MP3_MASK) != (header&MP3_MASK))
61  goto output_unchanged;
62 
63  header_size= (header&0x10000) ? 4 : 6;
64 
65  *poutbuf_size= buf_size - header_size;
66  *poutbuf= av_malloc(buf_size - header_size + FF_INPUT_BUFFER_PADDING_SIZE);
67  memcpy(*poutbuf, buf + header_size, buf_size - header_size + FF_INPUT_BUFFER_PADDING_SIZE);
68 
69  if(avctx->channels==2){
70  if((header & (3<<19)) != 3<<19){
71  (*poutbuf)[1] &= 0x3F;
72  (*poutbuf)[1] |= mode_extension<<6;
73  FFSWAP(int, (*poutbuf)[1], (*poutbuf)[2]);
74  }else{
75  (*poutbuf)[1] &= 0x8F;
76  (*poutbuf)[1] |= mode_extension<<4;
77  }
78  }
79 
80  return 1;
81 }
82 
84  "mp3comp",
85  0,
87 };
#define FF_COMPLIANCE_EXPERIMENTAL
Allow nonstandardized experimental things.
Definition: avcodec.h:1647
AV_WL32 AV_WL24 AV_WL16 AV_RB32
Definition: bytestream.h:89
#define MP3_MASK
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1387
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
static int ff_mpa_check_header(uint32_t header)
external API header
main external API structure.
Definition: avcodec.h:1329
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
AVBitStreamFilter ff_mp3_header_compress_bsf
int extradata_size
Definition: avcodec.h:1388
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:64
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:497
MPEG Audio header decoder.
int channels
number of audio channels
Definition: avcodec.h:1457
static int mp3_header_compress(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args, uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size, int keyframe)
#define AV_LOG_INFO
Definition: log.h:119
#define FFSWAP(type, a, b)
Definition: common.h:58
int strict_std_compliance
strictly follow the standard (MPEG4, ...).
Definition: avcodec.h:1642