code stringlengths 1 2.01M | repo_name stringlengths 3 62 | path stringlengths 1 267 | language stringclasses 231 values | license stringclasses 13 values | size int64 1 2.01M |
|---|---|---|---|---|---|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Stateless
{
static class ParameterConversion
{
public static object Unpack(object[] args, Type argType, int index)
{
Enforce.ArgumentNotNull(args, "args");
if (args.Length <= index)
throw new ArgumentException(
string.Format(ParameterConversionResources.ArgOfTypeRequiredInPosition, argType, index));
var arg = args[index];
if (arg != null && !argType.IsAssignableFrom(arg.GetType()))
throw new ArgumentException(
string.Format(ParameterConversionResources.WrongArgType, index, arg.GetType(), argType));
return arg;
}
public static TArg Unpack<TArg>(object[] args, int index)
{
return (TArg)Unpack(args, typeof(TArg), index);
}
public static void Validate(object[] args, Type[] expected)
{
if (args.Length > expected.Length)
throw new ArgumentException(
string.Format(ParameterConversionResources.TooManyParameters, expected.Length, args.Length));
for (int i = 0; i < expected.Length; ++i)
Unpack(args, expected[i], i);
}
}
}
| 1234567eight-test | Stateless/ParameterConversion.cs | C# | asf20 | 1,379 |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Stateless
{
public partial class StateMachine<TState, TTrigger>
{
internal class TransitioningTriggerBehaviour : TriggerBehaviour
{
readonly TState _destination;
public TransitioningTriggerBehaviour(TTrigger trigger, TState destination, Func<bool> guard)
: base(trigger, guard)
{
_destination = destination;
}
public override bool ResultsInTransitionFrom(TState source, object[] args, out TState destination)
{
destination = _destination;
return true;
}
}
}
}
| 1234567eight-test | Stateless/TransitioningTriggerBehaviour.cs | C# | asf20 | 773 |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Stateless;
namespace BugTrackerExample
{
public class Bug
{
enum State { Open, Assigned, Deferred, Resolved, Closed }
enum Trigger { Assign, Defer, Resolve, Close }
State _state = State.Open;
StateMachine<State, Trigger> _machine;
StateMachine<State, Trigger>.TriggerWithParameters<string> _assignTrigger;
string _title;
string _assignee;
public Bug(string title)
{
_title = title;
_machine = new StateMachine<State, Trigger>(() => _state, s => _state = s);
_assignTrigger = _machine.SetTriggerParameters<string>(Trigger.Assign);
_machine.Configure(State.Open)
.Permit(Trigger.Assign, State.Assigned);
_machine.Configure(State.Assigned)
.SubstateOf(State.Open)
.OnEntryFrom(_assignTrigger, assignee => OnAssigned(assignee))
.PermitReentry(Trigger.Assign)
.Permit(Trigger.Close, State.Closed)
.Permit(Trigger.Defer, State.Deferred)
.OnExit(() => OnDeassigned());
_machine.Configure(State.Deferred)
.OnEntry(() => _assignee = null)
.Permit(Trigger.Assign, State.Assigned);
}
public void Close()
{
_machine.Fire(Trigger.Close);
}
public void Assign(string assignee)
{
_machine.Fire(_assignTrigger, assignee);
}
public bool CanAssign
{
get
{
return _machine.CanFire(Trigger.Assign);
}
}
public void Defer()
{
_machine.Fire(Trigger.Defer);
}
void OnAssigned(string assignee)
{
if (_assignee != null && assignee != _assignee)
SendEmailToAssignee("Don't forget to help the new guy.");
_assignee = assignee;
SendEmailToAssignee("You own it.");
}
void OnDeassigned()
{
SendEmailToAssignee("You're off the hook.");
}
void SendEmailToAssignee(string message)
{
Console.WriteLine("{0}, RE {1}: {2}", _assignee, _title, message);
}
}
}
| 1234567eight-test | BugTrackerExample/Bug.cs | C# | asf20 | 2,462 |
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("BugTrackerExample")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Hewlett-Packard")]
[assembly: AssemblyProduct("BugTrackerExample")]
[assembly: AssemblyCopyright("Copyright © Hewlett-Packard 2009")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("44a44963-d8ec-44a3-b382-69fa3d5b52d9")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
| 1234567eight-test | BugTrackerExample/Properties/AssemblyInfo.cs | C# | asf20 | 1,476 |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BugTrackerExample
{
class Program
{
static void Main(string[] args)
{
var bug = new Bug("Incorrect stock count");
bug.Assign("Joe");
bug.Defer();
bug.Assign("Harry");
bug.Assign("Fred");
bug.Close();
Console.ReadKey(false);
}
}
}
| 1234567eight-test | BugTrackerExample/Program.cs | C# | asf20 | 475 |
package test;
public class test{
public static void main(String[] arg){
System.out.println("ok 1111");
}
}
| 00-project-test | trunk/test stage/test.java | Java | oos | 138 |
package net.avc.video.cutter.natives;
public class Natives {
public static native int takePics();
}
| 123linslouis-android-video-cutter | src/net/avc/video/cutter/natives/Natives.java | Java | asf20 | 102 |
package net.avc.video.cutter;
import net.avc.video.cutter.natives.Natives;
import android.app.Activity;
import android.os.Bundle;
public class TakePics extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
// Load Lib
System.loadLibrary("avformat");
System.loadLibrary("takepics");
Natives.takePics();
} catch (Exception e) {
e.printStackTrace();
}
}
} | 123linslouis-android-video-cutter | src/net/avc/video/cutter/TakePics.java | Java | asf20 | 577 |
/*
* MPEG4 decoder.
* Copyright (c) 2000,2001 Fabrice Bellard
* Copyright (c) 2002-2010 Michael Niedermayer <michaelni@gmx.at>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "mpegvideo.h"
#include "mpeg4video.h"
#include "h263.h"
// The defines below define the number of bits that are read at once for
// reading vlc values. Changing these may improve speed and data cache needs
// be aware though that decreasing them may need the number of stages that is
// passed to get_vlc* to be increased.
#define SPRITE_TRAJ_VLC_BITS 6
#define DC_VLC_BITS 9
#define MB_TYPE_B_VLC_BITS 4
static VLC dc_lum, dc_chrom;
static VLC sprite_trajectory;
static VLC mb_type_b_vlc;
static const int mb_type_b_map[4]= {
MB_TYPE_DIRECT2 | MB_TYPE_L0L1,
MB_TYPE_L0L1 | MB_TYPE_16x16,
MB_TYPE_L1 | MB_TYPE_16x16,
MB_TYPE_L0 | MB_TYPE_16x16,
};
/**
* predicts the ac.
* @param n block index (0-3 are luma, 4-5 are chroma)
* @param dir the ac prediction direction
*/
void mpeg4_pred_ac(MpegEncContext * s, DCTELEM *block, int n,
int dir)
{
int i;
int16_t *ac_val, *ac_val1;
int8_t * const qscale_table= s->current_picture.qscale_table;
/* find prediction */
ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
ac_val1 = ac_val;
if (s->ac_pred) {
if (dir == 0) {
const int xy= s->mb_x-1 + s->mb_y*s->mb_stride;
/* left prediction */
ac_val -= 16;
if(s->mb_x==0 || s->qscale == qscale_table[xy] || n==1 || n==3){
/* same qscale */
for(i=1;i<8;i++) {
block[s->dsp.idct_permutation[i<<3]] += ac_val[i];
}
}else{
/* different qscale, we must rescale */
for(i=1;i<8;i++) {
block[s->dsp.idct_permutation[i<<3]] += ROUNDED_DIV(ac_val[i]*qscale_table[xy], s->qscale);
}
}
} else {
const int xy= s->mb_x + s->mb_y*s->mb_stride - s->mb_stride;
/* top prediction */
ac_val -= 16 * s->block_wrap[n];
if(s->mb_y==0 || s->qscale == qscale_table[xy] || n==2 || n==3){
/* same qscale */
for(i=1;i<8;i++) {
block[s->dsp.idct_permutation[i]] += ac_val[i + 8];
}
}else{
/* different qscale, we must rescale */
for(i=1;i<8;i++) {
block[s->dsp.idct_permutation[i]] += ROUNDED_DIV(ac_val[i + 8]*qscale_table[xy], s->qscale);
}
}
}
}
/* left copy */
for(i=1;i<8;i++)
ac_val1[i ] = block[s->dsp.idct_permutation[i<<3]];
/* top copy */
for(i=1;i<8;i++)
ac_val1[8 + i] = block[s->dsp.idct_permutation[i ]];
}
/**
* check if the next stuff is a resync marker or the end.
* @return 0 if not
*/
static inline int mpeg4_is_resync(MpegEncContext *s){
int bits_count= get_bits_count(&s->gb);
int v= show_bits(&s->gb, 16);
if(s->workaround_bugs&FF_BUG_NO_PADDING){
return 0;
}
while(v<=0xFF){
if(s->pict_type==FF_B_TYPE || (v>>(8-s->pict_type)!=1) || s->partitioned_frame)
break;
skip_bits(&s->gb, 8+s->pict_type);
bits_count+= 8+s->pict_type;
v= show_bits(&s->gb, 16);
}
if(bits_count + 8 >= s->gb.size_in_bits){
v>>=8;
v|= 0x7F >> (7-(bits_count&7));
if(v==0x7F)
return 1;
}else{
if(v == ff_mpeg4_resync_prefix[bits_count&7]){
int len;
GetBitContext gb= s->gb;
skip_bits(&s->gb, 1);
align_get_bits(&s->gb);
for(len=0; len<32; len++){
if(get_bits1(&s->gb)) break;
}
s->gb= gb;
if(len>=ff_mpeg4_get_video_packet_prefix_length(s))
return 1;
}
}
return 0;
}
static void mpeg4_decode_sprite_trajectory(MpegEncContext * s, GetBitContext *gb)
{
int i;
int a= 2<<s->sprite_warping_accuracy;
int rho= 3-s->sprite_warping_accuracy;
int r=16/a;
const int vop_ref[4][2]= {{0,0}, {s->width,0}, {0, s->height}, {s->width, s->height}}; // only true for rectangle shapes
int d[4][2]={{0,0}, {0,0}, {0,0}, {0,0}};
int sprite_ref[4][2];
int virtual_ref[2][2];
int w2, h2, w3, h3;
int alpha=0, beta=0;
int w= s->width;
int h= s->height;
int min_ab;
for(i=0; i<s->num_sprite_warping_points; i++){
int length;
int x=0, y=0;
length= get_vlc2(gb, sprite_trajectory.table, SPRITE_TRAJ_VLC_BITS, 3);
if(length){
x= get_xbits(gb, length);
}
if(!(s->divx_version==500 && s->divx_build==413)) skip_bits1(gb); /* marker bit */
length= get_vlc2(gb, sprite_trajectory.table, SPRITE_TRAJ_VLC_BITS, 3);
if(length){
y=get_xbits(gb, length);
}
skip_bits1(gb); /* marker bit */
s->sprite_traj[i][0]= d[i][0]= x;
s->sprite_traj[i][1]= d[i][1]= y;
}
for(; i<4; i++)
s->sprite_traj[i][0]= s->sprite_traj[i][1]= 0;
while((1<<alpha)<w) alpha++;
while((1<<beta )<h) beta++; // there seems to be a typo in the mpeg4 std for the definition of w' and h'
w2= 1<<alpha;
h2= 1<<beta;
// Note, the 4th point isn't used for GMC
if(s->divx_version==500 && s->divx_build==413){
sprite_ref[0][0]= a*vop_ref[0][0] + d[0][0];
sprite_ref[0][1]= a*vop_ref[0][1] + d[0][1];
sprite_ref[1][0]= a*vop_ref[1][0] + d[0][0] + d[1][0];
sprite_ref[1][1]= a*vop_ref[1][1] + d[0][1] + d[1][1];
sprite_ref[2][0]= a*vop_ref[2][0] + d[0][0] + d[2][0];
sprite_ref[2][1]= a*vop_ref[2][1] + d[0][1] + d[2][1];
} else {
sprite_ref[0][0]= (a>>1)*(2*vop_ref[0][0] + d[0][0]);
sprite_ref[0][1]= (a>>1)*(2*vop_ref[0][1] + d[0][1]);
sprite_ref[1][0]= (a>>1)*(2*vop_ref[1][0] + d[0][0] + d[1][0]);
sprite_ref[1][1]= (a>>1)*(2*vop_ref[1][1] + d[0][1] + d[1][1]);
sprite_ref[2][0]= (a>>1)*(2*vop_ref[2][0] + d[0][0] + d[2][0]);
sprite_ref[2][1]= (a>>1)*(2*vop_ref[2][1] + d[0][1] + d[2][1]);
}
/* sprite_ref[3][0]= (a>>1)*(2*vop_ref[3][0] + d[0][0] + d[1][0] + d[2][0] + d[3][0]);
sprite_ref[3][1]= (a>>1)*(2*vop_ref[3][1] + d[0][1] + d[1][1] + d[2][1] + d[3][1]); */
// this is mostly identical to the mpeg4 std (and is totally unreadable because of that ...)
// perhaps it should be reordered to be more readable ...
// the idea behind this virtual_ref mess is to be able to use shifts later per pixel instead of divides
// so the distance between points is converted from w&h based to w2&h2 based which are of the 2^x form
virtual_ref[0][0]= 16*(vop_ref[0][0] + w2)
+ ROUNDED_DIV(((w - w2)*(r*sprite_ref[0][0] - 16*vop_ref[0][0]) + w2*(r*sprite_ref[1][0] - 16*vop_ref[1][0])),w);
virtual_ref[0][1]= 16*vop_ref[0][1]
+ ROUNDED_DIV(((w - w2)*(r*sprite_ref[0][1] - 16*vop_ref[0][1]) + w2*(r*sprite_ref[1][1] - 16*vop_ref[1][1])),w);
virtual_ref[1][0]= 16*vop_ref[0][0]
+ ROUNDED_DIV(((h - h2)*(r*sprite_ref[0][0] - 16*vop_ref[0][0]) + h2*(r*sprite_ref[2][0] - 16*vop_ref[2][0])),h);
virtual_ref[1][1]= 16*(vop_ref[0][1] + h2)
+ ROUNDED_DIV(((h - h2)*(r*sprite_ref[0][1] - 16*vop_ref[0][1]) + h2*(r*sprite_ref[2][1] - 16*vop_ref[2][1])),h);
switch(s->num_sprite_warping_points)
{
case 0:
s->sprite_offset[0][0]= 0;
s->sprite_offset[0][1]= 0;
s->sprite_offset[1][0]= 0;
s->sprite_offset[1][1]= 0;
s->sprite_delta[0][0]= a;
s->sprite_delta[0][1]= 0;
s->sprite_delta[1][0]= 0;
s->sprite_delta[1][1]= a;
s->sprite_shift[0]= 0;
s->sprite_shift[1]= 0;
break;
case 1: //GMC only
s->sprite_offset[0][0]= sprite_ref[0][0] - a*vop_ref[0][0];
s->sprite_offset[0][1]= sprite_ref[0][1] - a*vop_ref[0][1];
s->sprite_offset[1][0]= ((sprite_ref[0][0]>>1)|(sprite_ref[0][0]&1)) - a*(vop_ref[0][0]/2);
s->sprite_offset[1][1]= ((sprite_ref[0][1]>>1)|(sprite_ref[0][1]&1)) - a*(vop_ref[0][1]/2);
s->sprite_delta[0][0]= a;
s->sprite_delta[0][1]= 0;
s->sprite_delta[1][0]= 0;
s->sprite_delta[1][1]= a;
s->sprite_shift[0]= 0;
s->sprite_shift[1]= 0;
break;
case 2:
s->sprite_offset[0][0]= (sprite_ref[0][0]<<(alpha+rho))
+ (-r*sprite_ref[0][0] + virtual_ref[0][0])*(-vop_ref[0][0])
+ ( r*sprite_ref[0][1] - virtual_ref[0][1])*(-vop_ref[0][1])
+ (1<<(alpha+rho-1));
s->sprite_offset[0][1]= (sprite_ref[0][1]<<(alpha+rho))
+ (-r*sprite_ref[0][1] + virtual_ref[0][1])*(-vop_ref[0][0])
+ (-r*sprite_ref[0][0] + virtual_ref[0][0])*(-vop_ref[0][1])
+ (1<<(alpha+rho-1));
s->sprite_offset[1][0]= ( (-r*sprite_ref[0][0] + virtual_ref[0][0])*(-2*vop_ref[0][0] + 1)
+( r*sprite_ref[0][1] - virtual_ref[0][1])*(-2*vop_ref[0][1] + 1)
+2*w2*r*sprite_ref[0][0]
- 16*w2
+ (1<<(alpha+rho+1)));
s->sprite_offset[1][1]= ( (-r*sprite_ref[0][1] + virtual_ref[0][1])*(-2*vop_ref[0][0] + 1)
+(-r*sprite_ref[0][0] + virtual_ref[0][0])*(-2*vop_ref[0][1] + 1)
+2*w2*r*sprite_ref[0][1]
- 16*w2
+ (1<<(alpha+rho+1)));
s->sprite_delta[0][0]= (-r*sprite_ref[0][0] + virtual_ref[0][0]);
s->sprite_delta[0][1]= (+r*sprite_ref[0][1] - virtual_ref[0][1]);
s->sprite_delta[1][0]= (-r*sprite_ref[0][1] + virtual_ref[0][1]);
s->sprite_delta[1][1]= (-r*sprite_ref[0][0] + virtual_ref[0][0]);
s->sprite_shift[0]= alpha+rho;
s->sprite_shift[1]= alpha+rho+2;
break;
case 3:
min_ab= FFMIN(alpha, beta);
w3= w2>>min_ab;
h3= h2>>min_ab;
s->sprite_offset[0][0]= (sprite_ref[0][0]<<(alpha+beta+rho-min_ab))
+ (-r*sprite_ref[0][0] + virtual_ref[0][0])*h3*(-vop_ref[0][0])
+ (-r*sprite_ref[0][0] + virtual_ref[1][0])*w3*(-vop_ref[0][1])
+ (1<<(alpha+beta+rho-min_ab-1));
s->sprite_offset[0][1]= (sprite_ref[0][1]<<(alpha+beta+rho-min_ab))
+ (-r*sprite_ref[0][1] + virtual_ref[0][1])*h3*(-vop_ref[0][0])
+ (-r*sprite_ref[0][1] + virtual_ref[1][1])*w3*(-vop_ref[0][1])
+ (1<<(alpha+beta+rho-min_ab-1));
s->sprite_offset[1][0]= (-r*sprite_ref[0][0] + virtual_ref[0][0])*h3*(-2*vop_ref[0][0] + 1)
+ (-r*sprite_ref[0][0] + virtual_ref[1][0])*w3*(-2*vop_ref[0][1] + 1)
+ 2*w2*h3*r*sprite_ref[0][0]
- 16*w2*h3
+ (1<<(alpha+beta+rho-min_ab+1));
s->sprite_offset[1][1]= (-r*sprite_ref[0][1] + virtual_ref[0][1])*h3*(-2*vop_ref[0][0] + 1)
+ (-r*sprite_ref[0][1] + virtual_ref[1][1])*w3*(-2*vop_ref[0][1] + 1)
+ 2*w2*h3*r*sprite_ref[0][1]
- 16*w2*h3
+ (1<<(alpha+beta+rho-min_ab+1));
s->sprite_delta[0][0]= (-r*sprite_ref[0][0] + virtual_ref[0][0])*h3;
s->sprite_delta[0][1]= (-r*sprite_ref[0][0] + virtual_ref[1][0])*w3;
s->sprite_delta[1][0]= (-r*sprite_ref[0][1] + virtual_ref[0][1])*h3;
s->sprite_delta[1][1]= (-r*sprite_ref[0][1] + virtual_ref[1][1])*w3;
s->sprite_shift[0]= alpha + beta + rho - min_ab;
s->sprite_shift[1]= alpha + beta + rho - min_ab + 2;
break;
}
/* try to simplify the situation */
if( s->sprite_delta[0][0] == a<<s->sprite_shift[0]
&& s->sprite_delta[0][1] == 0
&& s->sprite_delta[1][0] == 0
&& s->sprite_delta[1][1] == a<<s->sprite_shift[0])
{
s->sprite_offset[0][0]>>=s->sprite_shift[0];
s->sprite_offset[0][1]>>=s->sprite_shift[0];
s->sprite_offset[1][0]>>=s->sprite_shift[1];
s->sprite_offset[1][1]>>=s->sprite_shift[1];
s->sprite_delta[0][0]= a;
s->sprite_delta[0][1]= 0;
s->sprite_delta[1][0]= 0;
s->sprite_delta[1][1]= a;
s->sprite_shift[0]= 0;
s->sprite_shift[1]= 0;
s->real_sprite_warping_points=1;
}
else{
int shift_y= 16 - s->sprite_shift[0];
int shift_c= 16 - s->sprite_shift[1];
for(i=0; i<2; i++){
s->sprite_offset[0][i]<<= shift_y;
s->sprite_offset[1][i]<<= shift_c;
s->sprite_delta[0][i]<<= shift_y;
s->sprite_delta[1][i]<<= shift_y;
s->sprite_shift[i]= 16;
}
s->real_sprite_warping_points= s->num_sprite_warping_points;
}
}
/**
* decodes the next video packet.
* @return <0 if something went wrong
*/
int mpeg4_decode_video_packet_header(MpegEncContext *s)
{
int mb_num_bits= av_log2(s->mb_num - 1) + 1;
int header_extension=0, mb_num, len;
/* is there enough space left for a video packet + header */
if( get_bits_count(&s->gb) > s->gb.size_in_bits-20) return -1;
for(len=0; len<32; len++){
if(get_bits1(&s->gb)) break;
}
if(len!=ff_mpeg4_get_video_packet_prefix_length(s)){
av_log(s->avctx, AV_LOG_ERROR, "marker does not match f_code\n");
return -1;
}
if(s->shape != RECT_SHAPE){
header_extension= get_bits1(&s->gb);
//FIXME more stuff here
}
mb_num= get_bits(&s->gb, mb_num_bits);
if(mb_num>=s->mb_num){
av_log(s->avctx, AV_LOG_ERROR, "illegal mb_num in video packet (%d %d) \n", mb_num, s->mb_num);
return -1;
}
if(s->pict_type == FF_B_TYPE){
while(s->next_picture.mbskip_table[ s->mb_index2xy[ mb_num ] ]) mb_num++;
if(mb_num >= s->mb_num) return -1; // slice contains just skipped MBs which where already decoded
}
s->mb_x= mb_num % s->mb_width;
s->mb_y= mb_num / s->mb_width;
if(s->shape != BIN_ONLY_SHAPE){
int qscale= get_bits(&s->gb, s->quant_precision);
if(qscale)
s->chroma_qscale=s->qscale= qscale;
}
if(s->shape == RECT_SHAPE){
header_extension= get_bits1(&s->gb);
}
if(header_extension){
int time_increment;
int time_incr=0;
while (get_bits1(&s->gb) != 0)
time_incr++;
check_marker(&s->gb, "before time_increment in video packed header");
time_increment= get_bits(&s->gb, s->time_increment_bits);
check_marker(&s->gb, "before vop_coding_type in video packed header");
skip_bits(&s->gb, 2); /* vop coding type */
//FIXME not rect stuff here
if(s->shape != BIN_ONLY_SHAPE){
skip_bits(&s->gb, 3); /* intra dc vlc threshold */
//FIXME don't just ignore everything
if(s->pict_type == FF_S_TYPE && s->vol_sprite_usage==GMC_SPRITE){
mpeg4_decode_sprite_trajectory(s, &s->gb);
av_log(s->avctx, AV_LOG_ERROR, "untested\n");
}
//FIXME reduced res stuff here
if (s->pict_type != FF_I_TYPE) {
int f_code = get_bits(&s->gb, 3); /* fcode_for */
if(f_code==0){
av_log(s->avctx, AV_LOG_ERROR, "Error, video packet header damaged (f_code=0)\n");
}
}
if (s->pict_type == FF_B_TYPE) {
int b_code = get_bits(&s->gb, 3);
if(b_code==0){
av_log(s->avctx, AV_LOG_ERROR, "Error, video packet header damaged (b_code=0)\n");
}
}
}
}
//FIXME new-pred stuff
return 0;
}
/**
* gets the average motion vector for a GMC MB.
* @param n either 0 for the x component or 1 for y
* @return the average MV for a GMC MB
*/
static inline int get_amv(MpegEncContext *s, int n){
int x, y, mb_v, sum, dx, dy, shift;
int len = 1 << (s->f_code + 4);
const int a= s->sprite_warping_accuracy;
if(s->workaround_bugs & FF_BUG_AMV)
len >>= s->quarter_sample;
if(s->real_sprite_warping_points==1){
if(s->divx_version==500 && s->divx_build==413)
sum= s->sprite_offset[0][n] / (1<<(a - s->quarter_sample));
else
sum= RSHIFT(s->sprite_offset[0][n]<<s->quarter_sample, a);
}else{
dx= s->sprite_delta[n][0];
dy= s->sprite_delta[n][1];
shift= s->sprite_shift[0];
if(n) dy -= 1<<(shift + a + 1);
else dx -= 1<<(shift + a + 1);
mb_v= s->sprite_offset[0][n] + dx*s->mb_x*16 + dy*s->mb_y*16;
sum=0;
for(y=0; y<16; y++){
int v;
v= mb_v + dy*y;
//XXX FIXME optimize
for(x=0; x<16; x++){
sum+= v>>shift;
v+= dx;
}
}
sum= RSHIFT(sum, a+8-s->quarter_sample);
}
if (sum < -len) sum= -len;
else if (sum >= len) sum= len-1;
return sum;
}
/**
* decodes the dc value.
* @param n block index (0-3 are luma, 4-5 are chroma)
* @param dir_ptr the prediction direction will be stored here
* @return the quantized dc
*/
static inline int mpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
{
int level, code;
if (n < 4)
code = get_vlc2(&s->gb, dc_lum.table, DC_VLC_BITS, 1);
else
code = get_vlc2(&s->gb, dc_chrom.table, DC_VLC_BITS, 1);
if (code < 0 || code > 9 /* && s->nbit<9 */){
av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n");
return -1;
}
if (code == 0) {
level = 0;
} else {
if(IS_3IV1){
if(code==1)
level= 2*get_bits1(&s->gb)-1;
else{
if(get_bits1(&s->gb))
level = get_bits(&s->gb, code-1) + (1<<(code-1));
else
level = -get_bits(&s->gb, code-1) - (1<<(code-1));
}
}else{
level = get_xbits(&s->gb, code);
}
if (code > 8){
if(get_bits1(&s->gb)==0){ /* marker */
if(s->error_recognition>=2){
av_log(s->avctx, AV_LOG_ERROR, "dc marker bit missing\n");
return -1;
}
}
}
}
return ff_mpeg4_pred_dc(s, n, level, dir_ptr, 0);
}
/**
* decodes first partition.
* @return number of MBs decoded or <0 if an error occurred
*/
static int mpeg4_decode_partition_a(MpegEncContext *s){
int mb_num;
static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
/* decode first partition */
mb_num=0;
s->first_slice_line=1;
for(; s->mb_y<s->mb_height; s->mb_y++){
ff_init_block_index(s);
for(; s->mb_x<s->mb_width; s->mb_x++){
const int xy= s->mb_x + s->mb_y*s->mb_stride;
int cbpc;
int dir=0;
mb_num++;
ff_update_block_index(s);
if(s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y+1)
s->first_slice_line=0;
if(s->pict_type==FF_I_TYPE){
int i;
do{
if(show_bits_long(&s->gb, 19)==DC_MARKER){
return mb_num-1;
}
cbpc = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
if (cbpc < 0){
av_log(s->avctx, AV_LOG_ERROR, "cbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
return -1;
}
}while(cbpc == 8);
s->cbp_table[xy]= cbpc & 3;
s->current_picture.mb_type[xy]= MB_TYPE_INTRA;
s->mb_intra = 1;
if(cbpc & 4) {
ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
}
s->current_picture.qscale_table[xy]= s->qscale;
s->mbintra_table[xy]= 1;
for(i=0; i<6; i++){
int dc_pred_dir;
int dc= mpeg4_decode_dc(s, i, &dc_pred_dir);
if(dc < 0){
av_log(s->avctx, AV_LOG_ERROR, "DC corrupted at %d %d\n", s->mb_x, s->mb_y);
return -1;
}
dir<<=1;
if(dc_pred_dir) dir|=1;
}
s->pred_dir_table[xy]= dir;
}else{ /* P/S_TYPE */
int mx, my, pred_x, pred_y, bits;
int16_t * const mot_val= s->current_picture.motion_val[0][s->block_index[0]];
const int stride= s->b8_stride*2;
try_again:
bits= show_bits(&s->gb, 17);
if(bits==MOTION_MARKER){
return mb_num-1;
}
skip_bits1(&s->gb);
if(bits&0x10000){
/* skip mb */
if(s->pict_type==FF_S_TYPE && s->vol_sprite_usage==GMC_SPRITE){
s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_GMC | MB_TYPE_L0;
mx= get_amv(s, 0);
my= get_amv(s, 1);
}else{
s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
mx=my=0;
}
mot_val[0 ]= mot_val[2 ]=
mot_val[0+stride]= mot_val[2+stride]= mx;
mot_val[1 ]= mot_val[3 ]=
mot_val[1+stride]= mot_val[3+stride]= my;
if(s->mbintra_table[xy])
ff_clean_intra_table_entries(s);
continue;
}
cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
if (cbpc < 0){
av_log(s->avctx, AV_LOG_ERROR, "cbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
return -1;
}
if(cbpc == 20)
goto try_again;
s->cbp_table[xy]= cbpc&(8+3); //8 is dquant
s->mb_intra = ((cbpc & 4) != 0);
if(s->mb_intra){
s->current_picture.mb_type[xy]= MB_TYPE_INTRA;
s->mbintra_table[xy]= 1;
mot_val[0 ]= mot_val[2 ]=
mot_val[0+stride]= mot_val[2+stride]= 0;
mot_val[1 ]= mot_val[3 ]=
mot_val[1+stride]= mot_val[3+stride]= 0;
}else{
if(s->mbintra_table[xy])
ff_clean_intra_table_entries(s);
if(s->pict_type==FF_S_TYPE && s->vol_sprite_usage==GMC_SPRITE && (cbpc & 16) == 0)
s->mcsel= get_bits1(&s->gb);
else s->mcsel= 0;
if ((cbpc & 16) == 0) {
/* 16x16 motion prediction */
h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
if(!s->mcsel){
mx = h263_decode_motion(s, pred_x, s->f_code);
if (mx >= 0xffff)
return -1;
my = h263_decode_motion(s, pred_y, s->f_code);
if (my >= 0xffff)
return -1;
s->current_picture.mb_type[xy]= MB_TYPE_16x16 | MB_TYPE_L0;
} else {
mx = get_amv(s, 0);
my = get_amv(s, 1);
s->current_picture.mb_type[xy]= MB_TYPE_16x16 | MB_TYPE_GMC | MB_TYPE_L0;
}
mot_val[0 ]= mot_val[2 ] =
mot_val[0+stride]= mot_val[2+stride]= mx;
mot_val[1 ]= mot_val[3 ]=
mot_val[1+stride]= mot_val[3+stride]= my;
} else {
int i;
s->current_picture.mb_type[xy]= MB_TYPE_8x8 | MB_TYPE_L0;
for(i=0;i<4;i++) {
int16_t *mot_val= h263_pred_motion(s, i, 0, &pred_x, &pred_y);
mx = h263_decode_motion(s, pred_x, s->f_code);
if (mx >= 0xffff)
return -1;
my = h263_decode_motion(s, pred_y, s->f_code);
if (my >= 0xffff)
return -1;
mot_val[0] = mx;
mot_val[1] = my;
}
}
}
}
}
s->mb_x= 0;
}
return mb_num;
}
/**
* decode second partition.
* @return <0 if an error occurred
*/
static int mpeg4_decode_partition_b(MpegEncContext *s, int mb_count){
int mb_num=0;
static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
s->mb_x= s->resync_mb_x;
s->first_slice_line=1;
for(s->mb_y= s->resync_mb_y; mb_num < mb_count; s->mb_y++){
ff_init_block_index(s);
for(; mb_num < mb_count && s->mb_x<s->mb_width; s->mb_x++){
const int xy= s->mb_x + s->mb_y*s->mb_stride;
mb_num++;
ff_update_block_index(s);
if(s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y+1)
s->first_slice_line=0;
if(s->pict_type==FF_I_TYPE){
int ac_pred= get_bits1(&s->gb);
int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
if(cbpy<0){
av_log(s->avctx, AV_LOG_ERROR, "cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
return -1;
}
s->cbp_table[xy]|= cbpy<<2;
s->current_picture.mb_type[xy] |= ac_pred*MB_TYPE_ACPRED;
}else{ /* P || S_TYPE */
if(IS_INTRA(s->current_picture.mb_type[xy])){
int dir=0,i;
int ac_pred = get_bits1(&s->gb);
int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
if(cbpy<0){
av_log(s->avctx, AV_LOG_ERROR, "I cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
return -1;
}
if(s->cbp_table[xy] & 8) {
ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
}
s->current_picture.qscale_table[xy]= s->qscale;
for(i=0; i<6; i++){
int dc_pred_dir;
int dc= mpeg4_decode_dc(s, i, &dc_pred_dir);
if(dc < 0){
av_log(s->avctx, AV_LOG_ERROR, "DC corrupted at %d %d\n", s->mb_x, s->mb_y);
return -1;
}
dir<<=1;
if(dc_pred_dir) dir|=1;
}
s->cbp_table[xy]&= 3; //remove dquant
s->cbp_table[xy]|= cbpy<<2;
s->current_picture.mb_type[xy] |= ac_pred*MB_TYPE_ACPRED;
s->pred_dir_table[xy]= dir;
}else if(IS_SKIP(s->current_picture.mb_type[xy])){
s->current_picture.qscale_table[xy]= s->qscale;
s->cbp_table[xy]= 0;
}else{
int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
if(cbpy<0){
av_log(s->avctx, AV_LOG_ERROR, "P cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
return -1;
}
if(s->cbp_table[xy] & 8) {
ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
}
s->current_picture.qscale_table[xy]= s->qscale;
s->cbp_table[xy]&= 3; //remove dquant
s->cbp_table[xy]|= (cbpy^0xf)<<2;
}
}
}
if(mb_num >= mb_count) return 0;
s->mb_x= 0;
}
return 0;
}
/**
* decodes the first & second partition
* @return <0 if error (and sets error type in the error_status_table)
*/
int ff_mpeg4_decode_partitions(MpegEncContext *s)
{
int mb_num;
const int part_a_error= s->pict_type==FF_I_TYPE ? (DC_ERROR|MV_ERROR) : MV_ERROR;
const int part_a_end = s->pict_type==FF_I_TYPE ? (DC_END |MV_END) : MV_END;
mb_num= mpeg4_decode_partition_a(s);
if(mb_num<0){
ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, part_a_error);
return -1;
}
if(s->resync_mb_x + s->resync_mb_y*s->mb_width + mb_num > s->mb_num){
av_log(s->avctx, AV_LOG_ERROR, "slice below monitor ...\n");
ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, part_a_error);
return -1;
}
s->mb_num_left= mb_num;
if(s->pict_type==FF_I_TYPE){
while(show_bits(&s->gb, 9) == 1)
skip_bits(&s->gb, 9);
if(get_bits_long(&s->gb, 19)!=DC_MARKER){
av_log(s->avctx, AV_LOG_ERROR, "marker missing after first I partition at %d %d\n", s->mb_x, s->mb_y);
return -1;
}
}else{
while(show_bits(&s->gb, 10) == 1)
skip_bits(&s->gb, 10);
if(get_bits(&s->gb, 17)!=MOTION_MARKER){
av_log(s->avctx, AV_LOG_ERROR, "marker missing after first P partition at %d %d\n", s->mb_x, s->mb_y);
return -1;
}
}
ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, part_a_end);
if( mpeg4_decode_partition_b(s, mb_num) < 0){
if(s->pict_type==FF_P_TYPE)
ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, DC_ERROR);
return -1;
}else{
if(s->pict_type==FF_P_TYPE)
ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, DC_END);
}
return 0;
}
/**
* decodes a block.
* @return <0 if an error occurred
*/
static inline int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
int n, int coded, int intra, int rvlc)
{
int level, i, last, run;
int dc_pred_dir;
RLTable * rl;
RL_VLC_ELEM * rl_vlc;
const uint8_t * scan_table;
int qmul, qadd;
//Note intra & rvlc should be optimized away if this is inlined
if(intra) {
if(s->use_intra_dc_vlc){
/* DC coef */
if(s->partitioned_frame){
level = s->dc_val[0][ s->block_index[n] ];
if(n<4) level= FASTDIV((level + (s->y_dc_scale>>1)), s->y_dc_scale);
else level= FASTDIV((level + (s->c_dc_scale>>1)), s->c_dc_scale);
dc_pred_dir= (s->pred_dir_table[s->mb_x + s->mb_y*s->mb_stride]<<n)&32;
}else{
level = mpeg4_decode_dc(s, n, &dc_pred_dir);
if (level < 0)
return -1;
}
block[0] = level;
i = 0;
}else{
i = -1;
ff_mpeg4_pred_dc(s, n, 0, &dc_pred_dir, 0);
}
if (!coded)
goto not_coded;
if(rvlc){
rl = &rvlc_rl_intra;
rl_vlc = rvlc_rl_intra.rl_vlc[0];
}else{
rl = &ff_mpeg4_rl_intra;
rl_vlc = ff_mpeg4_rl_intra.rl_vlc[0];
}
if (s->ac_pred) {
if (dc_pred_dir == 0)
scan_table = s->intra_v_scantable.permutated; /* left */
else
scan_table = s->intra_h_scantable.permutated; /* top */
} else {
scan_table = s->intra_scantable.permutated;
}
qmul=1;
qadd=0;
} else {
i = -1;
if (!coded) {
s->block_last_index[n] = i;
return 0;
}
if(rvlc) rl = &rvlc_rl_inter;
else rl = &ff_h263_rl_inter;
scan_table = s->intra_scantable.permutated;
if(s->mpeg_quant){
qmul=1;
qadd=0;
if(rvlc){
rl_vlc = rvlc_rl_inter.rl_vlc[0];
}else{
rl_vlc = ff_h263_rl_inter.rl_vlc[0];
}
}else{
qmul = s->qscale << 1;
qadd = (s->qscale - 1) | 1;
if(rvlc){
rl_vlc = rvlc_rl_inter.rl_vlc[s->qscale];
}else{
rl_vlc = ff_h263_rl_inter.rl_vlc[s->qscale];
}
}
}
{
OPEN_READER(re, &s->gb);
for(;;) {
UPDATE_CACHE(re, &s->gb);
GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 0);
if (level==0) {
/* escape */
if(rvlc){
if(SHOW_UBITS(re, &s->gb, 1)==0){
av_log(s->avctx, AV_LOG_ERROR, "1. marker bit missing in rvlc esc\n");
return -1;
}; SKIP_CACHE(re, &s->gb, 1);
last= SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1);
run= SHOW_UBITS(re, &s->gb, 6); LAST_SKIP_CACHE(re, &s->gb, 6);
SKIP_COUNTER(re, &s->gb, 1+1+6);
UPDATE_CACHE(re, &s->gb);
if(SHOW_UBITS(re, &s->gb, 1)==0){
av_log(s->avctx, AV_LOG_ERROR, "2. marker bit missing in rvlc esc\n");
return -1;
}; SKIP_CACHE(re, &s->gb, 1);
level= SHOW_UBITS(re, &s->gb, 11); SKIP_CACHE(re, &s->gb, 11);
if(SHOW_UBITS(re, &s->gb, 5)!=0x10){
av_log(s->avctx, AV_LOG_ERROR, "reverse esc missing\n");
return -1;
}; SKIP_CACHE(re, &s->gb, 5);
level= level * qmul + qadd;
level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); LAST_SKIP_CACHE(re, &s->gb, 1);
SKIP_COUNTER(re, &s->gb, 1+11+5+1);
i+= run + 1;
if(last) i+=192;
}else{
int cache;
cache= GET_CACHE(re, &s->gb);
if(IS_3IV1)
cache ^= 0xC0000000;
if (cache&0x80000000) {
if (cache&0x40000000) {
/* third escape */
SKIP_CACHE(re, &s->gb, 2);
last= SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1);
run= SHOW_UBITS(re, &s->gb, 6); LAST_SKIP_CACHE(re, &s->gb, 6);
SKIP_COUNTER(re, &s->gb, 2+1+6);
UPDATE_CACHE(re, &s->gb);
if(IS_3IV1){
level= SHOW_SBITS(re, &s->gb, 12); LAST_SKIP_BITS(re, &s->gb, 12);
}else{
if(SHOW_UBITS(re, &s->gb, 1)==0){
av_log(s->avctx, AV_LOG_ERROR, "1. marker bit missing in 3. esc\n");
return -1;
}; SKIP_CACHE(re, &s->gb, 1);
level= SHOW_SBITS(re, &s->gb, 12); SKIP_CACHE(re, &s->gb, 12);
if(SHOW_UBITS(re, &s->gb, 1)==0){
av_log(s->avctx, AV_LOG_ERROR, "2. marker bit missing in 3. esc\n");
return -1;
}; LAST_SKIP_CACHE(re, &s->gb, 1);
SKIP_COUNTER(re, &s->gb, 1+12+1);
}
#if 0
if(s->error_recognition >= FF_ER_COMPLIANT){
const int abs_level= FFABS(level);
if(abs_level<=MAX_LEVEL && run<=MAX_RUN){
const int run1= run - rl->max_run[last][abs_level] - 1;
if(abs_level <= rl->max_level[last][run]){
av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, vlc encoding possible\n");
return -1;
}
if(s->error_recognition > FF_ER_COMPLIANT){
if(abs_level <= rl->max_level[last][run]*2){
av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 1 encoding possible\n");
return -1;
}
if(run1 >= 0 && abs_level <= rl->max_level[last][run1]){
av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 2 encoding possible\n");
return -1;
}
}
}
}
#endif
if (level>0) level= level * qmul + qadd;
else level= level * qmul - qadd;
if((unsigned)(level + 2048) > 4095){
if(s->error_recognition > FF_ER_COMPLIANT){
if(level > 2560 || level<-2560){
av_log(s->avctx, AV_LOG_ERROR, "|level| overflow in 3. esc, qp=%d\n", s->qscale);
return -1;
}
}
level= level<0 ? -2048 : 2047;
}
i+= run + 1;
if(last) i+=192;
} else {
/* second escape */
#if MIN_CACHE_BITS < 20
LAST_SKIP_BITS(re, &s->gb, 2);
UPDATE_CACHE(re, &s->gb);
#else
SKIP_BITS(re, &s->gb, 2);
#endif
GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
i+= run + rl->max_run[run>>7][level/qmul] +1; //FIXME opt indexing
level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
LAST_SKIP_BITS(re, &s->gb, 1);
}
} else {
/* first escape */
#if MIN_CACHE_BITS < 19
LAST_SKIP_BITS(re, &s->gb, 1);
UPDATE_CACHE(re, &s->gb);
#else
SKIP_BITS(re, &s->gb, 1);
#endif
GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
i+= run;
level = level + rl->max_level[run>>7][(run-1)&63] * qmul;//FIXME opt indexing
level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
LAST_SKIP_BITS(re, &s->gb, 1);
}
}
} else {
i+= run;
level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
LAST_SKIP_BITS(re, &s->gb, 1);
}
if (i > 62){
i-= 192;
if(i&(~63)){
av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
return -1;
}
block[scan_table[i]] = level;
break;
}
block[scan_table[i]] = level;
}
CLOSE_READER(re, &s->gb);
}
not_coded:
if (intra) {
if(!s->use_intra_dc_vlc){
block[0] = ff_mpeg4_pred_dc(s, n, block[0], &dc_pred_dir, 0);
i -= i>>31; //if(i == -1) i=0;
}
mpeg4_pred_ac(s, block, n, dc_pred_dir);
if (s->ac_pred) {
i = 63; /* XXX: not optimal */
}
}
s->block_last_index[n] = i;
return 0;
}
/**
* decode partition C of one MB.
* @return <0 if an error occurred
*/
static int mpeg4_decode_partitioned_mb(MpegEncContext *s, DCTELEM block[6][64])
{
int cbp, mb_type;
const int xy= s->mb_x + s->mb_y*s->mb_stride;
mb_type= s->current_picture.mb_type[xy];
cbp = s->cbp_table[xy];
s->use_intra_dc_vlc= s->qscale < s->intra_dc_threshold;
if(s->current_picture.qscale_table[xy] != s->qscale){
ff_set_qscale(s, s->current_picture.qscale_table[xy] );
}
if (s->pict_type == FF_P_TYPE || s->pict_type==FF_S_TYPE) {
int i;
for(i=0; i<4; i++){
s->mv[0][i][0] = s->current_picture.motion_val[0][ s->block_index[i] ][0];
s->mv[0][i][1] = s->current_picture.motion_val[0][ s->block_index[i] ][1];
}
s->mb_intra = IS_INTRA(mb_type);
if (IS_SKIP(mb_type)) {
/* skip mb */
for(i=0;i<6;i++)
s->block_last_index[i] = -1;
s->mv_dir = MV_DIR_FORWARD;
s->mv_type = MV_TYPE_16X16;
if(s->pict_type==FF_S_TYPE && s->vol_sprite_usage==GMC_SPRITE){
s->mcsel=1;
s->mb_skipped = 0;
}else{
s->mcsel=0;
s->mb_skipped = 1;
}
}else if(s->mb_intra){
s->ac_pred = IS_ACPRED(s->current_picture.mb_type[xy]);
}else if(!s->mb_intra){
// s->mcsel= 0; //FIXME do we need to init that
s->mv_dir = MV_DIR_FORWARD;
if (IS_8X8(mb_type)) {
s->mv_type = MV_TYPE_8X8;
} else {
s->mv_type = MV_TYPE_16X16;
}
}
} else { /* I-Frame */
s->mb_intra = 1;
s->ac_pred = IS_ACPRED(s->current_picture.mb_type[xy]);
}
if (!IS_SKIP(mb_type)) {
int i;
s->dsp.clear_blocks(s->block[0]);
/* decode each block */
for (i = 0; i < 6; i++) {
if(mpeg4_decode_block(s, block[i], i, cbp&32, s->mb_intra, s->rvlc) < 0){
av_log(s->avctx, AV_LOG_ERROR, "texture corrupted at %d %d %d\n", s->mb_x, s->mb_y, s->mb_intra);
return -1;
}
cbp+=cbp;
}
}
/* per-MB end of slice check */
if(--s->mb_num_left <= 0){
if(mpeg4_is_resync(s))
return SLICE_END;
else
return SLICE_NOEND;
}else{
if(mpeg4_is_resync(s)){
const int delta= s->mb_x + 1 == s->mb_width ? 2 : 1;
if(s->cbp_table[xy+delta])
return SLICE_END;
}
return SLICE_OK;
}
}
static int mpeg4_decode_mb(MpegEncContext *s,
DCTELEM block[6][64])
{
int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
int16_t *mot_val;
static int8_t quant_tab[4] = { -1, -2, 1, 2 };
const int xy= s->mb_x + s->mb_y * s->mb_stride;
assert(s->h263_pred);
if (s->pict_type == FF_P_TYPE || s->pict_type==FF_S_TYPE) {
do{
if (get_bits1(&s->gb)) {
/* skip mb */
s->mb_intra = 0;
for(i=0;i<6;i++)
s->block_last_index[i] = -1;
s->mv_dir = MV_DIR_FORWARD;
s->mv_type = MV_TYPE_16X16;
if(s->pict_type==FF_S_TYPE && s->vol_sprite_usage==GMC_SPRITE){
s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_GMC | MB_TYPE_16x16 | MB_TYPE_L0;
s->mcsel=1;
s->mv[0][0][0]= get_amv(s, 0);
s->mv[0][0][1]= get_amv(s, 1);
s->mb_skipped = 0;
}else{
s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
s->mcsel=0;
s->mv[0][0][0] = 0;
s->mv[0][0][1] = 0;
s->mb_skipped = 1;
}
goto end;
}
cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
if (cbpc < 0){
av_log(s->avctx, AV_LOG_ERROR, "cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
return -1;
}
}while(cbpc == 20);
s->dsp.clear_blocks(s->block[0]);
dquant = cbpc & 8;
s->mb_intra = ((cbpc & 4) != 0);
if (s->mb_intra) goto intra;
if(s->pict_type==FF_S_TYPE && s->vol_sprite_usage==GMC_SPRITE && (cbpc & 16) == 0)
s->mcsel= get_bits1(&s->gb);
else s->mcsel= 0;
cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1) ^ 0x0F;
cbp = (cbpc & 3) | (cbpy << 2);
if (dquant) {
ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
}
if((!s->progressive_sequence) && (cbp || (s->workaround_bugs&FF_BUG_XVID_ILACE)))
s->interlaced_dct= get_bits1(&s->gb);
s->mv_dir = MV_DIR_FORWARD;
if ((cbpc & 16) == 0) {
if(s->mcsel){
s->current_picture.mb_type[xy]= MB_TYPE_GMC | MB_TYPE_16x16 | MB_TYPE_L0;
/* 16x16 global motion prediction */
s->mv_type = MV_TYPE_16X16;
mx= get_amv(s, 0);
my= get_amv(s, 1);
s->mv[0][0][0] = mx;
s->mv[0][0][1] = my;
}else if((!s->progressive_sequence) && get_bits1(&s->gb)){
s->current_picture.mb_type[xy]= MB_TYPE_16x8 | MB_TYPE_L0 | MB_TYPE_INTERLACED;
/* 16x8 field motion prediction */
s->mv_type= MV_TYPE_FIELD;
s->field_select[0][0]= get_bits1(&s->gb);
s->field_select[0][1]= get_bits1(&s->gb);
h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
for(i=0; i<2; i++){
mx = h263_decode_motion(s, pred_x, s->f_code);
if (mx >= 0xffff)
return -1;
my = h263_decode_motion(s, pred_y/2, s->f_code);
if (my >= 0xffff)
return -1;
s->mv[0][i][0] = mx;
s->mv[0][i][1] = my;
}
}else{
s->current_picture.mb_type[xy]= MB_TYPE_16x16 | MB_TYPE_L0;
/* 16x16 motion prediction */
s->mv_type = MV_TYPE_16X16;
h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
mx = h263_decode_motion(s, pred_x, s->f_code);
if (mx >= 0xffff)
return -1;
my = h263_decode_motion(s, pred_y, s->f_code);
if (my >= 0xffff)
return -1;
s->mv[0][0][0] = mx;
s->mv[0][0][1] = my;
}
} else {
s->current_picture.mb_type[xy]= MB_TYPE_8x8 | MB_TYPE_L0;
s->mv_type = MV_TYPE_8X8;
for(i=0;i<4;i++) {
mot_val = h263_pred_motion(s, i, 0, &pred_x, &pred_y);
mx = h263_decode_motion(s, pred_x, s->f_code);
if (mx >= 0xffff)
return -1;
my = h263_decode_motion(s, pred_y, s->f_code);
if (my >= 0xffff)
return -1;
s->mv[0][i][0] = mx;
s->mv[0][i][1] = my;
mot_val[0] = mx;
mot_val[1] = my;
}
}
} else if(s->pict_type==FF_B_TYPE) {
int modb1; // first bit of modb
int modb2; // second bit of modb
int mb_type;
s->mb_intra = 0; //B-frames never contain intra blocks
s->mcsel=0; // ... true gmc blocks
if(s->mb_x==0){
for(i=0; i<2; i++){
s->last_mv[i][0][0]=
s->last_mv[i][0][1]=
s->last_mv[i][1][0]=
s->last_mv[i][1][1]= 0;
}
}
/* if we skipped it in the future P Frame than skip it now too */
s->mb_skipped= s->next_picture.mbskip_table[s->mb_y * s->mb_stride + s->mb_x]; // Note, skiptab=0 if last was GMC
if(s->mb_skipped){
/* skip mb */
for(i=0;i<6;i++)
s->block_last_index[i] = -1;
s->mv_dir = MV_DIR_FORWARD;
s->mv_type = MV_TYPE_16X16;
s->mv[0][0][0] = 0;
s->mv[0][0][1] = 0;
s->mv[1][0][0] = 0;
s->mv[1][0][1] = 0;
s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
goto end;
}
modb1= get_bits1(&s->gb);
if(modb1){
mb_type= MB_TYPE_DIRECT2 | MB_TYPE_SKIP | MB_TYPE_L0L1; //like MB_TYPE_B_DIRECT but no vectors coded
cbp=0;
}else{
modb2= get_bits1(&s->gb);
mb_type= get_vlc2(&s->gb, mb_type_b_vlc.table, MB_TYPE_B_VLC_BITS, 1);
if(mb_type<0){
av_log(s->avctx, AV_LOG_ERROR, "illegal MB_type\n");
return -1;
}
mb_type= mb_type_b_map[ mb_type ];
if(modb2) cbp= 0;
else{
s->dsp.clear_blocks(s->block[0]);
cbp= get_bits(&s->gb, 6);
}
if ((!IS_DIRECT(mb_type)) && cbp) {
if(get_bits1(&s->gb)){
ff_set_qscale(s, s->qscale + get_bits1(&s->gb)*4 - 2);
}
}
if(!s->progressive_sequence){
if(cbp)
s->interlaced_dct= get_bits1(&s->gb);
if(!IS_DIRECT(mb_type) && get_bits1(&s->gb)){
mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
mb_type &= ~MB_TYPE_16x16;
if(USES_LIST(mb_type, 0)){
s->field_select[0][0]= get_bits1(&s->gb);
s->field_select[0][1]= get_bits1(&s->gb);
}
if(USES_LIST(mb_type, 1)){
s->field_select[1][0]= get_bits1(&s->gb);
s->field_select[1][1]= get_bits1(&s->gb);
}
}
}
s->mv_dir = 0;
if((mb_type & (MB_TYPE_DIRECT2|MB_TYPE_INTERLACED)) == 0){
s->mv_type= MV_TYPE_16X16;
if(USES_LIST(mb_type, 0)){
s->mv_dir = MV_DIR_FORWARD;
mx = h263_decode_motion(s, s->last_mv[0][0][0], s->f_code);
my = h263_decode_motion(s, s->last_mv[0][0][1], s->f_code);
s->last_mv[0][1][0]= s->last_mv[0][0][0]= s->mv[0][0][0] = mx;
s->last_mv[0][1][1]= s->last_mv[0][0][1]= s->mv[0][0][1] = my;
}
if(USES_LIST(mb_type, 1)){
s->mv_dir |= MV_DIR_BACKWARD;
mx = h263_decode_motion(s, s->last_mv[1][0][0], s->b_code);
my = h263_decode_motion(s, s->last_mv[1][0][1], s->b_code);
s->last_mv[1][1][0]= s->last_mv[1][0][0]= s->mv[1][0][0] = mx;
s->last_mv[1][1][1]= s->last_mv[1][0][1]= s->mv[1][0][1] = my;
}
}else if(!IS_DIRECT(mb_type)){
s->mv_type= MV_TYPE_FIELD;
if(USES_LIST(mb_type, 0)){
s->mv_dir = MV_DIR_FORWARD;
for(i=0; i<2; i++){
mx = h263_decode_motion(s, s->last_mv[0][i][0] , s->f_code);
my = h263_decode_motion(s, s->last_mv[0][i][1]/2, s->f_code);
s->last_mv[0][i][0]= s->mv[0][i][0] = mx;
s->last_mv[0][i][1]= (s->mv[0][i][1] = my)*2;
}
}
if(USES_LIST(mb_type, 1)){
s->mv_dir |= MV_DIR_BACKWARD;
for(i=0; i<2; i++){
mx = h263_decode_motion(s, s->last_mv[1][i][0] , s->b_code);
my = h263_decode_motion(s, s->last_mv[1][i][1]/2, s->b_code);
s->last_mv[1][i][0]= s->mv[1][i][0] = mx;
s->last_mv[1][i][1]= (s->mv[1][i][1] = my)*2;
}
}
}
}
if(IS_DIRECT(mb_type)){
if(IS_SKIP(mb_type))
mx=my=0;
else{
mx = h263_decode_motion(s, 0, 1);
my = h263_decode_motion(s, 0, 1);
}
s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
mb_type |= ff_mpeg4_set_direct_mv(s, mx, my);
}
s->current_picture.mb_type[xy]= mb_type;
} else { /* I-Frame */
do{
cbpc = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
if (cbpc < 0){
av_log(s->avctx, AV_LOG_ERROR, "I cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
return -1;
}
}while(cbpc == 8);
dquant = cbpc & 4;
s->mb_intra = 1;
intra:
s->ac_pred = get_bits1(&s->gb);
if(s->ac_pred)
s->current_picture.mb_type[xy]= MB_TYPE_INTRA | MB_TYPE_ACPRED;
else
s->current_picture.mb_type[xy]= MB_TYPE_INTRA;
cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
if(cbpy<0){
av_log(s->avctx, AV_LOG_ERROR, "I cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
return -1;
}
cbp = (cbpc & 3) | (cbpy << 2);
s->use_intra_dc_vlc= s->qscale < s->intra_dc_threshold;
if (dquant) {
ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
}
if(!s->progressive_sequence)
s->interlaced_dct= get_bits1(&s->gb);
s->dsp.clear_blocks(s->block[0]);
/* decode each block */
for (i = 0; i < 6; i++) {
if (mpeg4_decode_block(s, block[i], i, cbp&32, 1, 0) < 0)
return -1;
cbp+=cbp;
}
goto end;
}
/* decode each block */
for (i = 0; i < 6; i++) {
if (mpeg4_decode_block(s, block[i], i, cbp&32, 0, 0) < 0)
return -1;
cbp+=cbp;
}
end:
/* per-MB end of slice check */
if(s->codec_id==CODEC_ID_MPEG4){
if(mpeg4_is_resync(s)){
const int delta= s->mb_x + 1 == s->mb_width ? 2 : 1;
if(s->pict_type==FF_B_TYPE && s->next_picture.mbskip_table[xy + delta])
return SLICE_OK;
return SLICE_END;
}
}
return SLICE_OK;
}
static int mpeg4_decode_gop_header(MpegEncContext * s, GetBitContext *gb){
int hours, minutes, seconds;
hours= get_bits(gb, 5);
minutes= get_bits(gb, 6);
skip_bits1(gb);
seconds= get_bits(gb, 6);
s->time_base= seconds + 60*(minutes + 60*hours);
skip_bits1(gb);
skip_bits1(gb);
return 0;
}
static int decode_vol_header(MpegEncContext *s, GetBitContext *gb){
int width, height, vo_ver_id;
/* vol header */
skip_bits(gb, 1); /* random access */
s->vo_type= get_bits(gb, 8);
if (get_bits1(gb) != 0) { /* is_ol_id */
vo_ver_id = get_bits(gb, 4); /* vo_ver_id */
skip_bits(gb, 3); /* vo_priority */
} else {
vo_ver_id = 1;
}
s->aspect_ratio_info= get_bits(gb, 4);
if(s->aspect_ratio_info == FF_ASPECT_EXTENDED){
s->avctx->sample_aspect_ratio.num= get_bits(gb, 8); // par_width
s->avctx->sample_aspect_ratio.den= get_bits(gb, 8); // par_height
}else{
s->avctx->sample_aspect_ratio= ff_h263_pixel_aspect[s->aspect_ratio_info];
}
if ((s->vol_control_parameters=get_bits1(gb))) { /* vol control parameter */
int chroma_format= get_bits(gb, 2);
if(chroma_format!=CHROMA_420){
av_log(s->avctx, AV_LOG_ERROR, "illegal chroma format\n");
}
s->low_delay= get_bits1(gb);
if(get_bits1(gb)){ /* vbv parameters */
get_bits(gb, 15); /* first_half_bitrate */
skip_bits1(gb); /* marker */
get_bits(gb, 15); /* latter_half_bitrate */
skip_bits1(gb); /* marker */
get_bits(gb, 15); /* first_half_vbv_buffer_size */
skip_bits1(gb); /* marker */
get_bits(gb, 3); /* latter_half_vbv_buffer_size */
get_bits(gb, 11); /* first_half_vbv_occupancy */
skip_bits1(gb); /* marker */
get_bits(gb, 15); /* latter_half_vbv_occupancy */
skip_bits1(gb); /* marker */
}
}else{
// set low delay flag only once the smartest? low delay detection won't be overriden
if(s->picture_number==0)
s->low_delay=0;
}
s->shape = get_bits(gb, 2); /* vol shape */
if(s->shape != RECT_SHAPE) av_log(s->avctx, AV_LOG_ERROR, "only rectangular vol supported\n");
if(s->shape == GRAY_SHAPE && vo_ver_id != 1){
av_log(s->avctx, AV_LOG_ERROR, "Gray shape not supported\n");
skip_bits(gb, 4); //video_object_layer_shape_extension
}
check_marker(gb, "before time_increment_resolution");
s->avctx->time_base.den = get_bits(gb, 16);
if(!s->avctx->time_base.den){
av_log(s->avctx, AV_LOG_ERROR, "time_base.den==0\n");
return -1;
}
s->time_increment_bits = av_log2(s->avctx->time_base.den - 1) + 1;
if (s->time_increment_bits < 1)
s->time_increment_bits = 1;
check_marker(gb, "before fixed_vop_rate");
if (get_bits1(gb) != 0) { /* fixed_vop_rate */
s->avctx->time_base.num = get_bits(gb, s->time_increment_bits);
}else
s->avctx->time_base.num = 1;
s->t_frame=0;
if (s->shape != BIN_ONLY_SHAPE) {
if (s->shape == RECT_SHAPE) {
skip_bits1(gb); /* marker */
width = get_bits(gb, 13);
skip_bits1(gb); /* marker */
height = get_bits(gb, 13);
skip_bits1(gb); /* marker */
if(width && height && !(s->width && s->codec_tag == AV_RL32("MP4S"))){ /* they should be non zero but who knows ... */
s->width = width;
s->height = height;
}
}
s->progressive_sequence=
s->progressive_frame= get_bits1(gb)^1;
s->interlaced_dct=0;
if(!get_bits1(gb) && (s->avctx->debug & FF_DEBUG_PICT_INFO))
av_log(s->avctx, AV_LOG_INFO, "MPEG4 OBMC not supported (very likely buggy encoder)\n"); /* OBMC Disable */
if (vo_ver_id == 1) {
s->vol_sprite_usage = get_bits1(gb); /* vol_sprite_usage */
} else {
s->vol_sprite_usage = get_bits(gb, 2); /* vol_sprite_usage */
}
if(s->vol_sprite_usage==STATIC_SPRITE) av_log(s->avctx, AV_LOG_ERROR, "Static Sprites not supported\n");
if(s->vol_sprite_usage==STATIC_SPRITE || s->vol_sprite_usage==GMC_SPRITE){
if(s->vol_sprite_usage==STATIC_SPRITE){
s->sprite_width = get_bits(gb, 13);
skip_bits1(gb); /* marker */
s->sprite_height= get_bits(gb, 13);
skip_bits1(gb); /* marker */
s->sprite_left = get_bits(gb, 13);
skip_bits1(gb); /* marker */
s->sprite_top = get_bits(gb, 13);
skip_bits1(gb); /* marker */
}
s->num_sprite_warping_points= get_bits(gb, 6);
if(s->num_sprite_warping_points > 3){
av_log(s->avctx, AV_LOG_ERROR, "%d sprite_warping_points\n", s->num_sprite_warping_points);
s->num_sprite_warping_points= 0;
return -1;
}
s->sprite_warping_accuracy = get_bits(gb, 2);
s->sprite_brightness_change= get_bits1(gb);
if(s->vol_sprite_usage==STATIC_SPRITE)
s->low_latency_sprite= get_bits1(gb);
}
// FIXME sadct disable bit if verid!=1 && shape not rect
if (get_bits1(gb) == 1) { /* not_8_bit */
s->quant_precision = get_bits(gb, 4); /* quant_precision */
if(get_bits(gb, 4)!=8) av_log(s->avctx, AV_LOG_ERROR, "N-bit not supported\n"); /* bits_per_pixel */
if(s->quant_precision!=5) av_log(s->avctx, AV_LOG_ERROR, "quant precision %d\n", s->quant_precision);
} else {
s->quant_precision = 5;
}
// FIXME a bunch of grayscale shape things
if((s->mpeg_quant=get_bits1(gb))){ /* vol_quant_type */
int i, v;
/* load default matrixes */
for(i=0; i<64; i++){
int j= s->dsp.idct_permutation[i];
v= ff_mpeg4_default_intra_matrix[i];
s->intra_matrix[j]= v;
s->chroma_intra_matrix[j]= v;
v= ff_mpeg4_default_non_intra_matrix[i];
s->inter_matrix[j]= v;
s->chroma_inter_matrix[j]= v;
}
/* load custom intra matrix */
if(get_bits1(gb)){
int last=0;
for(i=0; i<64; i++){
int j;
v= get_bits(gb, 8);
if(v==0) break;
last= v;
j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
s->intra_matrix[j]= v;
s->chroma_intra_matrix[j]= v;
}
/* replicate last value */
for(; i<64; i++){
int j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
s->intra_matrix[j]= last;
s->chroma_intra_matrix[j]= last;
}
}
/* load custom non intra matrix */
if(get_bits1(gb)){
int last=0;
for(i=0; i<64; i++){
int j;
v= get_bits(gb, 8);
if(v==0) break;
last= v;
j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
s->inter_matrix[j]= v;
s->chroma_inter_matrix[j]= v;
}
/* replicate last value */
for(; i<64; i++){
int j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
s->inter_matrix[j]= last;
s->chroma_inter_matrix[j]= last;
}
}
// FIXME a bunch of grayscale shape things
}
if(vo_ver_id != 1)
s->quarter_sample= get_bits1(gb);
else s->quarter_sample=0;
if(!get_bits1(gb)){
int pos= get_bits_count(gb);
int estimation_method= get_bits(gb, 2);
if(estimation_method<2){
if(!get_bits1(gb)){
s->cplx_estimation_trash_i += 8*get_bits1(gb); //opaque
s->cplx_estimation_trash_i += 8*get_bits1(gb); //transparent
s->cplx_estimation_trash_i += 8*get_bits1(gb); //intra_cae
s->cplx_estimation_trash_i += 8*get_bits1(gb); //inter_cae
s->cplx_estimation_trash_i += 8*get_bits1(gb); //no_update
s->cplx_estimation_trash_i += 8*get_bits1(gb); //upampling
}
if(!get_bits1(gb)){
s->cplx_estimation_trash_i += 8*get_bits1(gb); //intra_blocks
s->cplx_estimation_trash_p += 8*get_bits1(gb); //inter_blocks
s->cplx_estimation_trash_p += 8*get_bits1(gb); //inter4v_blocks
s->cplx_estimation_trash_i += 8*get_bits1(gb); //not coded blocks
}
if(!check_marker(gb, "in complexity estimation part 1")){
skip_bits_long(gb, pos - get_bits_count(gb));
goto no_cplx_est;
}
if(!get_bits1(gb)){
s->cplx_estimation_trash_i += 8*get_bits1(gb); //dct_coeffs
s->cplx_estimation_trash_i += 8*get_bits1(gb); //dct_lines
s->cplx_estimation_trash_i += 8*get_bits1(gb); //vlc_syms
s->cplx_estimation_trash_i += 4*get_bits1(gb); //vlc_bits
}
if(!get_bits1(gb)){
s->cplx_estimation_trash_p += 8*get_bits1(gb); //apm
s->cplx_estimation_trash_p += 8*get_bits1(gb); //npm
s->cplx_estimation_trash_b += 8*get_bits1(gb); //interpolate_mc_q
s->cplx_estimation_trash_p += 8*get_bits1(gb); //forwback_mc_q
s->cplx_estimation_trash_p += 8*get_bits1(gb); //halfpel2
s->cplx_estimation_trash_p += 8*get_bits1(gb); //halfpel4
}
if(!check_marker(gb, "in complexity estimation part 2")){
skip_bits_long(gb, pos - get_bits_count(gb));
goto no_cplx_est;
}
if(estimation_method==1){
s->cplx_estimation_trash_i += 8*get_bits1(gb); //sadct
s->cplx_estimation_trash_p += 8*get_bits1(gb); //qpel
}
}else
av_log(s->avctx, AV_LOG_ERROR, "Invalid Complexity estimation method %d\n", estimation_method);
}else{
no_cplx_est:
s->cplx_estimation_trash_i=
s->cplx_estimation_trash_p=
s->cplx_estimation_trash_b= 0;
}
s->resync_marker= !get_bits1(gb); /* resync_marker_disabled */
s->data_partitioning= get_bits1(gb);
if(s->data_partitioning){
s->rvlc= get_bits1(gb);
}
if(vo_ver_id != 1) {
s->new_pred= get_bits1(gb);
if(s->new_pred){
av_log(s->avctx, AV_LOG_ERROR, "new pred not supported\n");
skip_bits(gb, 2); /* requested upstream message type */
skip_bits1(gb); /* newpred segment type */
}
s->reduced_res_vop= get_bits1(gb);
if(s->reduced_res_vop) av_log(s->avctx, AV_LOG_ERROR, "reduced resolution VOP not supported\n");
}
else{
s->new_pred=0;
s->reduced_res_vop= 0;
}
s->scalability= get_bits1(gb);
if (s->scalability) {
GetBitContext bak= *gb;
int ref_layer_id;
int ref_layer_sampling_dir;
int h_sampling_factor_n;
int h_sampling_factor_m;
int v_sampling_factor_n;
int v_sampling_factor_m;
s->hierachy_type= get_bits1(gb);
ref_layer_id= get_bits(gb, 4);
ref_layer_sampling_dir= get_bits1(gb);
h_sampling_factor_n= get_bits(gb, 5);
h_sampling_factor_m= get_bits(gb, 5);
v_sampling_factor_n= get_bits(gb, 5);
v_sampling_factor_m= get_bits(gb, 5);
s->enhancement_type= get_bits1(gb);
if( h_sampling_factor_n==0 || h_sampling_factor_m==0
|| v_sampling_factor_n==0 || v_sampling_factor_m==0){
/* illegal scalability header (VERY broken encoder),
* trying to workaround */
s->scalability=0;
*gb= bak;
}else
av_log(s->avctx, AV_LOG_ERROR, "scalability not supported\n");
// bin shape stuff FIXME
}
}
return 0;
}
/**
* decodes the user data stuff in the header.
* Also initializes divx/xvid/lavc_version/build.
*/
static int decode_user_data(MpegEncContext *s, GetBitContext *gb){
char buf[256];
int i;
int e;
int ver = 0, build = 0, ver2 = 0, ver3 = 0;
char last;
for(i=0; i<255 && get_bits_count(gb) < gb->size_in_bits; i++){
if(show_bits(gb, 23) == 0) break;
buf[i]= get_bits(gb, 8);
}
buf[i]=0;
/* divx detection */
e=sscanf(buf, "DivX%dBuild%d%c", &ver, &build, &last);
if(e<2)
e=sscanf(buf, "DivX%db%d%c", &ver, &build, &last);
if(e>=2){
s->divx_version= ver;
s->divx_build= build;
s->divx_packed= e==3 && last=='p';
if(s->divx_packed && !s->showed_packed_warning) {
av_log(s->avctx, AV_LOG_WARNING, "Invalid and inefficient vfw-avi packed B frames detected\n");
s->showed_packed_warning=1;
}
}
/* ffmpeg detection */
e=sscanf(buf, "FFmpe%*[^b]b%d", &build)+3;
if(e!=4)
e=sscanf(buf, "FFmpeg v%d.%d.%d / libavcodec build: %d", &ver, &ver2, &ver3, &build);
if(e!=4){
e=sscanf(buf, "Lavc%d.%d.%d", &ver, &ver2, &ver3)+1;
if (e>1)
build= (ver<<16) + (ver2<<8) + ver3;
}
if(e!=4){
if(strcmp(buf, "ffmpeg")==0){
s->lavc_build= 4600;
}
}
if(e==4){
s->lavc_build= build;
}
/* Xvid detection */
e=sscanf(buf, "XviD%d", &build);
if(e==1){
s->xvid_build= build;
}
return 0;
}
static int decode_vop_header(MpegEncContext *s, GetBitContext *gb){
int time_incr, time_increment;
s->pict_type = get_bits(gb, 2) + FF_I_TYPE; /* pict type: I = 0 , P = 1 */
if(s->pict_type==FF_B_TYPE && s->low_delay && s->vol_control_parameters==0 && !(s->flags & CODEC_FLAG_LOW_DELAY)){
av_log(s->avctx, AV_LOG_ERROR, "low_delay flag incorrectly, clearing it\n");
s->low_delay=0;
}
s->partitioned_frame= s->data_partitioning && s->pict_type!=FF_B_TYPE;
if(s->partitioned_frame)
s->decode_mb= mpeg4_decode_partitioned_mb;
else
s->decode_mb= mpeg4_decode_mb;
time_incr=0;
while (get_bits1(gb) != 0)
time_incr++;
check_marker(gb, "before time_increment");
if(s->time_increment_bits==0 || !(show_bits(gb, s->time_increment_bits+1)&1)){
av_log(s->avctx, AV_LOG_ERROR, "hmm, seems the headers are not complete, trying to guess time_increment_bits\n");
for(s->time_increment_bits=1 ;s->time_increment_bits<16; s->time_increment_bits++){
if ( s->pict_type == FF_P_TYPE
|| (s->pict_type == FF_S_TYPE && s->vol_sprite_usage==GMC_SPRITE)) {
if((show_bits(gb, s->time_increment_bits+6)&0x37) == 0x30) break;
}else
if((show_bits(gb, s->time_increment_bits+5)&0x1F) == 0x18) break;
}
av_log(s->avctx, AV_LOG_ERROR, "my guess is %d bits ;)\n",s->time_increment_bits);
}
if(IS_3IV1) time_increment= get_bits1(gb); //FIXME investigate further
else time_increment= get_bits(gb, s->time_increment_bits);
if(s->pict_type!=FF_B_TYPE){
s->last_time_base= s->time_base;
s->time_base+= time_incr;
s->time= s->time_base*s->avctx->time_base.den + time_increment;
if(s->workaround_bugs&FF_BUG_UMP4){
if(s->time < s->last_non_b_time){
/* header is not mpeg-4-compatible, broken encoder,
* trying to workaround */
s->time_base++;
s->time+= s->avctx->time_base.den;
}
}
s->pp_time= s->time - s->last_non_b_time;
s->last_non_b_time= s->time;
}else{
s->time= (s->last_time_base + time_incr)*s->avctx->time_base.den + time_increment;
s->pb_time= s->pp_time - (s->last_non_b_time - s->time);
if(s->pp_time <=s->pb_time || s->pp_time <= s->pp_time - s->pb_time || s->pp_time<=0){
/* messed up order, maybe after seeking? skipping current b-frame */
return FRAME_SKIPPED;
}
ff_mpeg4_init_direct_mv(s);
if(s->t_frame==0) s->t_frame= s->pb_time;
if(s->t_frame==0) s->t_frame=1; // 1/0 protection
s->pp_field_time= ( ROUNDED_DIV(s->last_non_b_time, s->t_frame)
- ROUNDED_DIV(s->last_non_b_time - s->pp_time, s->t_frame))*2;
s->pb_field_time= ( ROUNDED_DIV(s->time, s->t_frame)
- ROUNDED_DIV(s->last_non_b_time - s->pp_time, s->t_frame))*2;
if(!s->progressive_sequence){
if(s->pp_field_time <= s->pb_field_time || s->pb_field_time <= 1)
return FRAME_SKIPPED;
}
}
if(s->avctx->time_base.num)
s->current_picture_ptr->pts= (s->time + s->avctx->time_base.num/2) / s->avctx->time_base.num;
else
s->current_picture_ptr->pts= AV_NOPTS_VALUE;
if(s->avctx->debug&FF_DEBUG_PTS)
av_log(s->avctx, AV_LOG_DEBUG, "MPEG4 PTS: %"PRId64"\n", s->current_picture_ptr->pts);
check_marker(gb, "before vop_coded");
/* vop coded */
if (get_bits1(gb) != 1){
if(s->avctx->debug&FF_DEBUG_PICT_INFO)
av_log(s->avctx, AV_LOG_ERROR, "vop not coded\n");
return FRAME_SKIPPED;
}
if (s->shape != BIN_ONLY_SHAPE && ( s->pict_type == FF_P_TYPE
|| (s->pict_type == FF_S_TYPE && s->vol_sprite_usage==GMC_SPRITE))) {
/* rounding type for motion estimation */
s->no_rounding = get_bits1(gb);
} else {
s->no_rounding = 0;
}
//FIXME reduced res stuff
if (s->shape != RECT_SHAPE) {
if (s->vol_sprite_usage != 1 || s->pict_type != FF_I_TYPE) {
int width, height, hor_spat_ref, ver_spat_ref;
width = get_bits(gb, 13);
skip_bits1(gb); /* marker */
height = get_bits(gb, 13);
skip_bits1(gb); /* marker */
hor_spat_ref = get_bits(gb, 13); /* hor_spat_ref */
skip_bits1(gb); /* marker */
ver_spat_ref = get_bits(gb, 13); /* ver_spat_ref */
}
skip_bits1(gb); /* change_CR_disable */
if (get_bits1(gb) != 0) {
skip_bits(gb, 8); /* constant_alpha_value */
}
}
//FIXME complexity estimation stuff
if (s->shape != BIN_ONLY_SHAPE) {
skip_bits_long(gb, s->cplx_estimation_trash_i);
if(s->pict_type != FF_I_TYPE)
skip_bits_long(gb, s->cplx_estimation_trash_p);
if(s->pict_type == FF_B_TYPE)
skip_bits_long(gb, s->cplx_estimation_trash_b);
s->intra_dc_threshold= mpeg4_dc_threshold[ get_bits(gb, 3) ];
if(!s->progressive_sequence){
s->top_field_first= get_bits1(gb);
s->alternate_scan= get_bits1(gb);
}else
s->alternate_scan= 0;
}
if(s->alternate_scan){
ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_vertical_scan);
ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
} else{
ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
}
if(s->pict_type == FF_S_TYPE && (s->vol_sprite_usage==STATIC_SPRITE || s->vol_sprite_usage==GMC_SPRITE)){
mpeg4_decode_sprite_trajectory(s, gb);
if(s->sprite_brightness_change) av_log(s->avctx, AV_LOG_ERROR, "sprite_brightness_change not supported\n");
if(s->vol_sprite_usage==STATIC_SPRITE) av_log(s->avctx, AV_LOG_ERROR, "static sprite not supported\n");
}
if (s->shape != BIN_ONLY_SHAPE) {
s->chroma_qscale= s->qscale = get_bits(gb, s->quant_precision);
if(s->qscale==0){
av_log(s->avctx, AV_LOG_ERROR, "Error, header damaged or not MPEG4 header (qscale=0)\n");
return -1; // makes no sense to continue, as there is nothing left from the image then
}
if (s->pict_type != FF_I_TYPE) {
s->f_code = get_bits(gb, 3); /* fcode_for */
if(s->f_code==0){
av_log(s->avctx, AV_LOG_ERROR, "Error, header damaged or not MPEG4 header (f_code=0)\n");
return -1; // makes no sense to continue, as the MV decoding will break very quickly
}
}else
s->f_code=1;
if (s->pict_type == FF_B_TYPE) {
s->b_code = get_bits(gb, 3);
}else
s->b_code=1;
if(s->avctx->debug&FF_DEBUG_PICT_INFO){
av_log(s->avctx, AV_LOG_DEBUG, "qp:%d fc:%d,%d %s size:%d pro:%d alt:%d top:%d %spel part:%d resync:%d w:%d a:%d rnd:%d vot:%d%s dc:%d ce:%d/%d/%d\n",
s->qscale, s->f_code, s->b_code,
s->pict_type == FF_I_TYPE ? "I" : (s->pict_type == FF_P_TYPE ? "P" : (s->pict_type == FF_B_TYPE ? "B" : "S")),
gb->size_in_bits,s->progressive_sequence, s->alternate_scan, s->top_field_first,
s->quarter_sample ? "q" : "h", s->data_partitioning, s->resync_marker, s->num_sprite_warping_points,
s->sprite_warping_accuracy, 1-s->no_rounding, s->vo_type, s->vol_control_parameters ? " VOLC" : " ", s->intra_dc_threshold, s->cplx_estimation_trash_i, s->cplx_estimation_trash_p, s->cplx_estimation_trash_b);
}
if(!s->scalability){
if (s->shape!=RECT_SHAPE && s->pict_type!=FF_I_TYPE) {
skip_bits1(gb); // vop shape coding type
}
}else{
if(s->enhancement_type){
int load_backward_shape= get_bits1(gb);
if(load_backward_shape){
av_log(s->avctx, AV_LOG_ERROR, "load backward shape isn't supported\n");
}
}
skip_bits(gb, 2); //ref_select_code
}
}
/* detect buggy encoders which don't set the low_delay flag (divx4/xvid/opendivx)*/
// note we cannot detect divx5 without b-frames easily (although it's buggy too)
if(s->vo_type==0 && s->vol_control_parameters==0 && s->divx_version==-1 && s->picture_number==0){
av_log(s->avctx, AV_LOG_ERROR, "looks like this file was encoded with (divx4/(old)xvid/opendivx) -> forcing low_delay flag\n");
s->low_delay=1;
}
s->picture_number++; // better than pic number==0 always ;)
s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table; //FIXME add short header support
s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table;
if(s->workaround_bugs&FF_BUG_EDGE){
s->h_edge_pos= s->width;
s->v_edge_pos= s->height;
}
return 0;
}
/**
* decode mpeg4 headers
* @return <0 if no VOP found (or a damaged one)
* FRAME_SKIPPED if a not coded VOP is found
* 0 if a VOP is found
*/
int ff_mpeg4_decode_picture_header(MpegEncContext * s, GetBitContext *gb)
{
int startcode, v;
/* search next start code */
align_get_bits(gb);
if(s->codec_tag == AV_RL32("WV1F") && show_bits(gb, 24) == 0x575630){
skip_bits(gb, 24);
if(get_bits(gb, 8) == 0xF0)
goto end;
}
startcode = 0xff;
for(;;) {
if(get_bits_count(gb) >= gb->size_in_bits){
if(gb->size_in_bits==8 && (s->divx_version>=0 || s->xvid_build>=0)){
av_log(s->avctx, AV_LOG_ERROR, "frame skip %d\n", gb->size_in_bits);
return FRAME_SKIPPED; //divx bug
}else
return -1; //end of stream
}
/* use the bits after the test */
v = get_bits(gb, 8);
startcode = ((startcode << 8) | v) & 0xffffffff;
if((startcode&0xFFFFFF00) != 0x100)
continue; //no startcode
if(s->avctx->debug&FF_DEBUG_STARTCODE){
av_log(s->avctx, AV_LOG_DEBUG, "startcode: %3X ", startcode);
if (startcode<=0x11F) av_log(s->avctx, AV_LOG_DEBUG, "Video Object Start");
else if(startcode<=0x12F) av_log(s->avctx, AV_LOG_DEBUG, "Video Object Layer Start");
else if(startcode<=0x13F) av_log(s->avctx, AV_LOG_DEBUG, "Reserved");
else if(startcode<=0x15F) av_log(s->avctx, AV_LOG_DEBUG, "FGS bp start");
else if(startcode<=0x1AF) av_log(s->avctx, AV_LOG_DEBUG, "Reserved");
else if(startcode==0x1B0) av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Seq Start");
else if(startcode==0x1B1) av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Seq End");
else if(startcode==0x1B2) av_log(s->avctx, AV_LOG_DEBUG, "User Data");
else if(startcode==0x1B3) av_log(s->avctx, AV_LOG_DEBUG, "Group of VOP start");
else if(startcode==0x1B4) av_log(s->avctx, AV_LOG_DEBUG, "Video Session Error");
else if(startcode==0x1B5) av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Start");
else if(startcode==0x1B6) av_log(s->avctx, AV_LOG_DEBUG, "Video Object Plane start");
else if(startcode==0x1B7) av_log(s->avctx, AV_LOG_DEBUG, "slice start");
else if(startcode==0x1B8) av_log(s->avctx, AV_LOG_DEBUG, "extension start");
else if(startcode==0x1B9) av_log(s->avctx, AV_LOG_DEBUG, "fgs start");
else if(startcode==0x1BA) av_log(s->avctx, AV_LOG_DEBUG, "FBA Object start");
else if(startcode==0x1BB) av_log(s->avctx, AV_LOG_DEBUG, "FBA Object Plane start");
else if(startcode==0x1BC) av_log(s->avctx, AV_LOG_DEBUG, "Mesh Object start");
else if(startcode==0x1BD) av_log(s->avctx, AV_LOG_DEBUG, "Mesh Object Plane start");
else if(startcode==0x1BE) av_log(s->avctx, AV_LOG_DEBUG, "Still Texture Object start");
else if(startcode==0x1BF) av_log(s->avctx, AV_LOG_DEBUG, "Texture Spatial Layer start");
else if(startcode==0x1C0) av_log(s->avctx, AV_LOG_DEBUG, "Texture SNR Layer start");
else if(startcode==0x1C1) av_log(s->avctx, AV_LOG_DEBUG, "Texture Tile start");
else if(startcode==0x1C2) av_log(s->avctx, AV_LOG_DEBUG, "Texture Shape Layer start");
else if(startcode==0x1C3) av_log(s->avctx, AV_LOG_DEBUG, "stuffing start");
else if(startcode<=0x1C5) av_log(s->avctx, AV_LOG_DEBUG, "reserved");
else if(startcode<=0x1FF) av_log(s->avctx, AV_LOG_DEBUG, "System start");
av_log(s->avctx, AV_LOG_DEBUG, " at %d\n", get_bits_count(gb));
}
if(startcode >= 0x120 && startcode <= 0x12F){
if(decode_vol_header(s, gb) < 0)
return -1;
}
else if(startcode == USER_DATA_STARTCODE){
decode_user_data(s, gb);
}
else if(startcode == GOP_STARTCODE){
mpeg4_decode_gop_header(s, gb);
}
else if(startcode == VOP_STARTCODE){
break;
}
align_get_bits(gb);
startcode = 0xff;
}
end:
if(s->flags& CODEC_FLAG_LOW_DELAY)
s->low_delay=1;
s->avctx->has_b_frames= !s->low_delay;
return decode_vop_header(s, gb);
}
static av_cold int decode_init(AVCodecContext *avctx)
{
MpegEncContext *s = avctx->priv_data;
int ret;
static int done = 0;
s->divx_version=
s->divx_build=
s->xvid_build=
s->lavc_build= -1;
if((ret=ff_h263_decode_init(avctx)) < 0)
return ret;
if (!done) {
done = 1;
init_rl(&ff_mpeg4_rl_intra, ff_mpeg4_static_rl_table_store[0]);
init_rl(&rvlc_rl_inter, ff_mpeg4_static_rl_table_store[1]);
init_rl(&rvlc_rl_intra, ff_mpeg4_static_rl_table_store[2]);
INIT_VLC_RL(ff_mpeg4_rl_intra, 554);
INIT_VLC_RL(rvlc_rl_inter, 1072);
INIT_VLC_RL(rvlc_rl_intra, 1072);
INIT_VLC_STATIC(&dc_lum, DC_VLC_BITS, 10 /* 13 */,
&ff_mpeg4_DCtab_lum[0][1], 2, 1,
&ff_mpeg4_DCtab_lum[0][0], 2, 1, 512);
INIT_VLC_STATIC(&dc_chrom, DC_VLC_BITS, 10 /* 13 */,
&ff_mpeg4_DCtab_chrom[0][1], 2, 1,
&ff_mpeg4_DCtab_chrom[0][0], 2, 1, 512);
INIT_VLC_STATIC(&sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 15,
&sprite_trajectory_tab[0][1], 4, 2,
&sprite_trajectory_tab[0][0], 4, 2, 128);
INIT_VLC_STATIC(&mb_type_b_vlc, MB_TYPE_B_VLC_BITS, 4,
&mb_type_b_tab[0][1], 2, 1,
&mb_type_b_tab[0][0], 2, 1, 16);
}
s->h263_pred = 1;
s->low_delay = 0; //default, might be overriden in the vol header during header parsing
s->decode_mb= mpeg4_decode_mb;
s->time_increment_bits = 4; /* default value for broken headers */
avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
return 0;
}
AVCodec mpeg4_decoder = {
"mpeg4",
AVMEDIA_TYPE_VIDEO,
CODEC_ID_MPEG4,
sizeof(MpegEncContext),
decode_init,
NULL,
ff_h263_decode_end,
ff_h263_decode_frame,
CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
.flush= ff_mpeg_flush,
.long_name= NULL_IF_CONFIG_SMALL("MPEG-4 part 2"),
.pix_fmts= ff_hwaccel_pixfmt_list_420,
};
#if CONFIG_MPEG4_VDPAU_DECODER
AVCodec mpeg4_vdpau_decoder = {
"mpeg4_vdpau",
AVMEDIA_TYPE_VIDEO,
CODEC_ID_MPEG4,
sizeof(MpegEncContext),
decode_init,
NULL,
ff_h263_decode_end,
ff_h263_decode_frame,
CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
.long_name= NULL_IF_CONFIG_SMALL("MPEG-4 part 2 (VDPAU)"),
.pix_fmts= (const enum PixelFormat[]){PIX_FMT_VDPAU_MPEG4, PIX_FMT_NONE},
};
#endif
| 123linslouis-android-video-cutter | jni/libavcodec/mpeg4videodec.c | C | asf20 | 86,641 |
/*
* H.26L/H.264/AVC/JVT/14496-10/... parser
* Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* H.264 / AVC / MPEG4 part10 parser.
* @author Michael Niedermayer <michaelni@gmx.at>
*/
#ifndef AVCODEC_H264_PARSER_H
#define AVCODEC_H264_PARSER_H
#include "h264.h"
/**
* finds the end of the current frame in the bitstream.
* @return the position of the first byte of the next frame, or -1
*/
int ff_h264_find_frame_end(H264Context *h, const uint8_t *buf, int buf_size);
#endif /* AVCODEC_H264_PARSER_H */
| 123linslouis-android-video-cutter | jni/libavcodec/h264_parser.h | C | asf20 | 1,302 |
/*
* PNG image format
* Copyright (c) 2003 Fabrice Bellard
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "avcodec.h"
#include "bytestream.h"
#include "png.h"
#include "dsputil.h"
/* TODO:
* - add 2, 4 and 16 bit depth support
*/
#include <zlib.h>
//#define DEBUG
typedef struct PNGDecContext {
DSPContext dsp;
const uint8_t *bytestream;
const uint8_t *bytestream_start;
const uint8_t *bytestream_end;
AVFrame picture1, picture2;
AVFrame *current_picture, *last_picture;
int state;
int width, height;
int bit_depth;
int color_type;
int compression_type;
int interlace_type;
int filter_type;
int channels;
int bits_per_pixel;
int bpp;
uint8_t *image_buf;
int image_linesize;
uint32_t palette[256];
uint8_t *crow_buf;
uint8_t *last_row;
uint8_t *tmp_row;
int pass;
int crow_size; /* compressed row size (include filter type) */
int row_size; /* decompressed row size */
int pass_row_size; /* decompress row size of the current pass */
int y;
z_stream zstream;
} PNGDecContext;
/* Mask to determine which y pixels can be written in a pass */
static const uint8_t png_pass_dsp_ymask[NB_PASSES] = {
0xff, 0xff, 0x0f, 0xcc, 0x33, 0xff, 0x55,
};
/* Mask to determine which pixels to overwrite while displaying */
static const uint8_t png_pass_dsp_mask[NB_PASSES] = {
0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
};
/* NOTE: we try to construct a good looking image at each pass. width
is the original image width. We also do pixel format conversion at
this stage */
static void png_put_interlaced_row(uint8_t *dst, int width,
int bits_per_pixel, int pass,
int color_type, const uint8_t *src)
{
int x, mask, dsp_mask, j, src_x, b, bpp;
uint8_t *d;
const uint8_t *s;
mask = ff_png_pass_mask[pass];
dsp_mask = png_pass_dsp_mask[pass];
switch(bits_per_pixel) {
case 1:
/* we must initialize the line to zero before writing to it */
if (pass == 0)
memset(dst, 0, (width + 7) >> 3);
src_x = 0;
for(x = 0; x < width; x++) {
j = (x & 7);
if ((dsp_mask << j) & 0x80) {
b = (src[src_x >> 3] >> (7 - (src_x & 7))) & 1;
dst[x >> 3] |= b << (7 - j);
}
if ((mask << j) & 0x80)
src_x++;
}
break;
default:
bpp = bits_per_pixel >> 3;
d = dst;
s = src;
if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
for(x = 0; x < width; x++) {
j = x & 7;
if ((dsp_mask << j) & 0x80) {
*(uint32_t *)d = (s[3] << 24) | (s[0] << 16) | (s[1] << 8) | s[2];
}
d += bpp;
if ((mask << j) & 0x80)
s += bpp;
}
} else {
for(x = 0; x < width; x++) {
j = x & 7;
if ((dsp_mask << j) & 0x80) {
memcpy(d, s, bpp);
}
d += bpp;
if ((mask << j) & 0x80)
s += bpp;
}
}
break;
}
}
void ff_add_png_paeth_prediction(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)
{
int i;
for(i = 0; i < w; i++) {
int a, b, c, p, pa, pb, pc;
a = dst[i - bpp];
b = top[i];
c = top[i - bpp];
p = b - c;
pc = a - c;
pa = abs(p);
pb = abs(pc);
pc = abs(p + pc);
if (pa <= pb && pa <= pc)
p = a;
else if (pb <= pc)
p = b;
else
p = c;
dst[i] = p + src[i];
}
}
#define UNROLL1(bpp, op) {\
r = dst[0];\
if(bpp >= 2) g = dst[1];\
if(bpp >= 3) b = dst[2];\
if(bpp >= 4) a = dst[3];\
for(; i < size; i+=bpp) {\
dst[i+0] = r = op(r, src[i+0], last[i+0]);\
if(bpp == 1) continue;\
dst[i+1] = g = op(g, src[i+1], last[i+1]);\
if(bpp == 2) continue;\
dst[i+2] = b = op(b, src[i+2], last[i+2]);\
if(bpp == 3) continue;\
dst[i+3] = a = op(a, src[i+3], last[i+3]);\
}\
}
#define UNROLL_FILTER(op)\
if(bpp == 1) UNROLL1(1, op)\
else if(bpp == 2) UNROLL1(2, op)\
else if(bpp == 3) UNROLL1(3, op)\
else if(bpp == 4) UNROLL1(4, op)\
else {\
for (; i < size; i += bpp) {\
int j;\
for (j = 0; j < bpp; j++)\
dst[i+j] = op(dst[i+j-bpp], src[i+j], last[i+j]);\
}\
}
/* NOTE: 'dst' can be equal to 'last' */
static void png_filter_row(DSPContext *dsp, uint8_t *dst, int filter_type,
uint8_t *src, uint8_t *last, int size, int bpp)
{
int i, p, r, g, b, a;
switch(filter_type) {
case PNG_FILTER_VALUE_NONE:
memcpy(dst, src, size);
break;
case PNG_FILTER_VALUE_SUB:
for(i = 0; i < bpp; i++) {
dst[i] = src[i];
}
if(bpp == 4) {
p = *(int*)dst;
for(; i < size; i+=bpp) {
int s = *(int*)(src+i);
p = ((s&0x7f7f7f7f) + (p&0x7f7f7f7f)) ^ ((s^p)&0x80808080);
*(int*)(dst+i) = p;
}
} else {
#define OP_SUB(x,s,l) x+s
UNROLL_FILTER(OP_SUB);
}
break;
case PNG_FILTER_VALUE_UP:
dsp->add_bytes_l2(dst, src, last, size);
break;
case PNG_FILTER_VALUE_AVG:
for(i = 0; i < bpp; i++) {
p = (last[i] >> 1);
dst[i] = p + src[i];
}
#define OP_AVG(x,s,l) (((x + l) >> 1) + s) & 0xff
UNROLL_FILTER(OP_AVG);
break;
case PNG_FILTER_VALUE_PAETH:
for(i = 0; i < bpp; i++) {
p = last[i];
dst[i] = p + src[i];
}
if(bpp > 1 && size > 4) {
// would write off the end of the array if we let it process the last pixel with bpp=3
int w = bpp==4 ? size : size-3;
dsp->add_png_paeth_prediction(dst+i, src+i, last+i, w-i, bpp);
i = w;
}
ff_add_png_paeth_prediction(dst+i, src+i, last+i, size-i, bpp);
break;
}
}
static av_always_inline void convert_to_rgb32_loco(uint8_t *dst, const uint8_t *src, int width, int loco)
{
int j;
unsigned int r, g, b, a;
for(j = 0;j < width; j++) {
r = src[0];
g = src[1];
b = src[2];
a = src[3];
if(loco) {
r = (r+g)&0xff;
b = (b+g)&0xff;
}
*(uint32_t *)dst = (a << 24) | (r << 16) | (g << 8) | b;
dst += 4;
src += 4;
}
}
static void convert_to_rgb32(uint8_t *dst, const uint8_t *src, int width, int loco)
{
if(loco)
convert_to_rgb32_loco(dst, src, width, 1);
else
convert_to_rgb32_loco(dst, src, width, 0);
}
static void deloco_rgb24(uint8_t *dst, int size)
{
int i;
for(i=0; i<size; i+=3) {
int g = dst[i+1];
dst[i+0] += g;
dst[i+2] += g;
}
}
/* process exactly one decompressed row */
static void png_handle_row(PNGDecContext *s)
{
uint8_t *ptr, *last_row;
int got_line;
if (!s->interlace_type) {
ptr = s->image_buf + s->image_linesize * s->y;
/* need to swap bytes correctly for RGB_ALPHA */
if (s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
s->last_row, s->row_size, s->bpp);
convert_to_rgb32(ptr, s->tmp_row, s->width, s->filter_type == PNG_FILTER_TYPE_LOCO);
FFSWAP(uint8_t*, s->last_row, s->tmp_row);
} else {
/* in normal case, we avoid one copy */
if (s->y == 0)
last_row = s->last_row;
else
last_row = ptr - s->image_linesize;
png_filter_row(&s->dsp, ptr, s->crow_buf[0], s->crow_buf + 1,
last_row, s->row_size, s->bpp);
}
/* loco lags by 1 row so that it doesn't interfere with top prediction */
if (s->filter_type == PNG_FILTER_TYPE_LOCO &&
s->color_type == PNG_COLOR_TYPE_RGB && s->y > 0)
deloco_rgb24(ptr - s->image_linesize, s->row_size);
s->y++;
if (s->y == s->height) {
s->state |= PNG_ALLIMAGE;
if (s->filter_type == PNG_FILTER_TYPE_LOCO &&
s->color_type == PNG_COLOR_TYPE_RGB)
deloco_rgb24(ptr, s->row_size);
}
} else {
got_line = 0;
for(;;) {
ptr = s->image_buf + s->image_linesize * s->y;
if ((ff_png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
/* if we already read one row, it is time to stop to
wait for the next one */
if (got_line)
break;
png_filter_row(&s->dsp, s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
s->last_row, s->pass_row_size, s->bpp);
FFSWAP(uint8_t*, s->last_row, s->tmp_row);
got_line = 1;
}
if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
/* NOTE: RGB32 is handled directly in png_put_interlaced_row */
png_put_interlaced_row(ptr, s->width, s->bits_per_pixel, s->pass,
s->color_type, s->last_row);
}
s->y++;
if (s->y == s->height) {
for(;;) {
if (s->pass == NB_PASSES - 1) {
s->state |= PNG_ALLIMAGE;
goto the_end;
} else {
s->pass++;
s->y = 0;
s->pass_row_size = ff_png_pass_row_size(s->pass,
s->bits_per_pixel,
s->width);
s->crow_size = s->pass_row_size + 1;
if (s->pass_row_size != 0)
break;
/* skip pass if empty row */
}
}
}
}
the_end: ;
}
}
static int png_decode_idat(PNGDecContext *s, int length)
{
int ret;
s->zstream.avail_in = length;
s->zstream.next_in = s->bytestream;
s->bytestream += length;
if(s->bytestream > s->bytestream_end)
return -1;
/* decode one line if possible */
while (s->zstream.avail_in > 0) {
ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
if (ret != Z_OK && ret != Z_STREAM_END) {
return -1;
}
if (s->zstream.avail_out == 0) {
if (!(s->state & PNG_ALLIMAGE)) {
png_handle_row(s);
}
s->zstream.avail_out = s->crow_size;
s->zstream.next_out = s->crow_buf;
}
}
return 0;
}
static int decode_frame(AVCodecContext *avctx,
void *data, int *data_size,
AVPacket *avpkt)
{
const uint8_t *buf = avpkt->data;
int buf_size = avpkt->size;
PNGDecContext * const s = avctx->priv_data;
AVFrame *picture = data;
AVFrame *p;
uint8_t *crow_buf_base = NULL;
uint32_t tag, length;
int ret, crc;
FFSWAP(AVFrame *, s->current_picture, s->last_picture);
avctx->coded_frame= s->current_picture;
p = s->current_picture;
s->bytestream_start=
s->bytestream= buf;
s->bytestream_end= buf + buf_size;
/* check signature */
if (memcmp(s->bytestream, ff_pngsig, 8) != 0 &&
memcmp(s->bytestream, ff_mngsig, 8) != 0)
return -1;
s->bytestream+= 8;
s->y=
s->state=0;
// memset(s, 0, sizeof(PNGDecContext));
/* init the zlib */
s->zstream.zalloc = ff_png_zalloc;
s->zstream.zfree = ff_png_zfree;
s->zstream.opaque = NULL;
ret = inflateInit(&s->zstream);
if (ret != Z_OK)
return -1;
for(;;) {
int tag32;
if (s->bytestream >= s->bytestream_end)
goto fail;
length = bytestream_get_be32(&s->bytestream);
if (length > 0x7fffffff)
goto fail;
tag32 = bytestream_get_be32(&s->bytestream);
tag = bswap_32(tag32);
dprintf(avctx, "png: tag=%c%c%c%c length=%u\n",
(tag & 0xff),
((tag >> 8) & 0xff),
((tag >> 16) & 0xff),
((tag >> 24) & 0xff), length);
switch(tag) {
case MKTAG('I', 'H', 'D', 'R'):
if (length != 13)
goto fail;
s->width = bytestream_get_be32(&s->bytestream);
s->height = bytestream_get_be32(&s->bytestream);
if(avcodec_check_dimensions(avctx, s->width, s->height)){
s->width= s->height= 0;
goto fail;
}
s->bit_depth = *s->bytestream++;
s->color_type = *s->bytestream++;
s->compression_type = *s->bytestream++;
s->filter_type = *s->bytestream++;
s->interlace_type = *s->bytestream++;
crc = bytestream_get_be32(&s->bytestream);
s->state |= PNG_IHDR;
dprintf(avctx, "width=%d height=%d depth=%d color_type=%d compression_type=%d filter_type=%d interlace_type=%d\n",
s->width, s->height, s->bit_depth, s->color_type,
s->compression_type, s->filter_type, s->interlace_type);
break;
case MKTAG('I', 'D', 'A', 'T'):
if (!(s->state & PNG_IHDR))
goto fail;
if (!(s->state & PNG_IDAT)) {
/* init image info */
avctx->width = s->width;
avctx->height = s->height;
s->channels = ff_png_get_nb_channels(s->color_type);
s->bits_per_pixel = s->bit_depth * s->channels;
s->bpp = (s->bits_per_pixel + 7) >> 3;
s->row_size = (avctx->width * s->bits_per_pixel + 7) >> 3;
if (s->bit_depth == 8 &&
s->color_type == PNG_COLOR_TYPE_RGB) {
avctx->pix_fmt = PIX_FMT_RGB24;
} else if (s->bit_depth == 8 &&
s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
avctx->pix_fmt = PIX_FMT_RGB32;
} else if (s->bit_depth == 8 &&
s->color_type == PNG_COLOR_TYPE_GRAY) {
avctx->pix_fmt = PIX_FMT_GRAY8;
} else if (s->bit_depth == 16 &&
s->color_type == PNG_COLOR_TYPE_GRAY) {
avctx->pix_fmt = PIX_FMT_GRAY16BE;
} else if (s->bit_depth == 16 &&
s->color_type == PNG_COLOR_TYPE_RGB) {
avctx->pix_fmt = PIX_FMT_RGB48BE;
} else if (s->bit_depth == 1 &&
s->color_type == PNG_COLOR_TYPE_GRAY) {
avctx->pix_fmt = PIX_FMT_MONOBLACK;
} else if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
avctx->pix_fmt = PIX_FMT_PAL8;
} else if (s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
avctx->pix_fmt = PIX_FMT_Y400A;
} else {
goto fail;
}
if(p->data[0])
avctx->release_buffer(avctx, p);
p->reference= 0;
if(avctx->get_buffer(avctx, p) < 0){
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
goto fail;
}
p->pict_type= FF_I_TYPE;
p->key_frame= 1;
p->interlaced_frame = !!s->interlace_type;
/* compute the compressed row size */
if (!s->interlace_type) {
s->crow_size = s->row_size + 1;
} else {
s->pass = 0;
s->pass_row_size = ff_png_pass_row_size(s->pass,
s->bits_per_pixel,
s->width);
s->crow_size = s->pass_row_size + 1;
}
dprintf(avctx, "row_size=%d crow_size =%d\n",
s->row_size, s->crow_size);
s->image_buf = p->data[0];
s->image_linesize = p->linesize[0];
/* copy the palette if needed */
if (s->color_type == PNG_COLOR_TYPE_PALETTE)
memcpy(p->data[1], s->palette, 256 * sizeof(uint32_t));
/* empty row is used if differencing to the first row */
s->last_row = av_mallocz(s->row_size);
if (!s->last_row)
goto fail;
if (s->interlace_type ||
s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
s->tmp_row = av_malloc(s->row_size);
if (!s->tmp_row)
goto fail;
}
/* compressed row */
crow_buf_base = av_malloc(s->row_size + 16);
if (!crow_buf_base)
goto fail;
/* we want crow_buf+1 to be 16-byte aligned */
s->crow_buf = crow_buf_base + 15;
s->zstream.avail_out = s->crow_size;
s->zstream.next_out = s->crow_buf;
}
s->state |= PNG_IDAT;
if (png_decode_idat(s, length) < 0)
goto fail;
/* skip crc */
crc = bytestream_get_be32(&s->bytestream);
break;
case MKTAG('P', 'L', 'T', 'E'):
{
int n, i, r, g, b;
if ((length % 3) != 0 || length > 256 * 3)
goto skip_tag;
/* read the palette */
n = length / 3;
for(i=0;i<n;i++) {
r = *s->bytestream++;
g = *s->bytestream++;
b = *s->bytestream++;
s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
}
for(;i<256;i++) {
s->palette[i] = (0xff << 24);
}
s->state |= PNG_PLTE;
crc = bytestream_get_be32(&s->bytestream);
}
break;
case MKTAG('t', 'R', 'N', 'S'):
{
int v, i;
/* read the transparency. XXX: Only palette mode supported */
if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
length > 256 ||
!(s->state & PNG_PLTE))
goto skip_tag;
for(i=0;i<length;i++) {
v = *s->bytestream++;
s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
}
crc = bytestream_get_be32(&s->bytestream);
}
break;
case MKTAG('I', 'E', 'N', 'D'):
if (!(s->state & PNG_ALLIMAGE))
goto fail;
crc = bytestream_get_be32(&s->bytestream);
goto exit_loop;
default:
/* skip tag */
skip_tag:
s->bytestream += length + 4;
break;
}
}
exit_loop:
/* handle p-frames only if a predecessor frame is available */
if(s->last_picture->data[0] != NULL) {
if(!(avpkt->flags & AV_PKT_FLAG_KEY)) {
int i, j;
uint8_t *pd = s->current_picture->data[0];
uint8_t *pd_last = s->last_picture->data[0];
for(j=0; j < s->height; j++) {
for(i=0; i < s->width * s->bpp; i++) {
pd[i] += pd_last[i];
}
pd += s->image_linesize;
pd_last += s->image_linesize;
}
}
}
*picture= *s->current_picture;
*data_size = sizeof(AVFrame);
ret = s->bytestream - s->bytestream_start;
the_end:
inflateEnd(&s->zstream);
av_free(crow_buf_base);
s->crow_buf = NULL;
av_freep(&s->last_row);
av_freep(&s->tmp_row);
return ret;
fail:
ret = -1;
goto the_end;
}
static av_cold int png_dec_init(AVCodecContext *avctx){
PNGDecContext *s = avctx->priv_data;
s->current_picture = &s->picture1;
s->last_picture = &s->picture2;
avcodec_get_frame_defaults(&s->picture1);
avcodec_get_frame_defaults(&s->picture2);
dsputil_init(&s->dsp, avctx);
return 0;
}
static av_cold int png_dec_end(AVCodecContext *avctx)
{
PNGDecContext *s = avctx->priv_data;
if (s->picture1.data[0])
avctx->release_buffer(avctx, &s->picture1);
if (s->picture2.data[0])
avctx->release_buffer(avctx, &s->picture2);
return 0;
}
AVCodec png_decoder = {
"png",
AVMEDIA_TYPE_VIDEO,
CODEC_ID_PNG,
sizeof(PNGDecContext),
png_dec_init,
NULL,
png_dec_end,
decode_frame,
CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
NULL,
.long_name = NULL_IF_CONFIG_SMALL("PNG image"),
};
| 123linslouis-android-video-cutter | jni/libavcodec/pngdec.c | C | asf20 | 22,082 |
/*
* SIPR decoder for the 16k mode
*
* Copyright (c) 2008 Vladimir Voroshilov
* Copyright (c) 2009 Vitor Sessak
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef AVCODEC_SIPR16KDATA_H
#define AVCODEC_SIPR16KDATA_H
static const float pred_16k[2] = {0.8, 0.6};
static const float qu[2] = { 0.12, 0.5};
static const float gain_cb_16k[32] = {
0.07499, 0.10593, 0.14125, 0.18836,
0.23714, 0.28184, 0.32734, 0.37584,
0.42170, 0.47315, 0.53088, 0.59566,
0.66834, 0.74989, 0.84140, 0.94406,
1.05925, 1.18850, 1.33352, 1.49624,
1.67880, 1.88365, 2.11349, 2.37137,
2.66073, 3.05492, 3.54813, 4.21697,
5.30884, 7.07946, 9.44061, 13.33521,
};
static const float gain_pitch_cb_16k[16] = {
0.00, 0.2, 0.40, 0.5, 0.60, 0.7, 0.75, 0.8,
0.85, 0.9, 0.95, 1.0, 1.05, 1.1, 1.15, 1.2,
};
static const float mean_lsf_16k[16] = {
0.131554, 0.246615, 0.435896, 0.644419,
0.827810, 1.017876, 1.198910, 1.379159,
1.562157, 1.736908, 1.940719, 2.131963,
2.347162, 2.521521, 2.717870, 2.847068
};
/**
* Hamming windowed sinc function, like in AMR
*/
static const float sinc_win[40] = {
0.874475, 0.755101, 0.455962, 0.118807, -0.114223, -0.176778,
-0.101923, 0.015553, 0.086555, 0.078193, 0.018660, -0.037513,
-0.052733, -0.027459, 0.009967, 0.030278, 0.024050, 0.003055,
-0.013862, -0.016162, -0.006725, 0.004212, 0.008634, 0.005721,
-0.000000, -0.003710, -0.003690, -0.001228, 0.001409, 0.002610,
};
static const float lsf_cb1_16k[128][3] = {
{-0.089990, -0.172485, -0.203391}, {-0.094710, -0.178687, -0.134483},
{-0.056398, -0.131952, -0.154500}, {-0.051362, -0.128138, -0.198549},
{-0.061700, -0.142830, -0.251623}, {-0.041512, -0.115637, -0.229420},
{-0.036544, -0.107512, -0.173125}, {-0.024158, -0.088450, -0.204144},
{-0.038690, -0.103368, -0.132674}, {-0.056954, -0.128472, -0.104669},
{-0.020963, -0.076785, -0.163199}, {-0.012952, -0.077249, -0.128385},
{-0.032787, -0.097044, -0.093967}, {-0.035214, -0.053838, -0.111940},
{-0.013850, -0.036926, -0.139328}, {-0.004956, -0.065092, -0.087709},
{-0.065354, -0.065595, -0.079064}, {-0.023627, -0.081457, -0.054195},
{-0.027335, -0.035244, -0.068034}, { 0.016555, -0.047075, -0.128286},
{ 0.021066, -0.037252, -0.092041}, { 0.014681, -0.043044, -0.057739},
{-0.008493, -0.008143, -0.102486}, {-0.002303, -0.061540, -0.022952},
{-0.006061, -0.014278, -0.033652}, {-0.005276, 0.011246, -0.062762},
{ 0.043411, -0.006303, -0.063730}, { 0.035885, -0.010052, -0.115290},
{ 0.030628, -0.031538, -0.017807}, { 0.022345, 0.028210, -0.032335},
{ 0.026535, 0.027536, -0.091150}, {-0.003365, -0.008077, 0.015687},
{-0.026013, 0.017493, -0.010355}, { 0.059069, 0.010634, -0.007530},
{ 0.044038, -0.019424, 0.030453}, {-0.036065, -0.034215, -0.007758},
{ 0.022486, 0.042543, 0.027870}, {-0.049985, -0.016085, 0.021768},
{-0.021715, 0.021168, 0.052076}, {-0.004243, -0.061228, 0.027640},
{-0.033950, -0.017287, 0.064656}, { 0.016151, 0.000727, 0.062757},
{-0.063456, -0.043152, 0.056707}, {-0.067715, 0.006126, 0.058178},
{-0.038931, 0.051673, 0.030636}, {-0.073017, -0.074716, 0.026387},
{-0.039893, -0.104629, 0.039616}, {-0.073179, -0.074601, 0.082069},
{-0.066154, -0.027180, 0.099439}, {-0.075167, -0.121149, 0.071938},
{-0.030382, -0.092582, 0.091067}, {-0.084519, -0.137542, 0.023626},
{-0.060956, -0.121259, -0.015264}, {-0.030069, -0.093823, -0.008692},
{-0.063564, -0.065225, -0.025820}, {-0.052074, -0.117595, -0.059689},
{-0.091652, -0.165173, -0.045573}, {-0.070167, -0.121342, 0.131707},
{-0.061024, -0.005833, -0.051035}, { 0.007837, -0.051816, 0.074575},
{-0.070643, -0.053927, 0.149498}, {-0.014358, -0.066681, 0.139708},
{-0.058186, 0.029576, 0.092923}, {-0.023371, 0.007058, 0.112484},
{-0.057969, 0.022786, 0.148420}, { 0.029439, -0.017673, 0.121423},
{-0.015811, 0.056785, 0.091594}, { 0.004347, 0.056680, 0.137848},
{-0.004464, 0.002342, 0.184013}, { 0.029660, 0.046870, 0.082654},
{ 0.059408, 0.001016, 0.086063}, { 0.055263, 0.027574, 0.155037},
{ 0.062166, 0.064323, 0.117371}, { 0.022967, 0.100050, 0.077227},
{ 0.041795, 0.096343, 0.170421}, { 0.053189, 0.122931, 0.118549},
{ 0.094247, 0.094448, 0.078395}, { 0.082407, 0.033408, 0.041085},
{ 0.096820, 0.115960, 0.149433}, { 0.067804, 0.121849, 0.025336},
{-0.008421, 0.104316, 0.032314}, { 0.031013, 0.073218, -0.004899},
{ 0.085079, 0.060323, -0.009687}, { 0.028174, 0.092766, -0.055590},
{ 0.070133, 0.039160, -0.061035}, {-0.039211, 0.072517, -0.028756},
{ 0.129686, 0.100233, -0.046998}, { 0.154189, 0.107616, 0.022791},
{-0.049331, 0.094184, 0.087984}, {-0.013179, 0.126552, 0.125099},
{-0.058716, 0.098524, 0.150886}, {-0.022753, 0.080011, 0.191127},
{ 0.013451, 0.164593, 0.153768}, { 0.074818, 0.181214, 0.108211},
{ 0.091323, 0.169249, 0.168460}, { 0.033885, 0.155516, 0.213745},
{-0.032128, 0.227238, 0.135815}, {-0.059176, 0.168980, 0.229110},
{ 0.033917, 0.229753, 0.222264}, { 0.082702, 0.116370, 0.224697},
{ 0.127737, 0.186658, 0.212783}, { 0.047528, 0.063920, 0.216856},
{-0.002446, 0.114174, 0.263289}, {-0.077783, 0.082523, 0.249697},
{ 0.010023, 0.024267, 0.256874}, { 0.053190, 0.111422, 0.310407},
{-0.078804, 0.004444, 0.224078}, {-0.055253, -0.059180, 0.217892},
{-0.065371, 0.008124, 0.333405}, {-0.076188, -0.098767, 0.286983},
{-0.071911, -0.115804, 0.198031}, {-0.062473, 0.183639, 0.370162},
{-0.042666, 0.255210, 0.262720}, { 0.011999, 0.217530, 0.318291},
{-0.042144, 0.322087, 0.326387}, { 0.090663, 0.205835, 0.294784},
{ 0.058218, 0.293649, 0.277927}, { 0.157506, 0.282870, 0.294610},
{ 0.118248, 0.261007, 0.148614}, { 0.065261, 0.332362, 0.411912},
{ 0.141269, 0.451850, 0.315726}, { 0.001706, 0.456301, 0.357590},
{-0.052947, 0.356559, 0.456944}, { 0.247707, 0.263837, 0.152591},
{ 0.306847, 0.417373, 0.258553}, { 0.166347, 0.149222, 0.118973},
{ 0.379709, 0.292172, 0.139875}, { 0.010171, -0.055170, -0.174523}
};
static const float lsf_cb2_16k[256][3] = {
{-0.213011, -0.293385, -0.330597}, {-0.212582, -0.240992, -0.338239},
{-0.223373, -0.306214, -0.277192}, {-0.231138, -0.287729, -0.229412},
{-0.238466, -0.228571, -0.260954}, {-0.140931, -0.247018, -0.258566},
{-0.136239, -0.249669, -0.350143}, {-0.149738, -0.192970, -0.281475},
{-0.167058, -0.261052, -0.196301}, {-0.177049, -0.201324, -0.207897},
{-0.116915, -0.200629, -0.212526}, {-0.162247, -0.143805, -0.245093},
{-0.082042, -0.191842, -0.266338}, {-0.098846, -0.208511, -0.320481},
{-0.113510, -0.152470, -0.222474}, {-0.066197, -0.179112, -0.207813},
{-0.129490, -0.169320, -0.155238}, {-0.078843, -0.190292, -0.155172},
{-0.087790, -0.147729, -0.169351}, {-0.141037, -0.127207, -0.177910},
{-0.126525, -0.223961, -0.153639}, {-0.101464, -0.189953, -0.114559},
{-0.102450, -0.106303, -0.151171}, {-0.103208, -0.144457, -0.105378},
{-0.170794, -0.140525, -0.136428}, {-0.168641, -0.203064, -0.135368},
{-0.138193, -0.116042, -0.111905}, {-0.145085, -0.168581, -0.092613},
{-0.126379, -0.220431, -0.091327}, {-0.212934, -0.184797, -0.101632},
{-0.193711, -0.140556, -0.078304}, {-0.173674, -0.197276, -0.060140},
{-0.197897, -0.241907, -0.091997}, {-0.156037, -0.258519, -0.111628},
{-0.241964, -0.191124, -0.063140}, {-0.261340, -0.240847, -0.103132},
{-0.221621, -0.242972, -0.041255}, {-0.224166, -0.232742, -0.161568},
{-0.203591, -0.294470, -0.126035}, {-0.209540, -0.303149, -0.053170},
{-0.253859, -0.295066, -0.156050}, {-0.278143, -0.331105, -0.085237},
{-0.300273, -0.198750, -0.094834}, {-0.260477, -0.169713, -0.132476},
{-0.211889, -0.172454, -0.164281}, {-0.228370, -0.122149, -0.124178},
{-0.254629, -0.135668, -0.081692}, {-0.263813, -0.154928, -0.213596},
{-0.308224, -0.106877, -0.084404}, {-0.242644, -0.082862, -0.085835},
{-0.252084, -0.064888, -0.146498}, {-0.198162, -0.105721, -0.188887},
{-0.189238, -0.088028, -0.109736}, {-0.197598, -0.099831, -0.044030},
{-0.269017, -0.105991, -0.021513}, {-0.231349, -0.058825, -0.041407},
{-0.225589, -0.027501, -0.087160}, {-0.160347, -0.058341, -0.079789},
{-0.158729, -0.108951, -0.067262}, {-0.170483, -0.053023, -0.017561},
{-0.175207, -0.013649, -0.049513}, {-0.156004, -0.108378, -0.004052},
{-0.219958, -0.082362, 0.014950}, {-0.217785, -0.012981, -0.009410},
{-0.123290, -0.040849, -0.040910}, {-0.119861, -0.095078, -0.060246},
{-0.117537, -0.065479, 0.002968}, {-0.103231, -0.113298, -0.023282},
{-0.136365, -0.149524, -0.051387}, {-0.119332, -0.164400, -0.009103},
{-0.104522, -0.060948, -0.083056}, {-0.071461, -0.070787, -0.037347},
{-0.081116, -0.149015, -0.056740}, {-0.069561, -0.108099, -0.069167},
{-0.055624, -0.117369, -0.025091}, {-0.091941, -0.190091, -0.060020},
{-0.072003, -0.168433, -0.006540}, {-0.033305, -0.154427, -0.054608},
{-0.062988, -0.127093, -0.108307}, {-0.056690, -0.170813, -0.102834},
{-0.018273, -0.127863, -0.094998}, {-0.056239, -0.123678, -0.146262},
{-0.023442, -0.154617, -0.137417}, {-0.051903, -0.078379, -0.093395},
{-0.014599, -0.104412, -0.135959}, {-0.051582, -0.081280, -0.140643},
{-0.092727, -0.091930, -0.107816}, {-0.024814, -0.140993, -0.183243},
{-0.064307, -0.113024, -0.194788}, {-0.000118, -0.098858, -0.195336},
{-0.028090, -0.048258, -0.164101}, {-0.093414, -0.055969, -0.172743},
{-0.114445, -0.104336, -0.215204}, {-0.048518, -0.132063, -0.242991},
{-0.159620, -0.060240, -0.178592}, {-0.135728, -0.067473, -0.131876},
{-0.078464, -0.038040, -0.125105}, {-0.011061, -0.064011, -0.102496},
{-0.033887, -0.026485, -0.109493}, {-0.129128, -0.014216, -0.111329},
{-0.190687, -0.030660, -0.135825}, {-0.082037, 0.010997, -0.100167},
{-0.183403, 0.001651, -0.098962}, {-0.074775, -0.030335, -0.062217},
{-0.031759, -0.050551, -0.059420}, {-0.051439, 0.010827, -0.052148},
{-0.126744, 0.008689, -0.047785}, {-0.145916, 0.042019, -0.077431},
{-0.093552, 0.054143, -0.060473}, {-0.090660, 0.012868, -0.018195},
{-0.079783, -0.033071, 0.001482}, {-0.033010, -0.022331, -0.014506},
{-0.004798, -0.017339, -0.060120}, {-0.025021, 0.026390, -0.003263},
{-0.001437, 0.025994, -0.040892}, {-0.074821, 0.019005, 0.027549},
{-0.030811, -0.012114, 0.034284}, { 0.006785, 0.004618, 0.018717},
{ 0.013392, -0.032597, -0.023731}, { 0.035994, 0.005963, -0.011757},
{ 0.008071, -0.045750, 0.024889}, { 0.013055, 0.017040, 0.054121},
{-0.012989, 0.044864, 0.036327}, { 0.025054, 0.047137, 0.009974},
{ 0.053801, 0.024178, 0.031774}, { 0.056442, -0.030647, 0.021291},
{ 0.032247, 0.052680, 0.049886}, { 0.035369, 0.090207, 0.031394},
{ 0.064720, 0.070390, 0.040938}, { 0.022112, 0.054834, 0.091059},
{ 0.041765, 0.086248, 0.070196}, { 0.070645, 0.060852, 0.078825},
{ 0.058506, 0.016920, 0.081612}, { 0.000009, 0.086500, 0.059849},
{ 0.071253, 0.107392, 0.059046}, { 0.094702, 0.096160, 0.090982},
{ 0.047639, 0.110877, 0.111227}, { 0.122444, 0.090909, 0.057396},
{ 0.101916, 0.052299, 0.029909}, { 0.076560, 0.086094, -0.007252},
{ 0.123411, 0.030769, 0.082749}, { 0.135579, 0.103022, 0.009540},
{ 0.120576, 0.065284, -0.024095}, { 0.077483, 0.028526, -0.012369},
{ 0.128747, 0.017901, -0.003874}, { 0.158254, 0.046962, 0.029577},
{ 0.102287, -0.002211, 0.037329}, { 0.089654, -0.021372, -0.006857},
{ 0.137917, 0.027228, -0.053223}, { 0.098728, -0.012192, -0.048518},
{ 0.083974, 0.036153, -0.062266}, { 0.048230, -0.010241, -0.052293},
{ 0.110135, 0.007715, -0.095233}, { 0.068294, -0.014317, -0.104029},
{ 0.063909, -0.056416, -0.063023}, { 0.059133, -0.044675, -0.023780},
{ 0.030748, 0.021845, -0.086332}, { 0.023994, -0.045574, -0.076232},
{ 0.052147, -0.059825, -0.109667}, { 0.013087, -0.020420, -0.121945},
{ 0.018163, -0.096765, -0.088758}, { 0.020196, -0.076470, -0.048112},
{ 0.020282, -0.084204, -0.135535}, { 0.040076, -0.053464, -0.161949},
{-0.017796, -0.103070, -0.059559}, {-0.016484, -0.070138, -0.016866},
{ 0.004849, -0.112481, -0.017731}, { 0.040160, -0.073873, -0.005327},
{ 0.002202, -0.094723, 0.045366}, {-0.056918, -0.081578, 0.017875},
{-0.031099, -0.141708, 0.009186}, {-0.102802, -0.122675, 0.030060},
{-0.061717, -0.145116, 0.076680}, {-0.073607, -0.050464, 0.072853},
{-0.117403, -0.194921, 0.040101}, {-0.185236, -0.133620, 0.045939},
{-0.160174, -0.057226, 0.056641}, {-0.178489, -0.173435, -0.007806},
{-0.199916, -0.204866, 0.047342}, {-0.152337, -0.249651, 0.034656},
{-0.185637, -0.230942, -0.002072}, {-0.122548, -0.215209, -0.024552},
{-0.249578, -0.209714, 0.009470}, {-0.160108, -0.257702, -0.040992},
{-0.216694, -0.289353, 0.027182}, {-0.226390, -0.147844, -0.022742},
{-0.288737, -0.272150, -0.013948}, {-0.262554, -0.237035, 0.072473},
{-0.306267, -0.188335, -0.032894}, {-0.259666, -0.345816, 0.024138},
{-0.271093, -0.137143, 0.040404}, {-0.201317, -0.286782, 0.107615},
{-0.235725, -0.163396, 0.113844}, {-0.159988, -0.209788, 0.112140},
{-0.262985, -0.056741, 0.093506}, {-0.277226, -0.037306, 0.016008},
{-0.293486, -0.040422, -0.062018}, {-0.214921, 0.022900, 0.055295},
{-0.253889, 0.058575, -0.000151}, {-0.246689, 0.024242, -0.058488},
{-0.143790, 0.006767, 0.014061}, {-0.187077, 0.048882, -0.035625},
{-0.196369, 0.112085, 0.031546}, {-0.124264, 0.086197, -0.020800},
{-0.126249, 0.016960, 0.095741}, {-0.079816, 0.080398, 0.051038},
{-0.056269, 0.075380, -0.028262}, {-0.120493, 0.148495, 0.028430},
{-0.161750, 0.101290, 0.117806}, {-0.003247, 0.083393, -0.017061},
{-0.034007, 0.142542, 0.007402}, {-0.037618, 0.025871, 0.089496},
{-0.082819, 0.184435, 0.073224}, { 0.006448, 0.167015, 0.080548},
{ 0.035315, 0.144022, 0.003218}, {-0.023459, 0.088147, 0.152604},
{ 0.006247, -0.024099, 0.077792}, { 0.039894, 0.057586, -0.042455},
{-0.020417, 0.035400, -0.093971}, { 0.075465, 0.052063, 0.145582},
{ 0.078027, 0.184720, 0.092096}, { 0.107295, 0.148380, 0.022264},
{ 0.066928, -0.052831, 0.065108}, { 0.093295, 0.118157, 0.149815},
{ 0.119373, 0.137114, 0.099536}, { 0.138653, 0.075509, 0.121545},
{ 0.174025, 0.077531, 0.077169}, { 0.165839, 0.150080, 0.133423},
{ 0.173276, 0.155887, 0.048150}, { 0.162910, 0.095898, 0.171896},
{ 0.214577, 0.112888, 0.115579}, { 0.204755, 0.106392, 0.032337},
{ 0.178853, 0.205034, 0.114760}, { 0.177401, 0.070504, -0.013778},
{ 0.241624, 0.166921, 0.066087}, { 0.219595, 0.183553, 0.172332},
{ 0.123671, 0.170842, 0.167216}, { 0.177104, 0.240197, 0.186359},
{ 0.272003, 0.220214, 0.126073}, { 0.093748, 0.235843, 0.160998},
{ 0.141510, 0.190012, 0.240416}, { 0.046878, 0.168984, 0.190412},
{ 0.094898, 0.107038, 0.235003}, { 0.108592, 0.269536, 0.262528},
{-0.027754, 0.234355, 0.134544}, { 0.265127, 0.267540, 0.199041},
{ 0.199523, 0.291507, 0.265171}, { 0.266177, 0.209339, 0.350369},
{ 0.322159, 0.344794, 0.270823}, { 0.399957, 0.264065, 0.110387},
{ 0.277817, 0.127407, -0.035625}, {-0.177038, 0.208155, 0.119077},
{ 0.049075, -0.076294, 0.145711}, { 0.187246, 0.042865, -0.127097},
{ 0.117885, -0.023489, -0.138658}, {-0.284256, 0.068153, 0.124259}
};
static const float lsf_cb3_16k[128][3] = {
{-0.223412, -0.236300, -0.188067}, {-0.202286, -0.218711, -0.102947},
{-0.251652, -0.161020, -0.125280}, {-0.169223, -0.138155, -0.140430},
{-0.176427, -0.146628, -0.222632}, {-0.120584, -0.187276, -0.180164},
{-0.195559, -0.074225, -0.169109}, {-0.144551, -0.142774, -0.073340},
{-0.111001, -0.111310, -0.130696}, {-0.095221, -0.174684, -0.111841},
{-0.112158, -0.103049, -0.195130}, {-0.059989, -0.142170, -0.157850},
{-0.127598, -0.051759, -0.153109}, {-0.063753, -0.067898, -0.164117},
{-0.141753, -0.068274, -0.091999}, {-0.060482, -0.101054, -0.099475},
{-0.104699, -0.104456, -0.066496}, {-0.073649, -0.052614, -0.091612},
{-0.088268, -0.019072, -0.129956}, {-0.018837, -0.104115, -0.127837},
{-0.021630, -0.033055, -0.129868}, {-0.083768, -0.047549, -0.041407},
{-0.055892, -0.108526, -0.043200}, {-0.027816, -0.062499, -0.048190},
{-0.002248, -0.110428, -0.062868}, { 0.001270, -0.033245, -0.072404},
{-0.042747, -0.013835, -0.033829}, {-0.037615, -0.147833, -0.083912},
{-0.045023, 0.006011, -0.092182}, {-0.050411, -0.081832, 0.005787},
{ 0.000357, -0.104282, -0.009428}, {-0.003893, -0.047892, -0.001506},
{-0.040077, -0.147110, -0.009065}, {-0.060858, -0.030972, 0.012999},
{-0.014674, 0.001370, 0.005554}, {-0.101362, -0.126061, -0.001898},
{-0.102519, -0.000390, -0.015721}, {-0.132687, -0.069608, -0.019928},
{-0.102227, -0.076131, 0.043306}, {-0.055193, 0.027001, 0.011857},
{-0.156427, -0.016629, 0.017480}, {-0.078736, 0.002809, 0.057979},
{-0.157789, -0.016693, -0.055073}, {-0.179397, -0.095520, 0.022065},
{-0.110219, 0.010408, -0.081927}, {-0.125392, 0.049111, 0.044595},
{-0.112528, 0.063173, -0.024954}, {-0.185525, 0.053093, -0.032102},
{-0.176887, -0.019379, -0.115125}, {-0.249706, -0.017664, -0.059188},
{-0.200243, -0.103311, -0.066846}, {-0.055404, 0.045106, -0.046991},
{-0.000544, 0.022690, -0.044831}, { 0.022298, -0.016367, -0.022509},
{ 0.028278, 0.017585, -0.100612}, { 0.061781, -0.020826, -0.068190},
{ 0.029157, -0.074477, -0.098898}, { 0.043073, -0.067234, -0.032293},
{ 0.060157, 0.034636, -0.034885}, { 0.071153, -0.013881, -0.009036},
{ 0.054196, -0.029989, -0.131139}, { 0.030193, 0.024976, 0.009861},
{ 0.055943, -0.045304, 0.031927}, { 0.033217, -0.002418, 0.038165},
{ 0.063814, 0.045625, 0.025309}, { 0.033689, 0.038819, 0.049700},
{ 0.073582, 0.028527, 0.060200}, {-0.007957, 0.022531, 0.043687},
{-0.000984, 0.054518, 0.018742}, { 0.057004, 0.060916, 0.060573},
{ 0.009883, 0.015238, 0.080211}, { 0.022742, 0.070832, 0.068855},
{ 0.053001, 0.029790, 0.091446}, {-0.042447, 0.060379, 0.061462},
{ 0.076826, 0.062468, 0.089653}, { 0.039065, 0.069768, 0.119128},
{ 0.064145, 0.095353, 0.071621}, { 0.094411, 0.069527, 0.054197},
{ 0.042812, 0.093060, 0.027980}, { 0.094791, 0.099189, 0.101112},
{ 0.117611, 0.048601, 0.093111}, { 0.119951, 0.122758, 0.051546},
{ 0.103558, 0.085245, -0.010700}, { 0.150126, 0.059766, 0.020280},
{ 0.108066, 0.017170, 0.008606}, { 0.108422, 0.023253, -0.063942},
{ 0.019652, 0.072284, -0.030331}, { 0.192719, 0.075624, 0.071156},
{ 0.221140, 0.069191, -0.035085}, { 0.188367, 0.126200, 0.035225},
{ 0.185760, 0.043537, -0.101714}, {-0.042518, 0.099646, 0.003244},
{-0.015308, -0.027521, 0.046006}, { 0.034086, -0.045777, 0.095989},
{ 0.007174, -0.093358, 0.046459}, {-0.051248, -0.062095, 0.083161},
{-0.045626, -0.133301, 0.052997}, {-0.037840, 0.024042, 0.131097},
{-0.020217, -0.115942, 0.126170}, {-0.134550, -0.036291, 0.111322},
{-0.110576, -0.160024, 0.091841}, {-0.093308, -0.184958, 0.013939},
{-0.082735, -0.167417, -0.051725}, {-0.169934, -0.173003, -0.007155},
{-0.128244, -0.213123, -0.053337}, {-0.079852, -0.154116, -0.246546},
{-0.032242, -0.108756, -0.204133}, {-0.140117, -0.199495, -0.284505},
{ 0.010842, -0.074979, -0.166333}, {-0.093313, 0.145006, 0.034110},
{-0.039236, 0.113213, 0.111053}, { 0.040613, -0.031783, 0.174058},
{-0.164232, 0.131421, 0.149842}, { 0.026893, 0.107281, 0.179297},
{ 0.047086, 0.158606, 0.103267}, {-0.070567, 0.210459, 0.134734},
{ 0.094392, 0.137050, 0.166892}, { 0.086039, 0.063657, 0.168825},
{ 0.159371, 0.120897, 0.154357}, { 0.147101, 0.160684, 0.114882},
{ 0.120158, 0.199650, 0.180948}, { 0.191417, 0.174500, 0.170734},
{ 0.159153, 0.142165, 0.233347}, { 0.232002, 0.150181, 0.102736},
{ 0.188299, 0.221738, 0.228748}, { 0.256786, 0.209685, 0.161534},
{ 0.257861, 0.247793, 0.250516}, {-0.164461, -0.000143, 0.232461}
};
static const float lsf_cb4_16k[128][3] = {
{-0.193369, -0.304643, -0.253777}, {-0.164125, -0.277786, -0.153116},
{-0.135681, -0.209120, -0.211724}, {-0.121822, -0.215734, -0.292207},
{-0.198781, -0.161674, -0.242538}, {-0.164147, -0.180570, -0.138070},
{-0.095915, -0.198695, -0.154309}, {-0.248386, -0.234462, -0.136984},
{-0.164968, -0.108318, -0.175635}, {-0.124171, -0.111809, -0.224402},
{-0.067398, -0.157017, -0.195759}, {-0.090132, -0.119174, -0.165253},
{-0.099460, -0.146895, -0.106799}, {-0.141493, -0.108103, -0.108880},
{-0.085088, -0.098340, -0.109953}, {-0.105526, -0.054463, -0.154315},
{-0.040480, -0.144285, -0.124042}, {-0.040969, -0.084039, -0.142880},
{-0.049082, -0.118553, -0.066686}, {-0.096336, -0.087515, -0.055741},
{-0.058605, -0.059327, -0.089275}, {-0.121842, -0.058681, -0.086949},
{-0.053792, -0.022025, -0.124451}, {-0.036744, -0.068891, -0.045865},
{ 0.003900, -0.098237, -0.091158}, {-0.001664, -0.045089, -0.081353},
{-0.072829, -0.034087, -0.038416}, {-0.100822, -0.007330, -0.088715},
{-0.035911, -0.005864, -0.062577}, {-0.020205, -0.026547, -0.019634},
{ 0.004291, -0.041290, -0.138181}, { 0.023404, -0.010932, -0.044904},
{ 0.013557, 0.014823, -0.092943}, { 0.059673, -0.031024, -0.095739},
{ 0.021130, -0.080607, -0.034594}, { 0.024655, -0.035564, 0.003243},
{ 0.017106, 0.006952, -0.000308}, { 0.075208, -0.030910, -0.031181},
{ 0.024965, 0.048632, -0.039448}, { 0.057028, 0.021547, -0.009418},
{-0.018577, 0.023697, -0.009759}, { 0.024077, 0.033053, 0.024324},
{ 0.037052, -0.003436, 0.044530}, {-0.012871, -0.007179, 0.031795},
{ 0.077877, 0.021547, 0.023131}, { 0.053365, 0.052078, 0.029433},
{ 0.011429, 0.070426, 0.028734}, {-0.001827, 0.033115, 0.061505},
{-0.044870, 0.038568, 0.026239}, { 0.061633, 0.034799, 0.059784},
{ 0.034261, 0.060342, 0.065185}, { 0.058981, 0.082481, 0.047252},
{ 0.090008, 0.065942, 0.044470}, { 0.066961, 0.073728, -0.000428},
{ 0.074763, 0.060293, 0.085632}, { 0.066366, 0.103375, 0.079642},
{ 0.122297, 0.036558, 0.058745}, { 0.111042, 0.092093, 0.085412},
{ 0.099243, 0.115476, 0.039254}, { 0.019973, 0.122844, 0.050255},
{ 0.159571, 0.098965, 0.051740}, { 0.137624, 0.072405, -0.006922},
{ 0.130240, 0.146091, 0.089698}, { 0.138335, 0.092968, 0.136193},
{ 0.066031, 0.149304, 0.125476}, { 0.202749, 0.145751, 0.077122},
{ 0.002224, 0.082811, 0.131200}, { 0.124476, 0.178073, 0.162336},
{ 0.174722, 0.190298, 0.127106}, { 0.202193, 0.153569, 0.163840},
{ 0.242604, 0.197796, 0.136929}, { 0.185809, 0.229348, 0.193353},
{-0.058814, 0.195178, 0.141821}, { 0.253646, 0.247175, 0.205766},
{ 0.061433, -0.025542, 0.119311}, {-0.057816, 0.082445, 0.073243},
{-0.069239, 0.148678, 0.031146}, {-0.030217, -0.008503, 0.106194},
{-0.026708, 0.087469, -0.009589}, {-0.090418, 0.000265, 0.056807},
{-0.050607, -0.019383, 0.010494}, {-0.079397, 0.008233, -0.011469},
{-0.072634, -0.061165, 0.046917}, {-0.075741, -0.072343, -0.007557},
{-0.025162, -0.073363, 0.005173}, {-0.123371, -0.041257, -0.008375},
{-0.139904, 0.018285, 0.009920}, {-0.143421, -0.104238, 0.033457},
{-0.100923, -0.134400, -0.023257}, {-0.157791, -0.095042, -0.036959},
{-0.219890, -0.078637, 0.001815}, {-0.183607, -0.023053, -0.043678},
{-0.145303, -0.158923, -0.059045}, {-0.197615, -0.165199, 0.028099},
{-0.225131, -0.167756, -0.056401}, {-0.216572, -0.104751, -0.102964},
{-0.171336, -0.241967, -0.063404}, {-0.134035, -0.205614, 0.011831},
{-0.297116, -0.211173, -0.015352}, {-0.086464, -0.200592, -0.070454},
{-0.217777, -0.278403, 0.030398}, {-0.236248, -0.323694, -0.087588},
{-0.222074, -0.210785, 0.106210}, {-0.283400, -0.097077, 0.041303},
{-0.078417, -0.154464, 0.062956}, {-0.214417, -0.100695, 0.121909},
{-0.178576, -0.028847, 0.061042}, {-0.037999, -0.144233, -0.010546},
{-0.086695, -0.070996, 0.125282}, { 0.010788, -0.085006, 0.058527},
{-0.154015, 0.066560, 0.071038}, {-0.143503, 0.033260, 0.154393},
{-0.134069, 0.032420, -0.056293}, {-0.110851, 0.086908, 0.003920},
{-0.057254, 0.047674, -0.055571}, {-0.214206, 0.068784, -0.004735},
{-0.257264, 0.050468, 0.081702}, {-0.291834, 0.004120, -0.022366},
{-0.173309, -0.029081, -0.115901}, {-0.207622, 0.168664, 0.136030},
{ 0.090541, 0.032754, -0.057330}, { 0.140219, -0.000735, -0.015633},
{ 0.136697, -0.017163, -0.100909}, { 0.029838, -0.089515, -0.147130},
{-0.055367, -0.072683, -0.214015}, { 0.048680, -0.057633, -0.212429},
{-0.013134, -0.113898, -0.196403}, {-0.071702, -0.159408, -0.254895}
};
static const float lsf_cb5_16k[128][4] = {
{-0.201277, -0.278679, -0.173262, -0.198580},
{-0.214667, -0.151922, -0.117551, -0.192713},
{-0.160962, -0.207728, -0.124750, -0.129749},
{-0.131043, -0.137818, -0.155281, -0.166308},
{-0.179134, -0.169602, -0.165223, -0.066293},
{-0.136474, -0.177035, -0.250127, -0.134370},
{-0.066970, -0.146274, -0.170638, -0.134436},
{-0.083288, -0.165860, -0.103437, -0.140361},
{-0.130474, -0.119317, -0.124393, -0.086408},
{-0.127609, -0.134415, -0.073592, -0.116103},
{-0.113027, -0.091756, -0.107786, -0.131935},
{-0.125530, -0.182152, -0.093796, -0.045088},
{-0.077122, -0.138052, -0.166271, -0.038886},
{-0.073027, -0.106845, -0.067073, -0.113910},
{-0.049146, -0.107019, -0.112531, -0.063388},
{-0.101539, -0.119586, -0.050297, -0.040670},
{-0.107784, -0.066913, -0.080993, -0.052352},
{-0.152155, -0.103010, -0.090461, -0.015526},
{-0.153087, -0.087656, -0.029889, -0.037367},
{-0.215281, -0.138062, -0.089162, -0.050839},
{-0.053350, -0.060169, -0.063459, -0.024499},
{-0.051674, -0.076355, -0.033733, -0.077211},
{-0.045047, -0.107006, -0.020880, -0.024525},
{-0.083003, -0.063672, -0.013243, -0.028324},
{-0.104104, -0.075450, -0.032746, 0.024480},
{-0.085695, -0.019502, -0.045121, -0.025016},
{-0.123120, -0.030844, -0.003533, -0.016224},
{-0.025568, -0.049172, -0.003911, -0.027522},
{-0.039029, -0.019857, -0.043211, -0.058087},
{-0.040122, -0.023067, -0.001356, 0.008607},
{-0.063351, -0.001776, 0.016015, -0.027088},
{-0.068110, -0.038838, 0.042525, 0.001076},
{-0.043623, -0.020736, -0.047862, 0.037710},
{-0.041052, 0.021954, -0.025660, 0.000758},
{-0.013035, 0.002583, -0.008233, -0.037300},
{-0.005523, -0.014670, 0.019651, -0.012667},
{-0.004409, -0.014437, -0.059412, -0.019701},
{ 0.024946, -0.011663, -0.014351, -0.028762},
{ 0.012660, 0.018489, -0.010205, 0.012695},
{-0.004423, 0.017827, 0.040544, 0.003629},
{ 0.020684, 0.026743, 0.007752, -0.025595},
{ 0.032071, 0.000043, 0.026188, -0.006444},
{ 0.058793, 0.015820, -0.001119, -0.017415},
{ 0.020156, -0.047590, 0.004227, 0.008670},
{ 0.054770, 0.032135, 0.029770, -0.009767},
{ 0.030884, 0.047757, 0.033068, 0.006866},
{ 0.062039, 0.011646, 0.056037, 0.016859},
{ 0.013798, -0.028196, 0.060710, 0.014299},
{ 0.100043, 0.041445, 0.023379, -0.014889},
{ 0.062728, -0.042821, 0.002180, -0.055380},
{ 0.061663, 0.018767, -0.015571, -0.074095},
{ 0.062980, 0.080497, 0.011808, -0.031787},
{ 0.084964, 0.043100, -0.025877, 0.020309},
{ 0.014707, 0.035421, -0.041440, -0.053373},
{ 0.081268, 0.005791, -0.066290, -0.039825},
{ 0.017691, -0.020401, -0.040513, -0.083960},
{ 0.120874, 0.055753, -0.025988, -0.059552},
{ 0.079912, 0.007894, -0.085380, -0.114587},
{ 0.036856, -0.039331, -0.104237, -0.069116},
{ 0.008526, -0.064273, -0.048312, -0.038595},
{ 0.033461, -0.028956, -0.066505, 0.038722},
{-0.042064, -0.043989, -0.100653, -0.071550},
{-0.015342, -0.064850, -0.065675, -0.122769},
{-0.006581, -0.004919, -0.113564, -0.145753},
{ 0.008273, -0.070702, -0.164998, -0.095541},
{-0.001698, -0.063744, -0.129971, -0.011162},
{-0.048471, -0.087500, -0.111006, -0.161823},
{-0.032193, -0.091955, -0.080642, 0.012288},
{-0.095873, -0.015986, -0.072722, -0.101745},
{-0.079477, -0.082060, -0.203008, -0.100297},
{-0.023883, -0.064022, -0.168341, -0.211739},
{-0.070530, -0.103547, -0.123858, 0.055049},
{-0.033503, -0.076812, -0.016287, 0.044159},
{-0.088427, -0.161682, -0.058579, 0.013873},
{-0.083068, -0.168222, -0.016773, -0.080209},
{-0.080548, -0.139090, 0.030544, 0.007171},
{-0.117482, -0.083718, 0.027074, -0.003674},
{-0.163085, -0.156856, -0.012618, -0.022329},
{-0.176540, -0.113042, -0.020148, 0.051770},
{-0.153891, -0.199293, -0.043244, 0.028331},
{-0.107822, -0.150615, 0.016430, 0.092919},
{-0.137676, -0.183224, 0.066026, 0.029343},
{-0.191106, -0.099250, 0.045370, 0.004084},
{-0.237042, -0.130815, -0.022543, -0.029428},
{-0.201014, -0.053591, -0.007305, -0.033547},
{-0.249286, -0.228408, 0.005002, 0.007146},
{-0.206509, -0.211998, -0.061352, -0.047233},
{-0.255702, -0.135114, 0.076375, 0.036630},
{-0.296271, -0.073946, -0.007273, -0.019601},
{-0.302917, -0.175111, -0.070024, -0.043905},
{-0.239275, -0.043962, -0.084982, -0.067446},
{-0.254583, -0.294720, -0.088762, -0.070451},
{-0.205583, -0.238996, -0.124753, 0.033076},
{-0.205583, -0.215882, -0.028472, 0.118679},
{-0.153640, -0.204464, -0.039654, -0.134441},
{-0.145929, -0.191970, -0.175308, 0.021366},
{-0.149348, -0.212569, -0.118324, 0.103812},
{-0.166397, -0.220581, -0.265260, -0.029113},
{-0.164171, -0.231262, -0.258828, 0.061427},
{-0.200198, -0.263453, -0.212016, 0.115359},
{-0.130088, -0.212168, -0.202368, 0.118563},
{-0.206387, -0.078075, -0.227856, -0.111165},
{-0.129605, -0.176848, -0.241584, -0.259900},
{-0.176826, -0.045901, -0.141712, -0.209345},
{-0.351173, -0.031097, -0.133935, -0.182412},
{-0.164232, 0.027006, -0.014039, -0.053567},
{-0.171037, -0.025924, 0.030972, 0.017329},
{-0.080862, -0.021577, 0.007652, 0.063968},
{-0.061788, 0.042024, -0.018783, -0.057979},
{-0.110311, 0.054760, 0.031446, -0.006710},
{-0.136637, 0.022171, 0.084991, 0.028039},
{-0.254471, -0.004376, 0.078034, 0.033649},
{-0.234464, 0.088157, 0.040999, 0.002639},
{-0.037095, 0.059443, 0.072180, 0.015027},
{-0.046841, -0.004813, 0.088266, 0.038786},
{-0.086782, 0.120100, 0.082655, 0.020271},
{-0.118361, -0.069242, 0.094867, 0.039200},
{-0.023342, -0.084303, 0.052684, 0.017093},
{-0.014194, 0.001012, 0.011946, 0.074125},
{-0.015342, 0.076396, 0.022365, -0.028001},
{ 0.027706, 0.037047, 0.107573, 0.060815},
{ 0.030615, 0.040664, 0.010467, 0.074289},
{ 0.038646, 0.115584, 0.069627, 0.007642},
{ 0.096463, 0.069818, 0.062494, 0.015413},
{ 0.054834, 0.065232, 0.054286, 0.110088},
{ 0.152312, 0.092371, 0.026420, -0.013184},
{ 0.144264, 0.123438, 0.080131, 0.023233},
{ 0.124405, 0.009943, -0.148477, -0.205184}
};
static const float *lsf_codebooks_16k[] = {
lsf_cb1_16k[0], lsf_cb2_16k[0], lsf_cb3_16k[0], lsf_cb4_16k[0],
lsf_cb5_16k[0]
};
#endif /* AVCODEC_SIPR16KDATA_H */
| 123linslouis-android-video-cutter | jni/libavcodec/sipr16kdata.h | C | asf20 | 32,717 |
/*
* MLP codec common header file
* Copyright (c) 2007-2008 Ian Caulfield
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef AVCODEC_MLP_H
#define AVCODEC_MLP_H
#include <stdint.h>
#include "avcodec.h"
/** Last possible matrix channel for each codec */
#define MAX_MATRIX_CHANNEL_MLP 5
#define MAX_MATRIX_CHANNEL_TRUEHD 7
/** Maximum number of channels in a valid stream.
* MLP : 5.1 + 2 noise channels -> 8 channels
* TrueHD: 7.1 -> 8 channels
*/
#define MAX_CHANNELS 8
/** Maximum number of matrices used in decoding; most streams have one matrix
* per output channel, but some rematrix a channel (usually 0) more than once.
*/
#define MAX_MATRICES_MLP 6
#define MAX_MATRICES_TRUEHD 8
#define MAX_MATRICES 8
/** Maximum number of substreams that can be decoded.
* MLP's limit is 2. TrueHD supports at least up to 3.
*/
#define MAX_SUBSTREAMS 3
/** which multiple of 48000 the maximum sample rate is */
#define MAX_RATEFACTOR 4
/** maximum sample frequency seen in files */
#define MAX_SAMPLERATE (MAX_RATEFACTOR * 48000)
/** maximum number of audio samples within one access unit */
#define MAX_BLOCKSIZE (40 * MAX_RATEFACTOR)
/** next power of two greater than MAX_BLOCKSIZE */
#define MAX_BLOCKSIZE_POW2 (64 * MAX_RATEFACTOR)
/** number of allowed filters */
#define NUM_FILTERS 2
/** The maximum number of taps in IIR and FIR filters. */
#define MAX_FIR_ORDER 8
#define MAX_IIR_ORDER 4
/** Code that signals end of a stream. */
#define END_OF_STREAM 0xd234d234
#define FIR 0
#define IIR 1
/** filter data */
typedef struct {
uint8_t order; ///< number of taps in filter
uint8_t shift; ///< Right shift to apply to output of filter.
int32_t state[MAX_FIR_ORDER];
} FilterParams;
/** sample data coding information */
typedef struct {
FilterParams filter_params[NUM_FILTERS];
int32_t coeff[NUM_FILTERS][MAX_FIR_ORDER];
int16_t huff_offset; ///< Offset to apply to residual values.
int32_t sign_huff_offset; ///< sign/rounding-corrected version of huff_offset
uint8_t codebook; ///< Which VLC codebook to use to read residuals.
uint8_t huff_lsbs; ///< Size of residual suffix not encoded using VLC.
} ChannelParams;
/** Tables defining the Huffman codes.
* There are three entropy coding methods used in MLP (four if you count
* "none" as a method). These use the same sequences for codes starting with
* 00 or 01, but have different codes starting with 1.
*/
extern const uint8_t ff_mlp_huffman_tables[3][18][2];
/** MLP uses checksums that seem to be based on the standard CRC algorithm, but
* are not (in implementation terms, the table lookup and XOR are reversed).
* We can implement this behavior using a standard av_crc on all but the
* last element, then XOR that with the last element.
*/
uint8_t ff_mlp_checksum8 (const uint8_t *buf, unsigned int buf_size);
uint16_t ff_mlp_checksum16(const uint8_t *buf, unsigned int buf_size);
/** Calculate an 8-bit checksum over a restart header -- a non-multiple-of-8
* number of bits, starting two bits into the first byte of buf.
*/
uint8_t ff_mlp_restart_checksum(const uint8_t *buf, unsigned int bit_size);
/** XOR together all the bytes of a buffer.
* Does this belong in dspcontext?
*/
uint8_t ff_mlp_calculate_parity(const uint8_t *buf, unsigned int buf_size);
void ff_mlp_init_crc(void);
/** XOR four bytes into one. */
static inline uint8_t xor_32_to_8(uint32_t value)
{
value ^= value >> 16;
value ^= value >> 8;
return value;
}
#endif /* AVCODEC_MLP_H */
| 123linslouis-android-video-cutter | jni/libavcodec/mlp.h | C | asf20 | 4,419 |
/*
* Zip Motion Blocks Video (ZMBV) decoder
* Copyright (c) 2006 Konstantin Shishkov
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* Zip Motion Blocks Video decoder
*/
#include <stdio.h>
#include <stdlib.h>
#include "libavutil/intreadwrite.h"
#include "avcodec.h"
#include <zlib.h>
#define ZMBV_KEYFRAME 1
#define ZMBV_DELTAPAL 2
enum ZmbvFormat {
ZMBV_FMT_NONE = 0,
ZMBV_FMT_1BPP = 1,
ZMBV_FMT_2BPP = 2,
ZMBV_FMT_4BPP = 3,
ZMBV_FMT_8BPP = 4,
ZMBV_FMT_15BPP = 5,
ZMBV_FMT_16BPP = 6,
ZMBV_FMT_24BPP = 7,
ZMBV_FMT_32BPP = 8
};
/*
* Decoder context
*/
typedef struct ZmbvContext {
AVCodecContext *avctx;
AVFrame pic;
int bpp;
unsigned int decomp_size;
uint8_t* decomp_buf;
uint8_t pal[768];
uint8_t *prev, *cur;
int width, height;
int fmt;
int comp;
int flags;
int bw, bh, bx, by;
int decomp_len;
z_stream zstream;
int (*decode_intra)(struct ZmbvContext *c);
int (*decode_xor)(struct ZmbvContext *c);
} ZmbvContext;
/**
* Decode XOR'ed frame - 8bpp version
*/
static int zmbv_decode_xor_8(ZmbvContext *c)
{
uint8_t *src = c->decomp_buf;
uint8_t *output, *prev;
int8_t *mvec;
int x, y;
int d, dx, dy, bw2, bh2;
int block;
int i, j;
int mx, my;
output = c->cur;
prev = c->prev;
if(c->flags & ZMBV_DELTAPAL){
for(i = 0; i < 768; i++)
c->pal[i] ^= *src++;
}
mvec = (int8_t*)src;
src += ((c->bx * c->by * 2 + 3) & ~3);
block = 0;
for(y = 0; y < c->height; y += c->bh) {
bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
for(x = 0; x < c->width; x += c->bw) {
uint8_t *out, *tprev;
d = mvec[block] & 1;
dx = mvec[block] >> 1;
dy = mvec[block + 1] >> 1;
block += 2;
bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
/* copy block - motion vectors out of bounds are used to zero blocks */
out = output + x;
tprev = prev + x + dx + dy * c->width;
mx = x + dx;
my = y + dy;
for(j = 0; j < bh2; j++){
if((my + j < 0) || (my + j >= c->height)) {
memset(out, 0, bw2);
} else {
for(i = 0; i < bw2; i++){
if((mx + i < 0) || (mx + i >= c->width))
out[i] = 0;
else
out[i] = tprev[i];
}
}
out += c->width;
tprev += c->width;
}
if(d) { /* apply XOR'ed difference */
out = output + x;
for(j = 0; j < bh2; j++){
for(i = 0; i < bw2; i++)
out[i] ^= *src++;
out += c->width;
}
}
}
output += c->width * c->bh;
prev += c->width * c->bh;
}
if(src - c->decomp_buf != c->decomp_len)
av_log(c->avctx, AV_LOG_ERROR, "Used %ti of %i bytes\n", src-c->decomp_buf, c->decomp_len);
return 0;
}
/**
* Decode XOR'ed frame - 15bpp and 16bpp version
*/
static int zmbv_decode_xor_16(ZmbvContext *c)
{
uint8_t *src = c->decomp_buf;
uint16_t *output, *prev;
int8_t *mvec;
int x, y;
int d, dx, dy, bw2, bh2;
int block;
int i, j;
int mx, my;
output = (uint16_t*)c->cur;
prev = (uint16_t*)c->prev;
mvec = (int8_t*)src;
src += ((c->bx * c->by * 2 + 3) & ~3);
block = 0;
for(y = 0; y < c->height; y += c->bh) {
bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
for(x = 0; x < c->width; x += c->bw) {
uint16_t *out, *tprev;
d = mvec[block] & 1;
dx = mvec[block] >> 1;
dy = mvec[block + 1] >> 1;
block += 2;
bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
/* copy block - motion vectors out of bounds are used to zero blocks */
out = output + x;
tprev = prev + x + dx + dy * c->width;
mx = x + dx;
my = y + dy;
for(j = 0; j < bh2; j++){
if((my + j < 0) || (my + j >= c->height)) {
memset(out, 0, bw2 * 2);
} else {
for(i = 0; i < bw2; i++){
if((mx + i < 0) || (mx + i >= c->width))
out[i] = 0;
else
out[i] = tprev[i];
}
}
out += c->width;
tprev += c->width;
}
if(d) { /* apply XOR'ed difference */
out = output + x;
for(j = 0; j < bh2; j++){
for(i = 0; i < bw2; i++) {
out[i] ^= *((uint16_t*)src);
src += 2;
}
out += c->width;
}
}
}
output += c->width * c->bh;
prev += c->width * c->bh;
}
if(src - c->decomp_buf != c->decomp_len)
av_log(c->avctx, AV_LOG_ERROR, "Used %ti of %i bytes\n", src-c->decomp_buf, c->decomp_len);
return 0;
}
#ifdef ZMBV_ENABLE_24BPP
/**
* Decode XOR'ed frame - 24bpp version
*/
static int zmbv_decode_xor_24(ZmbvContext *c)
{
uint8_t *src = c->decomp_buf;
uint8_t *output, *prev;
int8_t *mvec;
int x, y;
int d, dx, dy, bw2, bh2;
int block;
int i, j;
int mx, my;
int stride;
output = c->cur;
prev = c->prev;
stride = c->width * 3;
mvec = (int8_t*)src;
src += ((c->bx * c->by * 2 + 3) & ~3);
block = 0;
for(y = 0; y < c->height; y += c->bh) {
bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
for(x = 0; x < c->width; x += c->bw) {
uint8_t *out, *tprev;
d = mvec[block] & 1;
dx = mvec[block] >> 1;
dy = mvec[block + 1] >> 1;
block += 2;
bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
/* copy block - motion vectors out of bounds are used to zero blocks */
out = output + x * 3;
tprev = prev + (x + dx) * 3 + dy * stride;
mx = x + dx;
my = y + dy;
for(j = 0; j < bh2; j++){
if((my + j < 0) || (my + j >= c->height)) {
memset(out, 0, bw2 * 3);
} else {
for(i = 0; i < bw2; i++){
if((mx + i < 0) || (mx + i >= c->width)) {
out[i * 3 + 0] = 0;
out[i * 3 + 1] = 0;
out[i * 3 + 2] = 0;
} else {
out[i * 3 + 0] = tprev[i * 3 + 0];
out[i * 3 + 1] = tprev[i * 3 + 1];
out[i * 3 + 2] = tprev[i * 3 + 2];
}
}
}
out += stride;
tprev += stride;
}
if(d) { /* apply XOR'ed difference */
out = output + x * 3;
for(j = 0; j < bh2; j++){
for(i = 0; i < bw2; i++) {
out[i * 3 + 0] ^= *src++;
out[i * 3 + 1] ^= *src++;
out[i * 3 + 2] ^= *src++;
}
out += stride;
}
}
}
output += stride * c->bh;
prev += stride * c->bh;
}
if(src - c->decomp_buf != c->decomp_len)
av_log(c->avctx, AV_LOG_ERROR, "Used %i of %i bytes\n", src-c->decomp_buf, c->decomp_len);
return 0;
}
#endif //ZMBV_ENABLE_24BPP
/**
* Decode XOR'ed frame - 32bpp version
*/
static int zmbv_decode_xor_32(ZmbvContext *c)
{
uint8_t *src = c->decomp_buf;
uint32_t *output, *prev;
int8_t *mvec;
int x, y;
int d, dx, dy, bw2, bh2;
int block;
int i, j;
int mx, my;
output = (uint32_t*)c->cur;
prev = (uint32_t*)c->prev;
mvec = (int8_t*)src;
src += ((c->bx * c->by * 2 + 3) & ~3);
block = 0;
for(y = 0; y < c->height; y += c->bh) {
bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
for(x = 0; x < c->width; x += c->bw) {
uint32_t *out, *tprev;
d = mvec[block] & 1;
dx = mvec[block] >> 1;
dy = mvec[block + 1] >> 1;
block += 2;
bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
/* copy block - motion vectors out of bounds are used to zero blocks */
out = output + x;
tprev = prev + x + dx + dy * c->width;
mx = x + dx;
my = y + dy;
for(j = 0; j < bh2; j++){
if((my + j < 0) || (my + j >= c->height)) {
memset(out, 0, bw2 * 4);
} else {
for(i = 0; i < bw2; i++){
if((mx + i < 0) || (mx + i >= c->width))
out[i] = 0;
else
out[i] = tprev[i];
}
}
out += c->width;
tprev += c->width;
}
if(d) { /* apply XOR'ed difference */
out = output + x;
for(j = 0; j < bh2; j++){
for(i = 0; i < bw2; i++) {
out[i] ^= *((uint32_t*)src);
src += 4;
}
out += c->width;
}
}
}
output += c->width * c->bh;
prev += c->width * c->bh;
}
if(src - c->decomp_buf != c->decomp_len)
av_log(c->avctx, AV_LOG_ERROR, "Used %ti of %i bytes\n", src-c->decomp_buf, c->decomp_len);
return 0;
}
/**
* Decode intraframe
*/
static int zmbv_decode_intra(ZmbvContext *c)
{
uint8_t *src = c->decomp_buf;
/* make the palette available on the way out */
if (c->fmt == ZMBV_FMT_8BPP) {
memcpy(c->pal, src, 768);
src += 768;
}
memcpy(c->cur, src, c->width * c->height * (c->bpp / 8));
return 0;
}
static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
{
const uint8_t *buf = avpkt->data;
int buf_size = avpkt->size;
ZmbvContext * const c = avctx->priv_data;
uint8_t *outptr;
int zret = Z_OK; // Zlib return code
int len = buf_size;
int hi_ver, lo_ver;
if(c->pic.data[0])
avctx->release_buffer(avctx, &c->pic);
c->pic.reference = 1;
c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
if(avctx->get_buffer(avctx, &c->pic) < 0){
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
return -1;
}
outptr = c->pic.data[0]; // Output image pointer
/* parse header */
c->flags = buf[0];
buf++; len--;
if(c->flags & ZMBV_KEYFRAME) {
hi_ver = buf[0];
lo_ver = buf[1];
c->comp = buf[2];
c->fmt = buf[3];
c->bw = buf[4];
c->bh = buf[5];
buf += 6;
len -= 6;
av_log(avctx, AV_LOG_DEBUG, "Flags=%X ver=%i.%i comp=%i fmt=%i blk=%ix%i\n",c->flags,hi_ver,lo_ver,c->comp,c->fmt,c->bw,c->bh);
if(hi_ver != 0 || lo_ver != 1) {
av_log(avctx, AV_LOG_ERROR, "Unsupported version %i.%i\n", hi_ver, lo_ver);
return -1;
}
if(c->bw == 0 || c->bh == 0) {
av_log(avctx, AV_LOG_ERROR, "Unsupported block size %ix%i\n", c->bw, c->bh);
return -1;
}
if(c->comp != 0 && c->comp != 1) {
av_log(avctx, AV_LOG_ERROR, "Unsupported compression type %i\n", c->comp);
return -1;
}
switch(c->fmt) {
case ZMBV_FMT_8BPP:
c->bpp = 8;
c->decode_intra = zmbv_decode_intra;
c->decode_xor = zmbv_decode_xor_8;
break;
case ZMBV_FMT_15BPP:
case ZMBV_FMT_16BPP:
c->bpp = 16;
c->decode_intra = zmbv_decode_intra;
c->decode_xor = zmbv_decode_xor_16;
break;
#ifdef ZMBV_ENABLE_24BPP
case ZMBV_FMT_24BPP:
c->bpp = 24;
c->decode_intra = zmbv_decode_intra;
c->decode_xor = zmbv_decode_xor_24;
break;
#endif //ZMBV_ENABLE_24BPP
case ZMBV_FMT_32BPP:
c->bpp = 32;
c->decode_intra = zmbv_decode_intra;
c->decode_xor = zmbv_decode_xor_32;
break;
default:
c->decode_intra = NULL;
c->decode_xor = NULL;
av_log(avctx, AV_LOG_ERROR, "Unsupported (for now) format %i\n", c->fmt);
return -1;
}
zret = inflateReset(&c->zstream);
if (zret != Z_OK) {
av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
return -1;
}
c->cur = av_realloc(c->cur, avctx->width * avctx->height * (c->bpp / 8));
c->prev = av_realloc(c->prev, avctx->width * avctx->height * (c->bpp / 8));
c->bx = (c->width + c->bw - 1) / c->bw;
c->by = (c->height+ c->bh - 1) / c->bh;
}
if(c->decode_intra == NULL) {
av_log(avctx, AV_LOG_ERROR, "Error! Got no format or no keyframe!\n");
return -1;
}
if(c->comp == 0) { //Uncompressed data
memcpy(c->decomp_buf, buf, len);
c->decomp_size = 1;
} else { // ZLIB-compressed data
c->zstream.total_in = c->zstream.total_out = 0;
c->zstream.next_in = buf;
c->zstream.avail_in = len;
c->zstream.next_out = c->decomp_buf;
c->zstream.avail_out = c->decomp_size;
inflate(&c->zstream, Z_FINISH);
c->decomp_len = c->zstream.total_out;
}
if(c->flags & ZMBV_KEYFRAME) {
c->pic.key_frame = 1;
c->pic.pict_type = FF_I_TYPE;
c->decode_intra(c);
} else {
c->pic.key_frame = 0;
c->pic.pict_type = FF_P_TYPE;
if(c->decomp_len)
c->decode_xor(c);
}
/* update frames */
{
uint8_t *out, *src;
int i, j;
out = c->pic.data[0];
src = c->cur;
switch(c->fmt) {
case ZMBV_FMT_8BPP:
for(j = 0; j < c->height; j++) {
for(i = 0; i < c->width; i++) {
out[i * 3 + 0] = c->pal[(*src) * 3 + 0];
out[i * 3 + 1] = c->pal[(*src) * 3 + 1];
out[i * 3 + 2] = c->pal[(*src) * 3 + 2];
src++;
}
out += c->pic.linesize[0];
}
break;
case ZMBV_FMT_15BPP:
for(j = 0; j < c->height; j++) {
for(i = 0; i < c->width; i++) {
uint16_t tmp = AV_RL16(src);
src += 2;
out[i * 3 + 0] = (tmp & 0x7C00) >> 7;
out[i * 3 + 1] = (tmp & 0x03E0) >> 2;
out[i * 3 + 2] = (tmp & 0x001F) << 3;
}
out += c->pic.linesize[0];
}
break;
case ZMBV_FMT_16BPP:
for(j = 0; j < c->height; j++) {
for(i = 0; i < c->width; i++) {
uint16_t tmp = AV_RL16(src);
src += 2;
out[i * 3 + 0] = (tmp & 0xF800) >> 8;
out[i * 3 + 1] = (tmp & 0x07E0) >> 3;
out[i * 3 + 2] = (tmp & 0x001F) << 3;
}
out += c->pic.linesize[0];
}
break;
#ifdef ZMBV_ENABLE_24BPP
case ZMBV_FMT_24BPP:
for(j = 0; j < c->height; j++) {
memcpy(out, src, c->width * 3);
src += c->width * 3;
out += c->pic.linesize[0];
}
break;
#endif //ZMBV_ENABLE_24BPP
case ZMBV_FMT_32BPP:
for(j = 0; j < c->height; j++) {
for(i = 0; i < c->width; i++) {
uint32_t tmp = AV_RL32(src);
src += 4;
AV_WB24(out+(i*3), tmp);
}
out += c->pic.linesize[0];
}
break;
default:
av_log(avctx, AV_LOG_ERROR, "Cannot handle format %i\n", c->fmt);
}
memcpy(c->prev, c->cur, c->width * c->height * (c->bpp / 8));
}
*data_size = sizeof(AVFrame);
*(AVFrame*)data = c->pic;
/* always report that the buffer was completely consumed */
return buf_size;
}
/*
*
* Init zmbv decoder
*
*/
static av_cold int decode_init(AVCodecContext *avctx)
{
ZmbvContext * const c = avctx->priv_data;
int zret; // Zlib return code
c->avctx = avctx;
c->width = avctx->width;
c->height = avctx->height;
c->bpp = avctx->bits_per_coded_sample;
// Needed if zlib unused or init aborted before inflateInit
memset(&(c->zstream), 0, sizeof(z_stream));
avctx->pix_fmt = PIX_FMT_RGB24;
c->decomp_size = (avctx->width + 255) * 4 * (avctx->height + 64);
/* Allocate decompression buffer */
if (c->decomp_size) {
if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
return 1;
}
}
c->zstream.zalloc = Z_NULL;
c->zstream.zfree = Z_NULL;
c->zstream.opaque = Z_NULL;
zret = inflateInit(&(c->zstream));
if (zret != Z_OK) {
av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
return 1;
}
return 0;
}
/*
*
* Uninit zmbv decoder
*
*/
static av_cold int decode_end(AVCodecContext *avctx)
{
ZmbvContext * const c = avctx->priv_data;
av_freep(&c->decomp_buf);
if (c->pic.data[0])
avctx->release_buffer(avctx, &c->pic);
inflateEnd(&(c->zstream));
av_freep(&c->cur);
av_freep(&c->prev);
return 0;
}
AVCodec zmbv_decoder = {
"zmbv",
AVMEDIA_TYPE_VIDEO,
CODEC_ID_ZMBV,
sizeof(ZmbvContext),
decode_init,
NULL,
decode_end,
decode_frame,
CODEC_CAP_DR1,
.long_name = NULL_IF_CONFIG_SMALL("Zip Motion Blocks Video"),
};
| 123linslouis-android-video-cutter | jni/libavcodec/zmbv.c | C | asf20 | 19,097 |
/*
* Dirac decoder support via libdirac library
* Copyright (c) 2005 BBC, Andrew Kennedy <dirac at rd dot bbc dot co dot uk>
* Copyright (c) 2006-2008 BBC, Anuradha Suraparaju <asuraparaju at gmail dot com >
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* Dirac decoder support via libdirac library; more details about the Dirac
* project can be found at http://dirac.sourceforge.net/.
* The libdirac_decoder library implements Dirac specification version 2.2
* (http://dirac.sourceforge.net/specification.html).
*/
#include "libdirac.h"
#undef NDEBUG
#include <assert.h>
#include <libdirac_decoder/dirac_parser.h>
/** contains a single frame returned from Dirac */
typedef struct FfmpegDiracDecoderParams {
/** decoder handle */
dirac_decoder_t* p_decoder;
/** buffer to hold decoded frame */
unsigned char* p_out_frame_buf;
} FfmpegDiracDecoderParams;
/**
* returns FFmpeg chroma format
*/
static enum PixelFormat GetFfmpegChromaFormat(dirac_chroma_t dirac_pix_fmt)
{
int num_formats = sizeof(ffmpeg_dirac_pixel_format_map) /
sizeof(ffmpeg_dirac_pixel_format_map[0]);
int idx;
for (idx = 0; idx < num_formats; ++idx)
if (ffmpeg_dirac_pixel_format_map[idx].dirac_pix_fmt == dirac_pix_fmt)
return ffmpeg_dirac_pixel_format_map[idx].ff_pix_fmt;
return PIX_FMT_NONE;
}
static av_cold int libdirac_decode_init(AVCodecContext *avccontext)
{
FfmpegDiracDecoderParams *p_dirac_params = avccontext->priv_data;
p_dirac_params->p_decoder = dirac_decoder_init(avccontext->debug);
if (!p_dirac_params->p_decoder)
return -1;
return 0;
}
static int libdirac_decode_frame(AVCodecContext *avccontext,
void *data, int *data_size,
AVPacket *avpkt)
{
const uint8_t *buf = avpkt->data;
int buf_size = avpkt->size;
FfmpegDiracDecoderParams *p_dirac_params = avccontext->priv_data;
AVPicture *picture = data;
AVPicture pic;
int pict_size;
unsigned char *buffer[3];
*data_size = 0;
if (buf_size > 0) {
/* set data to decode into buffer */
dirac_buffer(p_dirac_params->p_decoder, buf, buf + buf_size);
if ((buf[4] & 0x08) == 0x08 && (buf[4] & 0x03))
avccontext->has_b_frames = 1;
}
while (1) {
/* parse data and process result */
DecoderState state = dirac_parse(p_dirac_params->p_decoder);
switch (state) {
case STATE_BUFFER:
return buf_size;
case STATE_SEQUENCE:
{
/* tell FFmpeg about sequence details */
dirac_sourceparams_t *src_params = &p_dirac_params->p_decoder->src_params;
if (avcodec_check_dimensions(avccontext, src_params->width,
src_params->height) < 0) {
av_log(avccontext, AV_LOG_ERROR, "Invalid dimensions (%dx%d)\n",
src_params->width, src_params->height);
avccontext->height = avccontext->width = 0;
return -1;
}
avccontext->height = src_params->height;
avccontext->width = src_params->width;
avccontext->pix_fmt = GetFfmpegChromaFormat(src_params->chroma);
if (avccontext->pix_fmt == PIX_FMT_NONE) {
av_log(avccontext, AV_LOG_ERROR,
"Dirac chroma format %d not supported currently\n",
src_params->chroma);
return -1;
}
avccontext->time_base.den = src_params->frame_rate.numerator;
avccontext->time_base.num = src_params->frame_rate.denominator;
/* calculate output dimensions */
avpicture_fill(&pic, NULL, avccontext->pix_fmt,
avccontext->width, avccontext->height);
pict_size = avpicture_get_size(avccontext->pix_fmt,
avccontext->width,
avccontext->height);
/* allocate output buffer */
if (!p_dirac_params->p_out_frame_buf)
p_dirac_params->p_out_frame_buf = av_malloc(pict_size);
buffer[0] = p_dirac_params->p_out_frame_buf;
buffer[1] = p_dirac_params->p_out_frame_buf +
pic.linesize[0] * avccontext->height;
buffer[2] = buffer[1] +
pic.linesize[1] * src_params->chroma_height;
/* tell Dirac about output destination */
dirac_set_buf(p_dirac_params->p_decoder, buffer, NULL);
break;
}
case STATE_SEQUENCE_END:
break;
case STATE_PICTURE_AVAIL:
/* fill picture with current buffer data from Dirac */
avpicture_fill(picture, p_dirac_params->p_out_frame_buf,
avccontext->pix_fmt,
avccontext->width, avccontext->height);
*data_size = sizeof(AVPicture);
return buf_size;
case STATE_INVALID:
return -1;
default:
break;
}
}
return buf_size;
}
static av_cold int libdirac_decode_close(AVCodecContext *avccontext)
{
FfmpegDiracDecoderParams *p_dirac_params = avccontext->priv_data;
dirac_decoder_close(p_dirac_params->p_decoder);
av_freep(&p_dirac_params->p_out_frame_buf);
return 0;
}
static void libdirac_flush(AVCodecContext *avccontext)
{
/* Got a seek request. We will need free memory held in the private
* context and free the current Dirac decoder handle and then open
* a new decoder handle. */
libdirac_decode_close(avccontext);
libdirac_decode_init(avccontext);
return;
}
AVCodec libdirac_decoder = {
"libdirac",
AVMEDIA_TYPE_VIDEO,
CODEC_ID_DIRAC,
sizeof(FfmpegDiracDecoderParams),
libdirac_decode_init,
NULL,
libdirac_decode_close,
libdirac_decode_frame,
CODEC_CAP_DELAY,
.flush = libdirac_flush,
.long_name = NULL_IF_CONFIG_SMALL("libdirac Dirac 2.2"),
};
| 123linslouis-android-video-cutter | jni/libavcodec/libdiracdec.c | C | asf20 | 6,847 |
/*
* VC3/DNxHD data.
* Copyright (c) 2007 SmartJog S.A., Baptiste Coudurier <baptiste dot coudurier at smartjog dot com>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "avcodec.h"
#include "dnxhddata.h"
static const uint8_t dnxhd_1237_luma_weight[] = {
0, 32, 33, 34, 34, 36, 37, 36,
36, 37, 38, 38, 38, 39, 41, 44,
43, 41, 40, 41, 46, 49, 47, 46,
47, 49, 51, 54, 60, 62, 59, 55,
54, 56, 58, 61, 65, 66, 64, 63,
66, 73, 78, 79, 80, 79, 78, 78,
82, 87, 89, 90, 93, 95, 96, 97,
97, 100, 104, 102, 98, 98, 99, 99,
};
static const uint8_t dnxhd_1237_chroma_weight[] = {
0, 32, 36, 39, 39, 38, 39, 41,
45, 51, 57, 58, 53, 48, 47, 51,
55, 58, 66, 75, 81, 83, 82, 78,
73, 72, 74, 77, 83, 85, 83, 82,
89, 99, 96, 90, 94, 97, 99, 105,
109, 105, 95, 89, 92, 95, 94, 93,
92, 88, 89, 90, 93, 95, 96, 97,
97, 100, 104, 102, 98, 98, 99, 99,
};
static const uint8_t dnxhd_1238_luma_weight[] = {
0, 32, 32, 33, 34, 33, 33, 33,
33, 33, 33, 33, 33, 35, 37, 37,
36, 36, 35, 36, 38, 38, 36, 35,
36, 37, 38, 41, 42, 41, 39, 38,
38, 38, 39, 41, 42, 41, 39, 39,
40, 41, 43, 44, 44, 44, 44, 44,
45, 47, 47, 47, 49, 50, 51, 51,
51, 53, 55, 57, 58, 59, 57, 57,
};
static const uint8_t dnxhd_1238_chroma_weight[] = {
0, 32, 35, 35, 35, 34, 34, 35,
39, 43, 45, 45, 41, 39, 40, 41,
42, 44, 48, 55, 59, 63, 65, 59,
53, 52, 52, 55, 61, 62, 58, 58,
63, 66, 66, 65, 70, 74, 70, 66,
65, 68, 75, 77, 74, 74, 77, 76,
73, 73, 73, 73, 76, 80, 89, 90,
82, 77, 80, 86, 84, 82, 82, 82,
};
static const uint8_t dnxhd_1241_luma_weight[] = {
0, 32, 33, 34, 34, 35, 36, 37,
36, 37, 38, 38, 38, 39, 39, 40,
40, 38, 38, 39, 38, 37, 39, 41,
41, 42, 43, 45, 45, 46, 47, 46,
45, 43, 39, 37, 37, 40, 44, 45,
45, 46, 46, 46, 47, 47, 46, 44,
42, 43, 45, 47, 48, 49, 50, 49,
48, 46, 47, 48, 48, 49, 49, 49,
};
static const uint8_t dnxhd_1241_chroma_weight[] = {
0, 32, 36, 38, 37, 37, 40, 41,
40, 40, 42, 42, 41, 41, 41, 41,
42, 43, 44, 44, 45, 46, 46, 45,
44, 45, 45, 45, 45, 46, 47, 46,
45, 44, 42, 41, 43, 45, 45, 47,
48, 48, 48, 46, 47, 47, 46, 47,
46, 45, 45, 47, 48, 49, 50, 49,
48, 46, 48, 49, 48, 49, 49, 49,
};
static const uint8_t dnxhd_1242_luma_weight[] = {
0, 32, 33, 33, 34, 35, 36, 35,
33, 33, 35, 36, 37, 37, 38, 37,
37, 37, 36, 37, 37, 37, 38, 39,
37, 36, 37, 40, 42, 45, 46, 44,
41, 42, 44, 45, 47, 49, 50, 48,
46, 48, 49, 50, 52, 52, 50, 49,
47, 48, 50, 50, 51, 51, 50, 49,
49, 51, 52, 51, 49, 47, 47, 47,
};
static const uint8_t dnxhd_1242_chroma_weight[] = {
0, 32, 37, 42, 45, 45, 45, 44,
38, 37, 40, 42, 44, 49, 51, 47,
41, 40, 43, 44, 46, 48, 51, 54,
51, 47, 47, 45, 47, 50, 51, 49,
46, 47, 49, 47, 50, 55, 55, 51,
48, 49, 51, 51, 52, 52, 54, 54,
49, 49, 52, 53, 54, 54, 53, 53,
55, 59, 63, 62, 60, 60, 60, 60,
};
static const uint8_t dnxhd_1243_luma_weight[] = {
0, 32, 32, 33, 33, 35, 35, 35,
35, 35, 35, 35, 34, 35, 38, 40,
39, 37, 37, 37, 36, 35, 36, 38,
40, 41, 42, 44, 45, 44, 42, 41,
40, 38, 36, 36, 37, 38, 40, 43,
44, 45, 45, 45, 45, 45, 45, 41,
39, 41, 45, 47, 47, 48, 48, 48,
46, 44, 45, 47, 47, 48, 47, 47,
};
static const uint8_t dnxhd_1243_chroma_weight[] = {
0, 32, 36, 37, 36, 37, 39, 39,
41, 43, 43, 42, 41, 41, 41, 42,
43, 43, 43, 44, 44, 44, 46, 47,
46, 45, 45, 45, 45, 46, 44, 44,
45, 44, 42, 41, 43, 46, 45, 44,
45, 45, 45, 46, 46, 46, 45, 44,
45, 44, 45, 47, 47, 48, 49, 48,
46, 45, 46, 47, 47, 48, 47, 47,
};
static const uint8_t dnxhd_1251_luma_weight[] = {
0, 32, 32, 34, 34, 34, 34, 35,
35, 35, 36, 37, 36, 36, 35, 36,
38, 38, 38, 38, 38, 38, 38, 38,
38, 38, 39, 41, 44, 43, 41, 40,
40, 40, 40, 39, 40, 41, 40, 39,
40, 43, 46, 46, 44, 44, 44, 42,
41, 43, 46, 48, 50, 55, 58, 53,
48, 50, 55, 58, 61, 62, 62, 62,
};
static const uint8_t dnxhd_1251_chroma_weight[] = {
0, 32, 35, 36, 36, 35, 36, 39,
41, 43, 45, 44, 41, 39, 40, 42,
43, 43, 45, 48, 48, 48, 50, 50,
50, 51, 51, 51, 51, 52, 53, 54,
51, 49, 51, 52, 52, 56, 57, 55,
54, 54, 55, 56, 55, 58, 58, 58,
60, 61, 62, 62, 59, 57, 58, 58,
61, 59, 59, 59, 61, 62, 62, 62,
};
static const uint8_t dnxhd_1252_luma_weight[] = {
0, 32, 34, 35, 36, 36, 36, 37,
36, 37, 39, 40, 41, 40, 40, 40,
41, 41, 42, 41, 41, 43, 44, 44,
45, 46, 48, 55, 60, 57, 52, 50,
49, 49, 52, 52, 53, 55, 58, 62,
65, 73, 82, 82, 80, 78, 73, 68,
71, 82, 90, 90, 88, 87, 90, 95,
100, 107, 103, 97, 95, 93, 99, 99,
};
static const uint8_t dnxhd_1252_chroma_weight[] = {
0, 32, 35, 36, 37, 37, 38, 40,
42, 46, 49, 50, 50, 49, 49, 53,
56, 56, 57, 58, 60, 62, 64, 65,
63, 64, 64, 65, 66, 65, 67, 71,
72, 74, 74, 74, 74, 77, 81, 78,
72, 73, 82, 85, 89, 88, 84, 80,
90, 100, 90, 90, 88, 87, 90, 95,
114, 128, 125, 129, 134, 125, 116, 116,
};
static const uint8_t dnxhd_1237_dc_codes[12] = {
0, 12, 13, 1, 2, 3, 4, 5, 14, 30, 62, 63,
};
static const uint8_t dnxhd_1237_dc_bits[12] = {
3, 4, 4, 3, 3, 3, 3, 3, 4, 5, 6, 6,
};
static const uint16_t dnxhd_1237_ac_codes[257] = {
0, 1, 4, 5, 12, 26, 27, 56, 57, 58, 59, 120, 121, 244, 245, 246, 247, 248, 498, 499, 500, 501, 502, 1006, 1007, 1008, 1009, 1010, 1011, 2024, 2025, 2026, 2027, 2028, 2029, 2030, 2031, 4064, 4065, 4066, 4067, 4068, 4069, 4070, 4071, 4072, 4073, 8148, 8149, 8150, 8151, 8152, 8153, 8154, 8155, 8156, 8157, 8158, 16318, 16319, 16320, 16321, 16322, 16323, 16324, 16325, 16326, 16327, 16328, 16329, 16330, 16331, 16332, 16333, 32668, 32669, 32670, 32671, 32672, 32673, 32674, 32675, 32676, 32677, 32678, 32679, 32680, 32681, 32682, 32683, 32684, 65370, 65371, 65372, 65373, 65374, 65375, 65376, 65377, 65378, 65379, 65380, 65381, 65382, 65383, 65384, 65385, 65386, 65387, 65388, 65389, 65390, 65391, 65392, 65393, 65394, 65395, 65396, 65397, 65398, 65399, 65400, 65401, 65402, 65403, 65404, 65405, 65406, 65407, 65408, 65409, 65410, 65411, 65412, 65413, 65414, 65415, 65416, 65417, 65418, 65419, 65420, 65421, 65422, 65423, 65424, 65425, 65426, 65427, 65428, 65429, 65430, 65431, 65432, 65433, 65434, 65435, 65436, 65437, 65438, 65439, 65440, 65441, 65442, 65443, 65444, 65445, 65446, 65447, 65448, 65449, 65450, 65451, 65452, 65453, 65454, 65455, 65456, 65457, 65458, 65459, 65460, 65461, 65462, 65463, 65464, 65465, 65466, 65467, 65468, 65469, 65470, 65471, 65472, 65473, 65474, 65475, 65476, 65477, 65478, 65479, 65480, 65481, 65482, 65483, 65484, 65485, 65486, 65487, 65488, 65489, 65490, 65491, 65492, 65493, 65494, 65495, 65496, 65497, 65498, 65499, 65500, 65501, 65502, 65503, 65504, 65505, 65506, 65507, 65508, 65509, 65510, 65511, 65512, 65513, 65514, 65515, 65516, 65517, 65518, 65519, 65520, 65521, 65522, 65523, 65524, 65525, 65526, 65527, 65528, 65529, 65530, 65531, 65532, 65533, 65534, 65535,
};
static const uint8_t dnxhd_1237_ac_bits[257] = {
2, 2, 3, 3, 4, 5, 5, 6, 6, 6, 6, 7, 7, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
};
static const uint8_t dnxhd_1237_ac_level[257] = {
1, 1, 2, 0, 3, 4, 2, 5, 6, 7, 3, 8, 9, 10, 11, 12, 4, 5, 13, 14, 15, 16, 6, 17, 18, 19, 20, 21, 7, 22, 23, 24, 25, 26, 27, 8, 9, 28, 29, 30, 31, 32, 33, 34, 10, 11, 12, 35, 36, 37, 38, 39, 40, 41, 13, 14, 15, 16, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 17, 18, 19, 20, 21, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 1, 22, 23, 24, 25, 26, 27, 62, 63, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64,
};
static const uint8_t dnxhd_1237_ac_run_flag[257] = {
0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
};
static const uint8_t dnxhd_1237_ac_index_flag[257] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
};
static const uint16_t dnxhd_1237_run_codes[62] = {
0, 4, 10, 11, 24, 25, 26, 54, 55, 56, 57, 58, 118, 119, 240, 482, 483, 484, 485, 486, 487, 488, 489, 490, 491, 492, 493, 494, 990, 991, 992, 993, 994, 995, 996, 997, 998, 999, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010, 1011, 1012, 1013, 1014, 1015, 1016, 1017, 1018, 1019, 1020, 1021, 1022, 1023,
};
static const uint8_t dnxhd_1237_run_bits[62] = {
1, 3, 4, 4, 5, 5, 5, 6, 6, 6, 6, 6, 7, 7, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
};
static const uint8_t dnxhd_1237_run[62] = {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 53, 57, 58, 59, 60, 61, 62, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 54, 55, 56,
};
static const uint8_t dnxhd_1238_dc_codes[12] = {
0, 12, 13, 1, 2, 3, 4, 5, 14, 30, 62, 63,
};
static const uint8_t dnxhd_1238_dc_bits[12] = {
3, 4, 4, 3, 3, 3, 3, 3, 4, 5, 6, 6,
};
static const uint16_t dnxhd_1238_ac_codes[257] = {
0, 1, 4, 10, 11, 24, 25, 26, 54, 55, 56, 57, 116, 117, 118, 119, 240, 241, 242, 243, 244, 245, 492, 493, 494, 495, 496, 497, 498, 499, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 2018, 2019, 2020, 2021, 2022, 2023, 2024, 2025, 2026, 2027, 4056, 4057, 4058, 4059, 4060, 4061, 4062, 4063, 4064, 4065, 4066, 4067, 4068, 4069, 8140, 8141, 8142, 8143, 8144, 8145, 8146, 8147, 8148, 8149, 8150, 8151, 8152, 8153, 8154, 8155, 8156, 16314, 16315, 16316, 16317, 16318, 16319, 16320, 16321, 16322, 16323, 16324, 16325, 16326, 16327, 16328, 16329, 16330, 16331, 16332, 16333, 16334, 16335, 16336, 16337, 16338, 32678, 32679, 32680, 32681, 32682, 32683, 32684, 32685, 32686, 32687, 32688, 32689, 32690, 32691, 32692, 32693, 32694, 32695, 32696, 32697, 32698, 32699, 32700, 32701, 32702, 32703, 32704, 32705, 65412, 65413, 65414, 65415, 65416, 65417, 65418, 65419, 65420, 65421, 65422, 65423, 65424, 65425, 65426, 65427, 65428, 65429, 65430, 65431, 65432, 65433, 65434, 65435, 65436, 65437, 65438, 65439, 65440, 65441, 65442, 65443, 65444, 65445, 65446, 65447, 65448, 65449, 65450, 65451, 65452, 65453, 65454, 65455, 65456, 65457, 65458, 65459, 65460, 65461, 65462, 65463, 65464, 65465, 65466, 65467, 65468, 65469, 65470, 65471, 65472, 65473, 65474, 65475, 65476, 65477, 65478, 65479, 65480, 65481, 65482, 65483, 65484, 65485, 65486, 65487, 65488, 65489, 65490, 65491, 65492, 65493, 65494, 65495, 65496, 65497, 65498, 65499, 65500, 65501, 65502, 65503, 65504, 65505, 65506, 65507, 65508, 65509, 65510, 65511, 65512, 65513, 65514, 65515, 65516, 65517, 65518, 65519, 65520, 65521, 65522, 65523, 65524, 65525, 65526, 65527, 65528, 65529, 65530, 65531, 65532, 65533, 65534, 65535,
};
static const uint8_t dnxhd_1238_ac_bits[257] = {
2, 2, 3, 4, 4, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
};
static const uint8_t dnxhd_1238_ac_level[257] = {
1, 1, 2, 3, 0, 4, 5, 2, 6, 7, 8, 3, 9, 10, 11, 4, 12, 13, 14, 15, 16, 5, 17, 18, 19, 20, 21, 22, 6, 7, 23, 24, 25, 26, 27, 28, 29, 8, 9, 30, 31, 32, 33, 34, 35, 36, 37, 10, 11, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 12, 13, 14, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 15, 16, 17, 18, 62, 63, 64, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 19, 20, 21, 22, 23, 24, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 40, 25, 26, 27, 28, 29, 30, 38, 39, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64,
}; /* 0 is EOB */
static const uint8_t dnxhd_1238_ac_run_flag[257] = {
0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
};
static const uint8_t dnxhd_1238_ac_index_flag[257] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
};
static const uint16_t dnxhd_1238_run_codes[62] = {
0, 4, 10, 11, 24, 25, 26, 27, 56, 57, 58, 59, 120, 242, 486, 487, 488, 489, 980, 981, 982, 983, 984, 985, 986, 987, 988, 989, 990, 991, 992, 993, 994, 995, 996, 997, 998, 999, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010, 1011, 1012, 1013, 1014, 1015, 1016, 1017, 1018, 1019, 1020, 1021, 1022, 1023,
};
static const uint8_t dnxhd_1238_run_bits[62] = {
1, 3, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 8, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
};
static const uint8_t dnxhd_1238_run[62] = {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 20, 21, 17, 18, 19, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62,
};
static const uint8_t dnxhd_1241_dc_codes[14] = {
10, 62, 11, 12, 13, 0, 1, 2, 3, 4, 14, 30, 126, 127,
};
static const uint8_t dnxhd_1241_dc_bits[14] = {
4, 6, 4, 4, 4, 3, 3, 3, 3, 3, 4, 5, 7, 7,
};
static const uint16_t dnxhd_1241_ac_codes[257] = {
0, 1, 4, 10, 11, 24, 25, 26, 54, 55, 56, 57, 116, 117, 118, 119, 240, 241, 242, 243, 244, 245, 492, 493, 494, 495, 496, 497, 498, 998, 999, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024, 2025, 2026, 4054, 4055, 4056, 4057, 4058, 4059, 4060, 4061, 4062, 4063, 4064, 4065, 4066, 4067, 4068, 4069, 8140, 8141, 8142, 8143, 8144, 8145, 8146, 8147, 8148, 8149, 8150, 8151, 8152, 8153, 8154, 8155, 8156, 8157, 16316, 16317, 16318, 16319, 16320, 16321, 16322, 16323, 16324, 16325, 16326, 16327, 16328, 16329, 16330, 16331, 16332, 16333, 16334, 16335, 16336, 16337, 32676, 32677, 32678, 32679, 32680, 32681, 32682, 32683, 32684, 32685, 32686, 32687, 32688, 32689, 32690, 32691, 32692, 32693, 32694, 32695, 32696, 32697, 32698, 32699, 32700, 32701, 32702, 32703, 32704, 32705, 32706, 32707, 32708, 65418, 65419, 65420, 65421, 65422, 65423, 65424, 65425, 65426, 65427, 65428, 65429, 65430, 65431, 65432, 65433, 65434, 65435, 65436, 65437, 65438, 65439, 65440, 65441, 65442, 65443, 65444, 65445, 65446, 65447, 65448, 65449, 65450, 65451, 65452, 65453, 65454, 65455, 65456, 65457, 65458, 65459, 65460, 65461, 65462, 65463, 65464, 65465, 65466, 65467, 65468, 65469, 65470, 65471, 65472, 65473, 65474, 65475, 65476, 65477, 65478, 65479, 65480, 65481, 65482, 65483, 65484, 65485, 65486, 65487, 65488, 65489, 65490, 65491, 65492, 65493, 65494, 65495, 65496, 65497, 65498, 65499, 65500, 65501, 65502, 65503, 65504, 65505, 65506, 65507, 65508, 65509, 65510, 65511, 65512, 65513, 65514, 65515, 65516, 65517, 65518, 65519, 65520, 65521, 65522, 65523, 65524, 65525, 65526, 65527, 65528, 65529, 65530, 65531, 65532, 65533, 65534, 65535,
};
static const uint8_t dnxhd_1241_ac_bits[257] = {
2, 2, 3, 4, 4, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
};
static const uint8_t dnxhd_1241_ac_level[257] = {
1, 1, 2, 3, 0, 4, 5, 2, 6, 7, 8, 3, 9, 10, 11, 4, 12, 13, 14, 15, 16, 5, 17, 18, 19, 20, 21, 6, 7, 22, 23, 24, 25, 26, 27, 28, 29, 8, 9, 30, 31, 32, 33, 34, 35, 36, 37, 38, 10, 11, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 12, 13, 14, 15, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 1, 16, 17, 18, 19, 64, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 20, 21, 22, 23, 24, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 25, 26, 27, 28, 29, 30, 31, 32, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64,
};
static const uint8_t dnxhd_1241_ac_run_flag[257] = {
0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
};
static const uint8_t dnxhd_1241_ac_index_flag[257] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
};
static const uint8_t dnxhd_1241_run[62] = {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 20, 17, 19, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62,
};
static const uint8_t dnxhd_1251_dc_codes[12] = {
0, 12, 13, 1, 2, 3, 4, 5, 14, 30, 62, 63,
};
static const uint8_t dnxhd_1251_dc_bits[12] = {
3, 4, 4, 3, 3, 3, 3, 3, 4, 5, 6, 6,
};
static const uint16_t dnxhd_1251_ac_codes[257] = {
0, 1, 4, 10, 11, 24, 25, 26, 54, 55, 56, 57, 116, 117, 118, 119, 240, 241, 242, 243, 244, 245, 492, 493, 494, 495, 496, 497, 996, 997, 998, 999, 1000, 1001, 1002, 1003, 1004, 1005, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024, 2025, 4052, 4053, 4054, 4055, 4056, 4057, 4058, 4059, 4060, 4061, 4062, 4063, 4064, 4065, 4066, 8134, 8135, 8136, 8137, 8138, 8139, 8140, 8141, 8142, 8143, 8144, 8145, 8146, 8147, 8148, 8149, 8150, 8151, 8152, 8153, 8154, 8155, 8156, 16314, 16315, 16316, 16317, 16318, 16319, 16320, 16321, 16322, 16323, 16324, 16325, 16326, 16327, 16328, 16329, 16330, 16331, 16332, 16333, 16334, 16335, 16336, 16337, 16338, 16339, 32680, 32681, 32682, 32683, 32684, 32685, 32686, 32687, 32688, 32689, 32690, 32691, 32692, 32693, 32694, 32695, 32696, 32697, 32698, 32699, 32700, 32701, 32702, 32703, 32704, 32705, 32706, 32707, 32708, 32709, 32710, 32711, 32712, 32713, 32714, 65430, 65431, 65432, 65433, 65434, 65435, 65436, 65437, 65438, 65439, 65440, 65441, 65442, 65443, 65444, 65445, 65446, 65447, 65448, 65449, 65450, 65451, 65452, 65453, 65454, 65455, 65456, 65457, 65458, 65459, 65460, 65461, 65462, 65463, 65464, 65465, 65466, 65467, 65468, 65469, 65470, 65471, 65472, 65473, 65474, 65475, 65476, 65477, 65478, 65479, 65480, 65481, 65482, 65483, 65484, 65485, 65486, 65487, 65488, 65489, 65490, 65491, 65492, 65493, 65494, 65495, 65496, 65497, 65498, 65499, 65500, 65501, 65502, 65503, 65504, 65505, 65506, 65507, 65508, 65509, 65510, 65511, 65512, 65513, 65514, 65515, 65516, 65517, 65518, 65519, 65520, 65521, 65522, 65523, 65524, 65525, 65526, 65527, 65528, 65529, 65530, 65531, 65532, 65533, 65534, 65535,
};
static const uint8_t dnxhd_1251_ac_bits[257] = {
2, 2, 3, 4, 4, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
};
static const uint8_t dnxhd_1251_ac_level[257] = {
1, 1, 2, 3, 0, 4, 5, 2, 6, 7, 8, 3, 9, 10, 11, 4, 12, 13, 14, 15, 16, 5, 17, 18, 19, 20, 21, 6, 22, 23, 24, 25, 26, 27, 28, 29, 7, 8, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 9, 10, 11, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 12, 13, 14, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 1, 2, 3, 4, 5, 6, 7, 8, 15, 16, 17, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 18, 19, 20, 21, 22, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 23, 24, 25, 26, 27, 28, 59, 60, 61, 62, 63, 64, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64,
};
static const uint8_t dnxhd_1251_ac_run_flag[257] = {
0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
};
static const uint8_t dnxhd_1251_ac_index_flag[257] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
};
static const uint16_t dnxhd_1251_run_codes[62] = {
0, 4, 5, 12, 26, 27, 28, 58, 118, 119, 120, 242, 486, 487, 976, 977, 978, 979, 980, 981, 982, 983, 984, 985, 986, 987, 988, 989, 990, 991, 992, 993, 994, 995, 996, 997, 998, 999, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010, 1011, 1012, 1013, 1014, 1015, 1016, 1017, 1018, 1019, 1020, 1021, 1022, 1023,
};
static const uint8_t dnxhd_1251_run_bits[62] = {
1, 3, 3, 4, 5, 5, 5, 6, 7, 7, 7, 8, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
};
static const uint8_t dnxhd_1251_run[62] = {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62,
};
static const uint8_t dnxhd_1252_dc_codes[12] = {
0, 12, 13, 1, 2, 3, 4, 5, 14, 30, 62, 63,
};
static const uint8_t dnxhd_1252_dc_bits[12] = {
3, 4, 4, 3, 3, 3, 3, 3, 4, 5, 6, 6,
};
static const uint16_t dnxhd_1252_ac_codes[257] = {
0, 1, 4, 10, 11, 12, 26, 27, 56, 57, 58, 118, 119, 120, 242, 243, 244, 245, 246, 247, 496, 497, 498, 499, 500, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 2020, 2021, 2022, 2023, 2024, 2025, 2026, 2027, 2028, 2029, 4060, 4061, 4062, 4063, 4064, 4065, 4066, 4067, 4068, 4069, 4070, 4071, 8144, 8145, 8146, 8147, 8148, 8149, 8150, 8151, 8152, 8153, 8154, 8155, 8156, 8157, 8158, 16318, 16319, 16320, 16321, 16322, 16323, 16324, 16325, 16326, 16327, 16328, 16329, 16330, 16331, 16332, 16333, 16334, 16335, 32672, 32673, 32674, 32675, 32676, 32677, 32678, 32679, 32680, 32681, 32682, 32683, 32684, 32685, 32686, 32687, 32688, 32689, 32690, 32691, 32692, 32693, 32694, 65390, 65391, 65392, 65393, 65394, 65395, 65396, 65397, 65398, 65399, 65400, 65401, 65402, 65403, 65404, 65405, 65406, 65407, 65408, 65409, 65410, 65411, 65412, 65413, 65414, 65415, 65416, 65417, 65418, 65419, 65420, 65421, 65422, 65423, 65424, 65425, 65426, 65427, 65428, 65429, 65430, 65431, 65432, 65433, 65434, 65435, 65436, 65437, 65438, 65439, 65440, 65441, 65442, 65443, 65444, 65445, 65446, 65447, 65448, 65449, 65450, 65451, 65452, 65453, 65454, 65455, 65456, 65457, 65458, 65459, 65460, 65461, 65462, 65463, 65464, 65465, 65466, 65467, 65468, 65469, 65470, 65471, 65472, 65473, 65474, 65475, 65476, 65477, 65478, 65479, 65480, 65481, 65482, 65483, 65484, 65485, 65486, 65487, 65488, 65489, 65490, 65491, 65492, 65493, 65494, 65495, 65496, 65497, 65498, 65499, 65500, 65501, 65502, 65503, 65504, 65505, 65506, 65507, 65508, 65509, 65510, 65511, 65512, 65513, 65514, 65515, 65516, 65517, 65518, 65519, 65520, 65521, 65522, 65523, 65524, 65525, 65526, 65527, 65528, 65529, 65530, 65531, 65532, 65533, 65534, 65535,
};
static const uint8_t dnxhd_1252_ac_bits[257] = {
2, 2, 3, 4, 4, 4, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
};
static const uint8_t dnxhd_1252_ac_level[257] = {
1, 1, 2, 3, 2, 0, 4, 5, 6, 7, 3, 8, 9, 10, 11, 12, 13, 14, 4, 5, 15, 16, 17, 18, 6, 19, 20, 21, 22, 23, 24, 7, 8, 25, 26, 27, 28, 29, 30, 31, 32, 9, 10, 33, 34, 35, 36, 37, 38, 39, 40, 41, 11, 12, 13, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 14, 15, 16, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 1, 2, 3, 17, 18, 19, 20, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 21, 22, 23, 24, 25, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64,
};
static const uint8_t dnxhd_1252_ac_run_flag[257] = {
0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
};
static const uint8_t dnxhd_1252_ac_index_flag[257] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
};
const CIDEntry ff_dnxhd_cid_table[] = {
{ 1237, 1920, 1080, 0, 606208, 606208, 4, 8,
dnxhd_1237_luma_weight, dnxhd_1237_chroma_weight,
dnxhd_1237_dc_codes, dnxhd_1237_dc_bits,
dnxhd_1237_ac_codes, dnxhd_1237_ac_bits, dnxhd_1237_ac_level,
dnxhd_1237_ac_run_flag, dnxhd_1237_ac_index_flag,
dnxhd_1237_run_codes, dnxhd_1237_run_bits, dnxhd_1237_run,
{ 115, 120, 145, 240, 290 } },
{ 1238, 1920, 1080, 0, 917504, 917504, 4, 8,
dnxhd_1238_luma_weight, dnxhd_1238_chroma_weight,
dnxhd_1238_dc_codes, dnxhd_1238_dc_bits,
dnxhd_1238_ac_codes, dnxhd_1238_ac_bits, dnxhd_1238_ac_level,
dnxhd_1238_ac_run_flag, dnxhd_1238_ac_index_flag,
dnxhd_1238_run_codes, dnxhd_1238_run_bits, dnxhd_1238_run,
{ 175, 185, 220, 365, 440 } },
{ 1241, 1920, 1080, 1, 917504, 458752, 6, 10,
dnxhd_1241_luma_weight, dnxhd_1241_chroma_weight,
dnxhd_1241_dc_codes, dnxhd_1241_dc_bits,
dnxhd_1241_ac_codes, dnxhd_1241_ac_bits, dnxhd_1241_ac_level,
dnxhd_1241_ac_run_flag, dnxhd_1241_ac_index_flag,
dnxhd_1238_run_codes, dnxhd_1238_run_bits, dnxhd_1241_run,
{ 185, 220 } },
{ 1242, 1920, 1080, 1, 606208, 303104, 4, 8,
dnxhd_1242_luma_weight, dnxhd_1242_chroma_weight,
dnxhd_1237_dc_codes, dnxhd_1237_dc_bits,
dnxhd_1237_ac_codes, dnxhd_1237_ac_bits, dnxhd_1237_ac_level,
dnxhd_1237_ac_run_flag, dnxhd_1237_ac_index_flag,
dnxhd_1237_run_codes, dnxhd_1237_run_bits, dnxhd_1237_run,
{ 120, 145 } },
{ 1243, 1920, 1080, 1, 917504, 458752, 4, 8,
dnxhd_1243_luma_weight, dnxhd_1243_chroma_weight,
dnxhd_1238_dc_codes, dnxhd_1238_dc_bits,
dnxhd_1238_ac_codes, dnxhd_1238_ac_bits, dnxhd_1238_ac_level,
dnxhd_1238_ac_run_flag, dnxhd_1238_ac_index_flag,
dnxhd_1238_run_codes, dnxhd_1238_run_bits, dnxhd_1238_run,
{ 185, 220 } },
{ 1251, 1280, 720, 0, 458752, 458752, 4, 8,
dnxhd_1251_luma_weight, dnxhd_1251_chroma_weight,
dnxhd_1251_dc_codes, dnxhd_1251_dc_bits,
dnxhd_1251_ac_codes, dnxhd_1251_ac_bits, dnxhd_1251_ac_level,
dnxhd_1251_ac_run_flag, dnxhd_1251_ac_index_flag,
dnxhd_1251_run_codes, dnxhd_1251_run_bits, dnxhd_1251_run,
{ 90, 110, 175, 220 } },
{ 1252, 1280, 720, 0, 303104, 303104, 4, 8,
dnxhd_1252_luma_weight, dnxhd_1252_chroma_weight,
dnxhd_1252_dc_codes, dnxhd_1252_dc_bits,
dnxhd_1252_ac_codes, dnxhd_1252_ac_bits, dnxhd_1252_ac_level,
dnxhd_1252_ac_run_flag, dnxhd_1252_ac_index_flag,
dnxhd_1251_run_codes, dnxhd_1251_run_bits, dnxhd_1251_run,
{ 60, 75, 115, 145 } },
{ 1253, 1920, 1080, 0, 188416, 188416, 4, 8,
dnxhd_1237_luma_weight, dnxhd_1237_chroma_weight,
dnxhd_1237_dc_codes, dnxhd_1237_dc_bits,
dnxhd_1237_ac_codes, dnxhd_1237_ac_bits, dnxhd_1237_ac_level,
dnxhd_1237_ac_run_flag, dnxhd_1237_ac_index_flag,
dnxhd_1237_run_codes, dnxhd_1237_run_bits, dnxhd_1237_run,
{ 36, 45, 75, 90 } },
};
int ff_dnxhd_get_cid_table(int cid)
{
int i;
for (i = 0; i < FF_ARRAY_ELEMS(ff_dnxhd_cid_table); i++)
if (ff_dnxhd_cid_table[i].cid == cid)
return i;
return -1;
}
int ff_dnxhd_find_cid(AVCodecContext *avctx)
{
int i, j;
int mbs = avctx->bit_rate/1000000;
if (!mbs)
return 0;
for (i = 0; i < FF_ARRAY_ELEMS(ff_dnxhd_cid_table); i++) {
const CIDEntry *cid = &ff_dnxhd_cid_table[i];
if (cid->width == avctx->width && cid->height == avctx->height &&
cid->interlaced == !!(avctx->flags & CODEC_FLAG_INTERLACED_DCT) &&
cid->bit_depth == 8) { // until 10 bit is supported
for (j = 0; j < sizeof(cid->bit_rates); j++) {
if (cid->bit_rates[j] == mbs)
return cid->cid;
}
}
}
return 0;
}
| 123linslouis-android-video-cutter | jni/libavcodec/dnxhddata.c | C | asf20 | 41,418 |
/*
* Atrac 1 compatible decoder data
* Copyright (c) 2009 Maxim Poliakovski
* Copyright (c) 2009 Benjamin Larsson
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* Atrac 1 compatible decoder data
*/
#ifndef AVCODEC_ATRAC1DATA_H
#define AVCODEC_ATRAC1DATA_H
#include <stdint.h>
static const uint8_t bfu_amount_tab1[8] = {20, 28, 32, 36, 40, 44, 48, 52};
static const uint8_t bfu_amount_tab2[4] = { 0, 112, 176, 208};
static const uint8_t bfu_amount_tab3[8] = { 0, 24, 36, 48, 72, 108, 132, 156};
/** number of BFUs in each QMF band */
static const uint8_t bfu_bands_t[4] = {0, 20, 36, 52};
/** number of spectral lines in each BFU
* block floating unit = group of spectral frequencies having the
* same quantization parameters like word length and scale factor
*/
static const uint8_t specs_per_bfu[52] = {
8, 8, 8, 8, 4, 4, 4, 4, 8, 8, 8, 8, 6, 6, 6, 6, 6, 6, 6, 6, // low band
6, 6, 6, 6, 7, 7, 7, 7, 9, 9, 9, 9, 10, 10, 10, 10, // midle band
12, 12, 12, 12, 12, 12, 12, 12, 20, 20, 20, 20, 20, 20, 20, 20 // high band
};
/** start position of each BFU in the MDCT spectrum for the long mode */
static const uint16_t bfu_start_long[52] = {
0, 8, 16, 24, 32, 36, 40, 44, 48, 56, 64, 72, 80, 86, 92, 98, 104, 110, 116, 122,
128, 134, 140, 146, 152, 159, 166, 173, 180, 189, 198, 207, 216, 226, 236, 246,
256, 268, 280, 292, 304, 316, 328, 340, 352, 372, 392, 412, 432, 452, 472, 492,
};
/** start position of each BFU in the MDCT spectrum for the short mode */
static const uint16_t bfu_start_short[52] = {
0, 32, 64, 96, 8, 40, 72, 104, 12, 44, 76, 108, 20, 52, 84, 116, 26, 58, 90, 122,
128, 160, 192, 224, 134, 166, 198, 230, 141, 173, 205, 237, 150, 182, 214, 246,
256, 288, 320, 352, 384, 416, 448, 480, 268, 300, 332, 364, 396, 428, 460, 492
};
#endif /* AVCODEC_ATRAC1DATA_H */
| 123linslouis-android-video-cutter | jni/libavcodec/atrac1data.h | C | asf20 | 2,660 |
/*
* various filters for ACELP-based codecs
*
* Copyright (c) 2008 Vladimir Voroshilov
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef AVCODEC_ACELP_FILTERS_H
#define AVCODEC_ACELP_FILTERS_H
#include <stdint.h>
/**
* low-pass Finite Impulse Response filter coefficients.
*
* Hamming windowed sinc filter with cutoff freq 3/40 of the sampling freq,
* the coefficients are scaled by 2^15.
* This array only contains the right half of the filter.
* This filter is likely identical to the one used in G.729, though this
* could not be determined from the original comments with certainity.
*/
extern const int16_t ff_acelp_interp_filter[61];
/**
* Generic FIR interpolation routine.
* @param out [out] buffer for interpolated data
* @param in input data
* @param filter_coeffs interpolation filter coefficients (0.15)
* @param precision sub sample factor, that is the precision of the position
* @param frac_pos fractional part of position [0..precision-1]
* @param filter_length filter length
* @param length length of output
*
* filter_coeffs contains coefficients of the right half of the symmetric
* interpolation filter. filter_coeffs[0] should the central (unpaired) coefficient.
* See ff_acelp_interp_filter for an example.
*
*/
void ff_acelp_interpolate(int16_t* out, const int16_t* in,
const int16_t* filter_coeffs, int precision,
int frac_pos, int filter_length, int length);
/**
* Floating point version of ff_acelp_interpolate()
*/
void ff_acelp_interpolatef(float *out, const float *in,
const float *filter_coeffs, int precision,
int frac_pos, int filter_length, int length);
/**
* high-pass filtering and upscaling (4.2.5 of G.729).
* @param out [out] output buffer for filtered speech data
* @param hpf_f [in/out] past filtered data from previous (2 items long)
* frames (-0x20000000 <= (14.13) < 0x20000000)
* @param in speech data to process
* @param length input data size
*
* out[i] = 0.93980581 * in[i] - 1.8795834 * in[i-1] + 0.93980581 * in[i-2] +
* 1.9330735 * out[i-1] - 0.93589199 * out[i-2]
*
* The filter has a cut-off frequency of 1/80 of the sampling freq
*
* @note Two items before the top of the out buffer must contain two items from the
* tail of the previous subframe.
*
* @remark It is safe to pass the same array in in and out parameters.
*
* @remark AMR uses mostly the same filter (cut-off frequency 60Hz, same formula,
* but constants differs in 5th sign after comma). Fortunately in
* fixed-point all coefficients are the same as in G.729. Thus this
* routine can be used for the fixed-point AMR decoder, too.
*/
void ff_acelp_high_pass_filter(int16_t* out, int hpf_f[2],
const int16_t* in, int length);
/**
* Apply an order 2 rational transfer function in-place.
*
* @param out output buffer for filtered speech samples
* @param in input buffer containing speech data (may be the same as out)
* @param zero_coeffs z^-1 and z^-2 coefficients of the numerator
* @param pole_coeffs z^-1 and z^-2 coefficients of the denominator
* @param gain scale factor for final output
* @param mem intermediate values used by filter (should be 0 initially)
* @param n number of samples
*/
void ff_acelp_apply_order_2_transfer_function(float *out, const float *in,
const float zero_coeffs[2],
const float pole_coeffs[2],
float gain,
float mem[2], int n);
/**
* Apply tilt compensation filter, 1 - tilt * z-1.
*
* @param mem pointer to the filter's state (one single float)
* @param tilt tilt factor
* @param samples array where the filter is applied
* @param size the size of the samples array
*/
void ff_tilt_compensation(float *mem, float tilt, float *samples, int size);
#endif /* AVCODEC_ACELP_FILTERS_H */
| 123linslouis-android-video-cutter | jni/libavcodec/acelp_filters.h | C | asf20 | 4,813 |
/*
* MPEG-4 ALS decoder
* Copyright (c) 2009 Thilo Borgmann <thilo.borgmann _at_ googlemail.com>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* MPEG-4 ALS decoder
* @author Thilo Borgmann <thilo.borgmann _at_ googlemail.com>
*/
//#define DEBUG
#include "avcodec.h"
#include "get_bits.h"
#include "unary.h"
#include "mpeg4audio.h"
#include "bytestream.h"
#include "bgmc.h"
#include <stdint.h>
/** Rice parameters and corresponding index offsets for decoding the
* indices of scaled PARCOR values. The table choosen is set globally
* by the encoder and stored in ALSSpecificConfig.
*/
static const int8_t parcor_rice_table[3][20][2] = {
{ {-52, 4}, {-29, 5}, {-31, 4}, { 19, 4}, {-16, 4},
{ 12, 3}, { -7, 3}, { 9, 3}, { -5, 3}, { 6, 3},
{ -4, 3}, { 3, 3}, { -3, 2}, { 3, 2}, { -2, 2},
{ 3, 2}, { -1, 2}, { 2, 2}, { -1, 2}, { 2, 2} },
{ {-58, 3}, {-42, 4}, {-46, 4}, { 37, 5}, {-36, 4},
{ 29, 4}, {-29, 4}, { 25, 4}, {-23, 4}, { 20, 4},
{-17, 4}, { 16, 4}, {-12, 4}, { 12, 3}, {-10, 4},
{ 7, 3}, { -4, 4}, { 3, 3}, { -1, 3}, { 1, 3} },
{ {-59, 3}, {-45, 5}, {-50, 4}, { 38, 4}, {-39, 4},
{ 32, 4}, {-30, 4}, { 25, 3}, {-23, 3}, { 20, 3},
{-20, 3}, { 16, 3}, {-13, 3}, { 10, 3}, { -7, 3},
{ 3, 3}, { 0, 3}, { -1, 3}, { 2, 3}, { -1, 2} }
};
/** Scaled PARCOR values used for the first two PARCOR coefficients.
* To be indexed by the Rice coded indices.
* Generated by: parcor_scaled_values[i] = 32 + ((i * (i+1)) << 7) - (1 << 20)
* Actual values are divided by 32 in order to be stored in 16 bits.
*/
static const int16_t parcor_scaled_values[] = {
-1048544 / 32, -1048288 / 32, -1047776 / 32, -1047008 / 32,
-1045984 / 32, -1044704 / 32, -1043168 / 32, -1041376 / 32,
-1039328 / 32, -1037024 / 32, -1034464 / 32, -1031648 / 32,
-1028576 / 32, -1025248 / 32, -1021664 / 32, -1017824 / 32,
-1013728 / 32, -1009376 / 32, -1004768 / 32, -999904 / 32,
-994784 / 32, -989408 / 32, -983776 / 32, -977888 / 32,
-971744 / 32, -965344 / 32, -958688 / 32, -951776 / 32,
-944608 / 32, -937184 / 32, -929504 / 32, -921568 / 32,
-913376 / 32, -904928 / 32, -896224 / 32, -887264 / 32,
-878048 / 32, -868576 / 32, -858848 / 32, -848864 / 32,
-838624 / 32, -828128 / 32, -817376 / 32, -806368 / 32,
-795104 / 32, -783584 / 32, -771808 / 32, -759776 / 32,
-747488 / 32, -734944 / 32, -722144 / 32, -709088 / 32,
-695776 / 32, -682208 / 32, -668384 / 32, -654304 / 32,
-639968 / 32, -625376 / 32, -610528 / 32, -595424 / 32,
-580064 / 32, -564448 / 32, -548576 / 32, -532448 / 32,
-516064 / 32, -499424 / 32, -482528 / 32, -465376 / 32,
-447968 / 32, -430304 / 32, -412384 / 32, -394208 / 32,
-375776 / 32, -357088 / 32, -338144 / 32, -318944 / 32,
-299488 / 32, -279776 / 32, -259808 / 32, -239584 / 32,
-219104 / 32, -198368 / 32, -177376 / 32, -156128 / 32,
-134624 / 32, -112864 / 32, -90848 / 32, -68576 / 32,
-46048 / 32, -23264 / 32, -224 / 32, 23072 / 32,
46624 / 32, 70432 / 32, 94496 / 32, 118816 / 32,
143392 / 32, 168224 / 32, 193312 / 32, 218656 / 32,
244256 / 32, 270112 / 32, 296224 / 32, 322592 / 32,
349216 / 32, 376096 / 32, 403232 / 32, 430624 / 32,
458272 / 32, 486176 / 32, 514336 / 32, 542752 / 32,
571424 / 32, 600352 / 32, 629536 / 32, 658976 / 32,
688672 / 32, 718624 / 32, 748832 / 32, 779296 / 32,
810016 / 32, 840992 / 32, 872224 / 32, 903712 / 32,
935456 / 32, 967456 / 32, 999712 / 32, 1032224 / 32
};
/** Gain values of p(0) for long-term prediction.
* To be indexed by the Rice coded indices.
*/
static const uint8_t ltp_gain_values [4][4] = {
{ 0, 8, 16, 24},
{32, 40, 48, 56},
{64, 70, 76, 82},
{88, 92, 96, 100}
};
/** Inter-channel weighting factors for multi-channel correlation.
* To be indexed by the Rice coded indices.
*/
static const int16_t mcc_weightings[] = {
204, 192, 179, 166, 153, 140, 128, 115,
102, 89, 76, 64, 51, 38, 25, 12,
0, -12, -25, -38, -51, -64, -76, -89,
-102, -115, -128, -140, -153, -166, -179, -192
};
/** Tail codes used in arithmetic coding using block Gilbert-Moore codes.
*/
static const uint8_t tail_code[16][6] = {
{ 74, 44, 25, 13, 7, 3},
{ 68, 42, 24, 13, 7, 3},
{ 58, 39, 23, 13, 7, 3},
{126, 70, 37, 19, 10, 5},
{132, 70, 37, 20, 10, 5},
{124, 70, 38, 20, 10, 5},
{120, 69, 37, 20, 11, 5},
{116, 67, 37, 20, 11, 5},
{108, 66, 36, 20, 10, 5},
{102, 62, 36, 20, 10, 5},
{ 88, 58, 34, 19, 10, 5},
{162, 89, 49, 25, 13, 7},
{156, 87, 49, 26, 14, 7},
{150, 86, 47, 26, 14, 7},
{142, 84, 47, 26, 14, 7},
{131, 79, 46, 26, 14, 7}
};
enum RA_Flag {
RA_FLAG_NONE,
RA_FLAG_FRAMES,
RA_FLAG_HEADER
};
typedef struct {
uint32_t samples; ///< number of samples, 0xFFFFFFFF if unknown
int resolution; ///< 000 = 8-bit; 001 = 16-bit; 010 = 24-bit; 011 = 32-bit
int floating; ///< 1 = IEEE 32-bit floating-point, 0 = integer
int frame_length; ///< frame length for each frame (last frame may differ)
int ra_distance; ///< distance between RA frames (in frames, 0...255)
enum RA_Flag ra_flag; ///< indicates where the size of ra units is stored
int adapt_order; ///< adaptive order: 1 = on, 0 = off
int coef_table; ///< table index of Rice code parameters
int long_term_prediction; ///< long term prediction (LTP): 1 = on, 0 = off
int max_order; ///< maximum prediction order (0..1023)
int block_switching; ///< number of block switching levels
int bgmc; ///< "Block Gilbert-Moore Code": 1 = on, 0 = off (Rice coding only)
int sb_part; ///< sub-block partition
int joint_stereo; ///< joint stereo: 1 = on, 0 = off
int mc_coding; ///< extended inter-channel coding (multi channel coding): 1 = on, 0 = off
int chan_config; ///< indicates that a chan_config_info field is present
int chan_sort; ///< channel rearrangement: 1 = on, 0 = off
int rlslms; ///< use "Recursive Least Square-Least Mean Square" predictor: 1 = on, 0 = off
int chan_config_info; ///< mapping of channels to loudspeaker locations. Unused until setting channel configuration is implemented.
int *chan_pos; ///< original channel positions
} ALSSpecificConfig;
typedef struct {
int stop_flag;
int master_channel;
int time_diff_flag;
int time_diff_sign;
int time_diff_index;
int weighting[6];
} ALSChannelData;
typedef struct {
AVCodecContext *avctx;
ALSSpecificConfig sconf;
GetBitContext gb;
unsigned int cur_frame_length; ///< length of the current frame to decode
unsigned int frame_id; ///< the frame ID / number of the current frame
unsigned int js_switch; ///< if true, joint-stereo decoding is enforced
unsigned int num_blocks; ///< number of blocks used in the current frame
unsigned int s_max; ///< maximum Rice parameter allowed in entropy coding
uint8_t *bgmc_lut; ///< pointer at lookup tables used for BGMC
unsigned int *bgmc_lut_status; ///< pointer at lookup table status flags used for BGMC
int ltp_lag_length; ///< number of bits used for ltp lag value
int *use_ltp; ///< contains use_ltp flags for all channels
int *ltp_lag; ///< contains ltp lag values for all channels
int **ltp_gain; ///< gain values for ltp 5-tap filter for a channel
int *ltp_gain_buffer; ///< contains all gain values for ltp 5-tap filter
int32_t **quant_cof; ///< quantized parcor coefficients for a channel
int32_t *quant_cof_buffer; ///< contains all quantized parcor coefficients
int32_t **lpc_cof; ///< coefficients of the direct form prediction filter for a channel
int32_t *lpc_cof_buffer; ///< contains all coefficients of the direct form prediction filter
int32_t *lpc_cof_reversed_buffer; ///< temporary buffer to set up a reversed versio of lpc_cof_buffer
ALSChannelData **chan_data; ///< channel data for multi-channel correlation
ALSChannelData *chan_data_buffer; ///< contains channel data for all channels
int *reverted_channels; ///< stores a flag for each reverted channel
int32_t *prev_raw_samples; ///< contains unshifted raw samples from the previous block
int32_t **raw_samples; ///< decoded raw samples for each channel
int32_t *raw_buffer; ///< contains all decoded raw samples including carryover samples
} ALSDecContext;
typedef struct {
unsigned int block_length; ///< number of samples within the block
unsigned int ra_block; ///< if true, this is a random access block
int const_block; ///< if true, this is a constant value block
int32_t const_val; ///< the sample value of a constant block
int js_blocks; ///< true if this block contains a difference signal
unsigned int shift_lsbs; ///< shift of values for this block
unsigned int opt_order; ///< prediction order of this block
int store_prev_samples;///< if true, carryover samples have to be stored
int *use_ltp; ///< if true, long-term prediction is used
int *ltp_lag; ///< lag value for long-term prediction
int *ltp_gain; ///< gain values for ltp 5-tap filter
int32_t *quant_cof; ///< quantized parcor coefficients
int32_t *lpc_cof; ///< coefficients of the direct form prediction
int32_t *raw_samples; ///< decoded raw samples / residuals for this block
int32_t *prev_raw_samples; ///< contains unshifted raw samples from the previous block
int32_t *raw_other; ///< decoded raw samples of the other channel of a channel pair
} ALSBlockData;
static av_cold void dprint_specific_config(ALSDecContext *ctx)
{
#ifdef DEBUG
AVCodecContext *avctx = ctx->avctx;
ALSSpecificConfig *sconf = &ctx->sconf;
dprintf(avctx, "resolution = %i\n", sconf->resolution);
dprintf(avctx, "floating = %i\n", sconf->floating);
dprintf(avctx, "frame_length = %i\n", sconf->frame_length);
dprintf(avctx, "ra_distance = %i\n", sconf->ra_distance);
dprintf(avctx, "ra_flag = %i\n", sconf->ra_flag);
dprintf(avctx, "adapt_order = %i\n", sconf->adapt_order);
dprintf(avctx, "coef_table = %i\n", sconf->coef_table);
dprintf(avctx, "long_term_prediction = %i\n", sconf->long_term_prediction);
dprintf(avctx, "max_order = %i\n", sconf->max_order);
dprintf(avctx, "block_switching = %i\n", sconf->block_switching);
dprintf(avctx, "bgmc = %i\n", sconf->bgmc);
dprintf(avctx, "sb_part = %i\n", sconf->sb_part);
dprintf(avctx, "joint_stereo = %i\n", sconf->joint_stereo);
dprintf(avctx, "mc_coding = %i\n", sconf->mc_coding);
dprintf(avctx, "chan_config = %i\n", sconf->chan_config);
dprintf(avctx, "chan_sort = %i\n", sconf->chan_sort);
dprintf(avctx, "RLSLMS = %i\n", sconf->rlslms);
dprintf(avctx, "chan_config_info = %i\n", sconf->chan_config_info);
#endif
}
/** Reads an ALSSpecificConfig from a buffer into the output struct.
*/
static av_cold int read_specific_config(ALSDecContext *ctx)
{
GetBitContext gb;
uint64_t ht_size;
int i, config_offset, crc_enabled;
MPEG4AudioConfig m4ac;
ALSSpecificConfig *sconf = &ctx->sconf;
AVCodecContext *avctx = ctx->avctx;
uint32_t als_id, header_size, trailer_size;
init_get_bits(&gb, avctx->extradata, avctx->extradata_size * 8);
config_offset = ff_mpeg4audio_get_config(&m4ac, avctx->extradata,
avctx->extradata_size);
if (config_offset < 0)
return -1;
skip_bits_long(&gb, config_offset);
if (get_bits_left(&gb) < (30 << 3))
return -1;
// read the fixed items
als_id = get_bits_long(&gb, 32);
avctx->sample_rate = m4ac.sample_rate;
skip_bits_long(&gb, 32); // sample rate already known
sconf->samples = get_bits_long(&gb, 32);
avctx->channels = m4ac.channels;
skip_bits(&gb, 16); // number of channels already knwon
skip_bits(&gb, 3); // skip file_type
sconf->resolution = get_bits(&gb, 3);
sconf->floating = get_bits1(&gb);
skip_bits1(&gb); // skip msb_first
sconf->frame_length = get_bits(&gb, 16) + 1;
sconf->ra_distance = get_bits(&gb, 8);
sconf->ra_flag = get_bits(&gb, 2);
sconf->adapt_order = get_bits1(&gb);
sconf->coef_table = get_bits(&gb, 2);
sconf->long_term_prediction = get_bits1(&gb);
sconf->max_order = get_bits(&gb, 10);
sconf->block_switching = get_bits(&gb, 2);
sconf->bgmc = get_bits1(&gb);
sconf->sb_part = get_bits1(&gb);
sconf->joint_stereo = get_bits1(&gb);
sconf->mc_coding = get_bits1(&gb);
sconf->chan_config = get_bits1(&gb);
sconf->chan_sort = get_bits1(&gb);
crc_enabled = get_bits1(&gb);
sconf->rlslms = get_bits1(&gb);
skip_bits(&gb, 5); // skip 5 reserved bits
skip_bits1(&gb); // skip aux_data_enabled
// check for ALSSpecificConfig struct
if (als_id != MKBETAG('A','L','S','\0'))
return -1;
ctx->cur_frame_length = sconf->frame_length;
// read channel config
if (sconf->chan_config)
sconf->chan_config_info = get_bits(&gb, 16);
// TODO: use this to set avctx->channel_layout
// read channel sorting
if (sconf->chan_sort && avctx->channels > 1) {
int chan_pos_bits = av_ceil_log2(avctx->channels);
int bits_needed = avctx->channels * chan_pos_bits + 7;
if (get_bits_left(&gb) < bits_needed)
return -1;
if (!(sconf->chan_pos = av_malloc(avctx->channels * sizeof(*sconf->chan_pos))))
return AVERROR(ENOMEM);
for (i = 0; i < avctx->channels; i++)
sconf->chan_pos[i] = get_bits(&gb, chan_pos_bits);
align_get_bits(&gb);
// TODO: use this to actually do channel sorting
} else {
sconf->chan_sort = 0;
}
// read fixed header and trailer sizes,
// if size = 0xFFFFFFFF then there is no data field!
if (get_bits_left(&gb) < 64)
return -1;
header_size = get_bits_long(&gb, 32);
trailer_size = get_bits_long(&gb, 32);
if (header_size == 0xFFFFFFFF)
header_size = 0;
if (trailer_size == 0xFFFFFFFF)
trailer_size = 0;
ht_size = ((int64_t)(header_size) + (int64_t)(trailer_size)) << 3;
// skip the header and trailer data
if (get_bits_left(&gb) < ht_size)
return -1;
if (ht_size > INT32_MAX)
return -1;
skip_bits_long(&gb, ht_size);
// skip the crc data
if (crc_enabled) {
if (get_bits_left(&gb) < 32)
return -1;
skip_bits_long(&gb, 32);
}
// no need to read the rest of ALSSpecificConfig (ra_unit_size & aux data)
dprint_specific_config(ctx);
return 0;
}
/** Checks the ALSSpecificConfig for unsupported features.
*/
static int check_specific_config(ALSDecContext *ctx)
{
ALSSpecificConfig *sconf = &ctx->sconf;
int error = 0;
// report unsupported feature and set error value
#define MISSING_ERR(cond, str, errval) \
{ \
if (cond) { \
av_log_missing_feature(ctx->avctx, str, 0); \
error = errval; \
} \
}
MISSING_ERR(sconf->floating, "Floating point decoding", -1);
MISSING_ERR(sconf->rlslms, "Adaptive RLS-LMS prediction", -1);
MISSING_ERR(sconf->chan_sort, "Channel sorting", 0);
return error;
}
/** Parses the bs_info field to extract the block partitioning used in
* block switching mode, refer to ISO/IEC 14496-3, section 11.6.2.
*/
static void parse_bs_info(const uint32_t bs_info, unsigned int n,
unsigned int div, unsigned int **div_blocks,
unsigned int *num_blocks)
{
if (n < 31 && ((bs_info << n) & 0x40000000)) {
// if the level is valid and the investigated bit n is set
// then recursively check both children at bits (2n+1) and (2n+2)
n *= 2;
div += 1;
parse_bs_info(bs_info, n + 1, div, div_blocks, num_blocks);
parse_bs_info(bs_info, n + 2, div, div_blocks, num_blocks);
} else {
// else the bit is not set or the last level has been reached
// (bit implicitly not set)
**div_blocks = div;
(*div_blocks)++;
(*num_blocks)++;
}
}
/** Reads and decodes a Rice codeword.
*/
static int32_t decode_rice(GetBitContext *gb, unsigned int k)
{
int max = get_bits_left(gb) - k;
int q = get_unary(gb, 0, max);
int r = k ? get_bits1(gb) : !(q & 1);
if (k > 1) {
q <<= (k - 1);
q += get_bits_long(gb, k - 1);
} else if (!k) {
q >>= 1;
}
return r ? q : ~q;
}
/** Converts PARCOR coefficient k to direct filter coefficient.
*/
static void parcor_to_lpc(unsigned int k, const int32_t *par, int32_t *cof)
{
int i, j;
for (i = 0, j = k - 1; i < j; i++, j--) {
int tmp1 = ((MUL64(par[k], cof[j]) + (1 << 19)) >> 20);
cof[j] += ((MUL64(par[k], cof[i]) + (1 << 19)) >> 20);
cof[i] += tmp1;
}
if (i == j)
cof[i] += ((MUL64(par[k], cof[j]) + (1 << 19)) >> 20);
cof[k] = par[k];
}
/** Reads block switching field if necessary and sets actual block sizes.
* Also assures that the block sizes of the last frame correspond to the
* actual number of samples.
*/
static void get_block_sizes(ALSDecContext *ctx, unsigned int *div_blocks,
uint32_t *bs_info)
{
ALSSpecificConfig *sconf = &ctx->sconf;
GetBitContext *gb = &ctx->gb;
unsigned int *ptr_div_blocks = div_blocks;
unsigned int b;
if (sconf->block_switching) {
unsigned int bs_info_len = 1 << (sconf->block_switching + 2);
*bs_info = get_bits_long(gb, bs_info_len);
*bs_info <<= (32 - bs_info_len);
}
ctx->num_blocks = 0;
parse_bs_info(*bs_info, 0, 0, &ptr_div_blocks, &ctx->num_blocks);
// The last frame may have an overdetermined block structure given in
// the bitstream. In that case the defined block structure would need
// more samples than available to be consistent.
// The block structure is actually used but the block sizes are adapted
// to fit the actual number of available samples.
// Example: 5 samples, 2nd level block sizes: 2 2 2 2.
// This results in the actual block sizes: 2 2 1 0.
// This is not specified in 14496-3 but actually done by the reference
// codec RM22 revision 2.
// This appears to happen in case of an odd number of samples in the last
// frame which is actually not allowed by the block length switching part
// of 14496-3.
// The ALS conformance files feature an odd number of samples in the last
// frame.
for (b = 0; b < ctx->num_blocks; b++)
div_blocks[b] = ctx->sconf.frame_length >> div_blocks[b];
if (ctx->cur_frame_length != ctx->sconf.frame_length) {
unsigned int remaining = ctx->cur_frame_length;
for (b = 0; b < ctx->num_blocks; b++) {
if (remaining <= div_blocks[b]) {
div_blocks[b] = remaining;
ctx->num_blocks = b + 1;
break;
}
remaining -= div_blocks[b];
}
}
}
/** Reads the block data for a constant block
*/
static void read_const_block_data(ALSDecContext *ctx, ALSBlockData *bd)
{
ALSSpecificConfig *sconf = &ctx->sconf;
AVCodecContext *avctx = ctx->avctx;
GetBitContext *gb = &ctx->gb;
bd->const_val = 0;
bd->const_block = get_bits1(gb); // 1 = constant value, 0 = zero block (silence)
bd->js_blocks = get_bits1(gb);
// skip 5 reserved bits
skip_bits(gb, 5);
if (bd->const_block) {
unsigned int const_val_bits = sconf->floating ? 24 : avctx->bits_per_raw_sample;
bd->const_val = get_sbits_long(gb, const_val_bits);
}
// ensure constant block decoding by reusing this field
bd->const_block = 1;
}
/** Decodes the block data for a constant block
*/
static void decode_const_block_data(ALSDecContext *ctx, ALSBlockData *bd)
{
int smp = bd->block_length;
int32_t val = bd->const_val;
int32_t *dst = bd->raw_samples;
// write raw samples into buffer
for (; smp; smp--)
*dst++ = val;
}
/** Reads the block data for a non-constant block
*/
static int read_var_block_data(ALSDecContext *ctx, ALSBlockData *bd)
{
ALSSpecificConfig *sconf = &ctx->sconf;
AVCodecContext *avctx = ctx->avctx;
GetBitContext *gb = &ctx->gb;
unsigned int k;
unsigned int s[8];
unsigned int sx[8];
unsigned int sub_blocks, log2_sub_blocks, sb_length;
unsigned int start = 0;
unsigned int opt_order;
int sb;
int32_t *quant_cof = bd->quant_cof;
int32_t *current_res;
// ensure variable block decoding by reusing this field
bd->const_block = 0;
bd->opt_order = 1;
bd->js_blocks = get_bits1(gb);
opt_order = bd->opt_order;
// determine the number of subblocks for entropy decoding
if (!sconf->bgmc && !sconf->sb_part) {
log2_sub_blocks = 0;
} else {
if (sconf->bgmc && sconf->sb_part)
log2_sub_blocks = get_bits(gb, 2);
else
log2_sub_blocks = 2 * get_bits1(gb);
}
sub_blocks = 1 << log2_sub_blocks;
// do not continue in case of a damaged stream since
// block_length must be evenly divisible by sub_blocks
if (bd->block_length & (sub_blocks - 1)) {
av_log(avctx, AV_LOG_WARNING,
"Block length is not evenly divisible by the number of subblocks.\n");
return -1;
}
sb_length = bd->block_length >> log2_sub_blocks;
if (sconf->bgmc) {
s[0] = get_bits(gb, 8 + (sconf->resolution > 1));
for (k = 1; k < sub_blocks; k++)
s[k] = s[k - 1] + decode_rice(gb, 2);
for (k = 0; k < sub_blocks; k++) {
sx[k] = s[k] & 0x0F;
s [k] >>= 4;
}
} else {
s[0] = get_bits(gb, 4 + (sconf->resolution > 1));
for (k = 1; k < sub_blocks; k++)
s[k] = s[k - 1] + decode_rice(gb, 0);
}
if (get_bits1(gb))
bd->shift_lsbs = get_bits(gb, 4) + 1;
bd->store_prev_samples = (bd->js_blocks && bd->raw_other) || bd->shift_lsbs;
if (!sconf->rlslms) {
if (sconf->adapt_order) {
int opt_order_length = av_ceil_log2(av_clip((bd->block_length >> 3) - 1,
2, sconf->max_order + 1));
bd->opt_order = get_bits(gb, opt_order_length);
} else {
bd->opt_order = sconf->max_order;
}
opt_order = bd->opt_order;
if (opt_order) {
int add_base;
if (sconf->coef_table == 3) {
add_base = 0x7F;
// read coefficient 0
quant_cof[0] = 32 * parcor_scaled_values[get_bits(gb, 7)];
// read coefficient 1
if (opt_order > 1)
quant_cof[1] = -32 * parcor_scaled_values[get_bits(gb, 7)];
// read coefficients 2 to opt_order
for (k = 2; k < opt_order; k++)
quant_cof[k] = get_bits(gb, 7);
} else {
int k_max;
add_base = 1;
// read coefficient 0 to 19
k_max = FFMIN(opt_order, 20);
for (k = 0; k < k_max; k++) {
int rice_param = parcor_rice_table[sconf->coef_table][k][1];
int offset = parcor_rice_table[sconf->coef_table][k][0];
quant_cof[k] = decode_rice(gb, rice_param) + offset;
}
// read coefficients 20 to 126
k_max = FFMIN(opt_order, 127);
for (; k < k_max; k++)
quant_cof[k] = decode_rice(gb, 2) + (k & 1);
// read coefficients 127 to opt_order
for (; k < opt_order; k++)
quant_cof[k] = decode_rice(gb, 1);
quant_cof[0] = 32 * parcor_scaled_values[quant_cof[0] + 64];
if (opt_order > 1)
quant_cof[1] = -32 * parcor_scaled_values[quant_cof[1] + 64];
}
for (k = 2; k < opt_order; k++)
quant_cof[k] = (quant_cof[k] << 14) + (add_base << 13);
}
}
// read LTP gain and lag values
if (sconf->long_term_prediction) {
*bd->use_ltp = get_bits1(gb);
if (*bd->use_ltp) {
int r, c;
bd->ltp_gain[0] = decode_rice(gb, 1) << 3;
bd->ltp_gain[1] = decode_rice(gb, 2) << 3;
r = get_unary(gb, 0, 4);
c = get_bits(gb, 2);
bd->ltp_gain[2] = ltp_gain_values[r][c];
bd->ltp_gain[3] = decode_rice(gb, 2) << 3;
bd->ltp_gain[4] = decode_rice(gb, 1) << 3;
*bd->ltp_lag = get_bits(gb, ctx->ltp_lag_length);
*bd->ltp_lag += FFMAX(4, opt_order + 1);
}
}
// read first value and residuals in case of a random access block
if (bd->ra_block) {
if (opt_order)
bd->raw_samples[0] = decode_rice(gb, avctx->bits_per_raw_sample - 4);
if (opt_order > 1)
bd->raw_samples[1] = decode_rice(gb, FFMIN(s[0] + 3, ctx->s_max));
if (opt_order > 2)
bd->raw_samples[2] = decode_rice(gb, FFMIN(s[0] + 1, ctx->s_max));
start = FFMIN(opt_order, 3);
}
// read all residuals
if (sconf->bgmc) {
unsigned int delta[sub_blocks];
unsigned int k [sub_blocks];
unsigned int b = av_clip((av_ceil_log2(bd->block_length) - 3) >> 1, 0, 5);
unsigned int i = start;
// read most significant bits
unsigned int high;
unsigned int low;
unsigned int value;
ff_bgmc_decode_init(gb, &high, &low, &value);
current_res = bd->raw_samples + start;
for (sb = 0; sb < sub_blocks; sb++, i = 0) {
k [sb] = s[sb] > b ? s[sb] - b : 0;
delta[sb] = 5 - s[sb] + k[sb];
ff_bgmc_decode(gb, sb_length, current_res,
delta[sb], sx[sb], &high, &low, &value, ctx->bgmc_lut, ctx->bgmc_lut_status);
current_res += sb_length;
}
ff_bgmc_decode_end(gb);
// read least significant bits and tails
i = start;
current_res = bd->raw_samples + start;
for (sb = 0; sb < sub_blocks; sb++, i = 0) {
unsigned int cur_tail_code = tail_code[sx[sb]][delta[sb]];
unsigned int cur_k = k[sb];
unsigned int cur_s = s[sb];
for (; i < sb_length; i++) {
int32_t res = *current_res;
if (res == cur_tail_code) {
unsigned int max_msb = (2 + (sx[sb] > 2) + (sx[sb] > 10))
<< (5 - delta[sb]);
res = decode_rice(gb, cur_s);
if (res >= 0) {
res += (max_msb ) << cur_k;
} else {
res -= (max_msb - 1) << cur_k;
}
} else {
if (res > cur_tail_code)
res--;
if (res & 1)
res = -res;
res >>= 1;
if (cur_k) {
res <<= cur_k;
res |= get_bits_long(gb, cur_k);
}
}
*current_res++ = res;
}
}
} else {
current_res = bd->raw_samples + start;
for (sb = 0; sb < sub_blocks; sb++, start = 0)
for (; start < sb_length; start++)
*current_res++ = decode_rice(gb, s[sb]);
}
if (!sconf->mc_coding || ctx->js_switch)
align_get_bits(gb);
return 0;
}
/** Decodes the block data for a non-constant block
*/
static int decode_var_block_data(ALSDecContext *ctx, ALSBlockData *bd)
{
ALSSpecificConfig *sconf = &ctx->sconf;
unsigned int block_length = bd->block_length;
unsigned int smp = 0;
unsigned int k;
int opt_order = bd->opt_order;
int sb;
int64_t y;
int32_t *quant_cof = bd->quant_cof;
int32_t *lpc_cof = bd->lpc_cof;
int32_t *raw_samples = bd->raw_samples;
int32_t *raw_samples_end = bd->raw_samples + bd->block_length;
int32_t *lpc_cof_reversed = ctx->lpc_cof_reversed_buffer;
// reverse long-term prediction
if (*bd->use_ltp) {
int ltp_smp;
for (ltp_smp = FFMAX(*bd->ltp_lag - 2, 0); ltp_smp < block_length; ltp_smp++) {
int center = ltp_smp - *bd->ltp_lag;
int begin = FFMAX(0, center - 2);
int end = center + 3;
int tab = 5 - (end - begin);
int base;
y = 1 << 6;
for (base = begin; base < end; base++, tab++)
y += MUL64(bd->ltp_gain[tab], raw_samples[base]);
raw_samples[ltp_smp] += y >> 7;
}
}
// reconstruct all samples from residuals
if (bd->ra_block) {
for (smp = 0; smp < opt_order; smp++) {
y = 1 << 19;
for (sb = 0; sb < smp; sb++)
y += MUL64(lpc_cof[sb], raw_samples[-(sb + 1)]);
*raw_samples++ -= y >> 20;
parcor_to_lpc(smp, quant_cof, lpc_cof);
}
} else {
for (k = 0; k < opt_order; k++)
parcor_to_lpc(k, quant_cof, lpc_cof);
// store previous samples in case that they have to be altered
if (bd->store_prev_samples)
memcpy(bd->prev_raw_samples, raw_samples - sconf->max_order,
sizeof(*bd->prev_raw_samples) * sconf->max_order);
// reconstruct difference signal for prediction (joint-stereo)
if (bd->js_blocks && bd->raw_other) {
int32_t *left, *right;
if (bd->raw_other > raw_samples) { // D = R - L
left = raw_samples;
right = bd->raw_other;
} else { // D = R - L
left = bd->raw_other;
right = raw_samples;
}
for (sb = -1; sb >= -sconf->max_order; sb--)
raw_samples[sb] = right[sb] - left[sb];
}
// reconstruct shifted signal
if (bd->shift_lsbs)
for (sb = -1; sb >= -sconf->max_order; sb--)
raw_samples[sb] >>= bd->shift_lsbs;
}
// reverse linear prediction coefficients for efficiency
lpc_cof = lpc_cof + opt_order;
for (sb = 0; sb < opt_order; sb++)
lpc_cof_reversed[sb] = lpc_cof[-(sb + 1)];
// reconstruct raw samples
raw_samples = bd->raw_samples + smp;
lpc_cof = lpc_cof_reversed + opt_order;
for (; raw_samples < raw_samples_end; raw_samples++) {
y = 1 << 19;
for (sb = -opt_order; sb < 0; sb++)
y += MUL64(lpc_cof[sb], raw_samples[sb]);
*raw_samples -= y >> 20;
}
raw_samples = bd->raw_samples;
// restore previous samples in case that they have been altered
if (bd->store_prev_samples)
memcpy(raw_samples - sconf->max_order, bd->prev_raw_samples,
sizeof(*raw_samples) * sconf->max_order);
return 0;
}
/** Reads the block data.
*/
static int read_block(ALSDecContext *ctx, ALSBlockData *bd)
{
GetBitContext *gb = &ctx->gb;
// read block type flag and read the samples accordingly
if (get_bits1(gb)) {
if (read_var_block_data(ctx, bd))
return -1;
} else {
read_const_block_data(ctx, bd);
}
return 0;
}
/** Decodes the block data.
*/
static int decode_block(ALSDecContext *ctx, ALSBlockData *bd)
{
unsigned int smp;
// read block type flag and read the samples accordingly
if (bd->const_block)
decode_const_block_data(ctx, bd);
else if (decode_var_block_data(ctx, bd))
return -1;
// TODO: read RLSLMS extension data
if (bd->shift_lsbs)
for (smp = 0; smp < bd->block_length; smp++)
bd->raw_samples[smp] <<= bd->shift_lsbs;
return 0;
}
/** Reads and decodes block data successively.
*/
static int read_decode_block(ALSDecContext *ctx, ALSBlockData *bd)
{
int ret;
ret = read_block(ctx, bd);
if (ret)
return ret;
ret = decode_block(ctx, bd);
return ret;
}
/** Computes the number of samples left to decode for the current frame and
* sets these samples to zero.
*/
static void zero_remaining(unsigned int b, unsigned int b_max,
const unsigned int *div_blocks, int32_t *buf)
{
unsigned int count = 0;
while (b < b_max)
count += div_blocks[b];
if (count)
memset(buf, 0, sizeof(*buf) * count);
}
/** Decodes blocks independently.
*/
static int decode_blocks_ind(ALSDecContext *ctx, unsigned int ra_frame,
unsigned int c, const unsigned int *div_blocks,
unsigned int *js_blocks)
{
unsigned int b;
ALSBlockData bd;
memset(&bd, 0, sizeof(ALSBlockData));
bd.ra_block = ra_frame;
bd.use_ltp = ctx->use_ltp;
bd.ltp_lag = ctx->ltp_lag;
bd.ltp_gain = ctx->ltp_gain[0];
bd.quant_cof = ctx->quant_cof[0];
bd.lpc_cof = ctx->lpc_cof[0];
bd.prev_raw_samples = ctx->prev_raw_samples;
bd.raw_samples = ctx->raw_samples[c];
for (b = 0; b < ctx->num_blocks; b++) {
bd.shift_lsbs = 0;
bd.block_length = div_blocks[b];
if (read_decode_block(ctx, &bd)) {
// damaged block, write zero for the rest of the frame
zero_remaining(b, ctx->num_blocks, div_blocks, bd.raw_samples);
return -1;
}
bd.raw_samples += div_blocks[b];
bd.ra_block = 0;
}
return 0;
}
/** Decodes blocks dependently.
*/
static int decode_blocks(ALSDecContext *ctx, unsigned int ra_frame,
unsigned int c, const unsigned int *div_blocks,
unsigned int *js_blocks)
{
ALSSpecificConfig *sconf = &ctx->sconf;
unsigned int offset = 0;
unsigned int b;
ALSBlockData bd[2];
memset(bd, 0, 2 * sizeof(ALSBlockData));
bd[0].ra_block = ra_frame;
bd[0].use_ltp = ctx->use_ltp;
bd[0].ltp_lag = ctx->ltp_lag;
bd[0].ltp_gain = ctx->ltp_gain[0];
bd[0].quant_cof = ctx->quant_cof[0];
bd[0].lpc_cof = ctx->lpc_cof[0];
bd[0].prev_raw_samples = ctx->prev_raw_samples;
bd[0].js_blocks = *js_blocks;
bd[1].ra_block = ra_frame;
bd[1].use_ltp = ctx->use_ltp;
bd[1].ltp_lag = ctx->ltp_lag;
bd[1].ltp_gain = ctx->ltp_gain[0];
bd[1].quant_cof = ctx->quant_cof[0];
bd[1].lpc_cof = ctx->lpc_cof[0];
bd[1].prev_raw_samples = ctx->prev_raw_samples;
bd[1].js_blocks = *(js_blocks + 1);
// decode all blocks
for (b = 0; b < ctx->num_blocks; b++) {
unsigned int s;
bd[0].shift_lsbs = 0;
bd[1].shift_lsbs = 0;
bd[0].block_length = div_blocks[b];
bd[1].block_length = div_blocks[b];
bd[0].raw_samples = ctx->raw_samples[c ] + offset;
bd[1].raw_samples = ctx->raw_samples[c + 1] + offset;
bd[0].raw_other = bd[1].raw_samples;
bd[1].raw_other = bd[0].raw_samples;
if(read_decode_block(ctx, &bd[0]) || read_decode_block(ctx, &bd[1])) {
// damaged block, write zero for the rest of the frame
zero_remaining(b, ctx->num_blocks, div_blocks, bd[0].raw_samples);
zero_remaining(b, ctx->num_blocks, div_blocks, bd[1].raw_samples);
return -1;
}
// reconstruct joint-stereo blocks
if (bd[0].js_blocks) {
if (bd[1].js_blocks)
av_log(ctx->avctx, AV_LOG_WARNING, "Invalid channel pair!\n");
for (s = 0; s < div_blocks[b]; s++)
bd[0].raw_samples[s] = bd[1].raw_samples[s] - bd[0].raw_samples[s];
} else if (bd[1].js_blocks) {
for (s = 0; s < div_blocks[b]; s++)
bd[1].raw_samples[s] = bd[1].raw_samples[s] + bd[0].raw_samples[s];
}
offset += div_blocks[b];
bd[0].ra_block = 0;
bd[1].ra_block = 0;
}
// store carryover raw samples,
// the others channel raw samples are stored by the calling function.
memmove(ctx->raw_samples[c] - sconf->max_order,
ctx->raw_samples[c] - sconf->max_order + sconf->frame_length,
sizeof(*ctx->raw_samples[c]) * sconf->max_order);
return 0;
}
/** Reads the channel data.
*/
static int read_channel_data(ALSDecContext *ctx, ALSChannelData *cd, int c)
{
GetBitContext *gb = &ctx->gb;
ALSChannelData *current = cd;
unsigned int channels = ctx->avctx->channels;
int entries = 0;
while (entries < channels && !(current->stop_flag = get_bits1(gb))) {
current->master_channel = get_bits_long(gb, av_ceil_log2(channels));
if (current->master_channel >= channels) {
av_log(ctx->avctx, AV_LOG_ERROR, "Invalid master channel!\n");
return -1;
}
if (current->master_channel != c) {
current->time_diff_flag = get_bits1(gb);
current->weighting[0] = mcc_weightings[av_clip(decode_rice(gb, 1) + 16, 0, 32)];
current->weighting[1] = mcc_weightings[av_clip(decode_rice(gb, 2) + 14, 0, 32)];
current->weighting[2] = mcc_weightings[av_clip(decode_rice(gb, 1) + 16, 0, 32)];
if (current->time_diff_flag) {
current->weighting[3] = mcc_weightings[av_clip(decode_rice(gb, 1) + 16, 0, 32)];
current->weighting[4] = mcc_weightings[av_clip(decode_rice(gb, 1) + 16, 0, 32)];
current->weighting[5] = mcc_weightings[av_clip(decode_rice(gb, 1) + 16, 0, 32)];
current->time_diff_sign = get_bits1(gb);
current->time_diff_index = get_bits(gb, ctx->ltp_lag_length - 3) + 3;
}
}
current++;
entries++;
}
if (entries == channels) {
av_log(ctx->avctx, AV_LOG_ERROR, "Damaged channel data!\n");
return -1;
}
align_get_bits(gb);
return 0;
}
/** Recursively reverts the inter-channel correlation for a block.
*/
static int revert_channel_correlation(ALSDecContext *ctx, ALSBlockData *bd,
ALSChannelData **cd, int *reverted,
unsigned int offset, int c)
{
ALSChannelData *ch = cd[c];
unsigned int dep = 0;
unsigned int channels = ctx->avctx->channels;
if (reverted[c])
return 0;
reverted[c] = 1;
while (dep < channels && !ch[dep].stop_flag) {
revert_channel_correlation(ctx, bd, cd, reverted, offset,
ch[dep].master_channel);
dep++;
}
if (dep == channels) {
av_log(ctx->avctx, AV_LOG_WARNING, "Invalid channel correlation!\n");
return -1;
}
bd->use_ltp = ctx->use_ltp + c;
bd->ltp_lag = ctx->ltp_lag + c;
bd->ltp_gain = ctx->ltp_gain[c];
bd->lpc_cof = ctx->lpc_cof[c];
bd->quant_cof = ctx->quant_cof[c];
bd->raw_samples = ctx->raw_samples[c] + offset;
dep = 0;
while (!ch[dep].stop_flag) {
unsigned int smp;
unsigned int begin = 1;
unsigned int end = bd->block_length - 1;
int64_t y;
int32_t *master = ctx->raw_samples[ch[dep].master_channel] + offset;
if (ch[dep].time_diff_flag) {
int t = ch[dep].time_diff_index;
if (ch[dep].time_diff_sign) {
t = -t;
begin -= t;
} else {
end -= t;
}
for (smp = begin; smp < end; smp++) {
y = (1 << 6) +
MUL64(ch[dep].weighting[0], master[smp - 1 ]) +
MUL64(ch[dep].weighting[1], master[smp ]) +
MUL64(ch[dep].weighting[2], master[smp + 1 ]) +
MUL64(ch[dep].weighting[3], master[smp - 1 + t]) +
MUL64(ch[dep].weighting[4], master[smp + t]) +
MUL64(ch[dep].weighting[5], master[smp + 1 + t]);
bd->raw_samples[smp] += y >> 7;
}
} else {
for (smp = begin; smp < end; smp++) {
y = (1 << 6) +
MUL64(ch[dep].weighting[0], master[smp - 1]) +
MUL64(ch[dep].weighting[1], master[smp ]) +
MUL64(ch[dep].weighting[2], master[smp + 1]);
bd->raw_samples[smp] += y >> 7;
}
}
dep++;
}
return 0;
}
/** Reads the frame data.
*/
static int read_frame_data(ALSDecContext *ctx, unsigned int ra_frame)
{
ALSSpecificConfig *sconf = &ctx->sconf;
AVCodecContext *avctx = ctx->avctx;
GetBitContext *gb = &ctx->gb;
unsigned int div_blocks[32]; ///< block sizes.
unsigned int c;
unsigned int js_blocks[2];
uint32_t bs_info = 0;
// skip the size of the ra unit if present in the frame
if (sconf->ra_flag == RA_FLAG_FRAMES && ra_frame)
skip_bits_long(gb, 32);
if (sconf->mc_coding && sconf->joint_stereo) {
ctx->js_switch = get_bits1(gb);
align_get_bits(gb);
}
if (!sconf->mc_coding || ctx->js_switch) {
int independent_bs = !sconf->joint_stereo;
for (c = 0; c < avctx->channels; c++) {
js_blocks[0] = 0;
js_blocks[1] = 0;
get_block_sizes(ctx, div_blocks, &bs_info);
// if joint_stereo and block_switching is set, independent decoding
// is signaled via the first bit of bs_info
if (sconf->joint_stereo && sconf->block_switching)
if (bs_info >> 31)
independent_bs = 2;
// if this is the last channel, it has to be decoded independently
if (c == avctx->channels - 1)
independent_bs = 1;
if (independent_bs) {
if (decode_blocks_ind(ctx, ra_frame, c, div_blocks, js_blocks))
return -1;
independent_bs--;
} else {
if (decode_blocks(ctx, ra_frame, c, div_blocks, js_blocks))
return -1;
c++;
}
// store carryover raw samples
memmove(ctx->raw_samples[c] - sconf->max_order,
ctx->raw_samples[c] - sconf->max_order + sconf->frame_length,
sizeof(*ctx->raw_samples[c]) * sconf->max_order);
}
} else { // multi-channel coding
ALSBlockData bd;
int b;
int *reverted_channels = ctx->reverted_channels;
unsigned int offset = 0;
for (c = 0; c < avctx->channels; c++)
if (ctx->chan_data[c] < ctx->chan_data_buffer) {
av_log(ctx->avctx, AV_LOG_ERROR, "Invalid channel data!\n");
return -1;
}
memset(&bd, 0, sizeof(ALSBlockData));
memset(reverted_channels, 0, sizeof(*reverted_channels) * avctx->channels);
bd.ra_block = ra_frame;
bd.prev_raw_samples = ctx->prev_raw_samples;
get_block_sizes(ctx, div_blocks, &bs_info);
for (b = 0; b < ctx->num_blocks; b++) {
bd.shift_lsbs = 0;
bd.block_length = div_blocks[b];
for (c = 0; c < avctx->channels; c++) {
bd.use_ltp = ctx->use_ltp + c;
bd.ltp_lag = ctx->ltp_lag + c;
bd.ltp_gain = ctx->ltp_gain[c];
bd.lpc_cof = ctx->lpc_cof[c];
bd.quant_cof = ctx->quant_cof[c];
bd.raw_samples = ctx->raw_samples[c] + offset;
bd.raw_other = NULL;
read_block(ctx, &bd);
if (read_channel_data(ctx, ctx->chan_data[c], c))
return -1;
}
for (c = 0; c < avctx->channels; c++)
if (revert_channel_correlation(ctx, &bd, ctx->chan_data,
reverted_channels, offset, c))
return -1;
for (c = 0; c < avctx->channels; c++) {
bd.use_ltp = ctx->use_ltp + c;
bd.ltp_lag = ctx->ltp_lag + c;
bd.ltp_gain = ctx->ltp_gain[c];
bd.lpc_cof = ctx->lpc_cof[c];
bd.quant_cof = ctx->quant_cof[c];
bd.raw_samples = ctx->raw_samples[c] + offset;
decode_block(ctx, &bd);
}
memset(reverted_channels, 0, avctx->channels * sizeof(*reverted_channels));
offset += div_blocks[b];
bd.ra_block = 0;
}
// store carryover raw samples
for (c = 0; c < avctx->channels; c++)
memmove(ctx->raw_samples[c] - sconf->max_order,
ctx->raw_samples[c] - sconf->max_order + sconf->frame_length,
sizeof(*ctx->raw_samples[c]) * sconf->max_order);
}
// TODO: read_diff_float_data
return 0;
}
/** Decodes an ALS frame.
*/
static int decode_frame(AVCodecContext *avctx,
void *data, int *data_size,
AVPacket *avpkt)
{
ALSDecContext *ctx = avctx->priv_data;
ALSSpecificConfig *sconf = &ctx->sconf;
const uint8_t *buffer = avpkt->data;
int buffer_size = avpkt->size;
int invalid_frame, size;
unsigned int c, sample, ra_frame, bytes_read, shift;
init_get_bits(&ctx->gb, buffer, buffer_size * 8);
// In the case that the distance between random access frames is set to zero
// (sconf->ra_distance == 0) no frame is treated as a random access frame.
// For the first frame, if prediction is used, all samples used from the
// previous frame are assumed to be zero.
ra_frame = sconf->ra_distance && !(ctx->frame_id % sconf->ra_distance);
// the last frame to decode might have a different length
if (sconf->samples != 0xFFFFFFFF)
ctx->cur_frame_length = FFMIN(sconf->samples - ctx->frame_id * (uint64_t) sconf->frame_length,
sconf->frame_length);
else
ctx->cur_frame_length = sconf->frame_length;
// decode the frame data
if ((invalid_frame = read_frame_data(ctx, ra_frame) < 0))
av_log(ctx->avctx, AV_LOG_WARNING,
"Reading frame data failed. Skipping RA unit.\n");
ctx->frame_id++;
// check for size of decoded data
size = ctx->cur_frame_length * avctx->channels *
(av_get_bits_per_sample_format(avctx->sample_fmt) >> 3);
if (size > *data_size) {
av_log(avctx, AV_LOG_ERROR, "Decoded data exceeds buffer size.\n");
return -1;
}
*data_size = size;
// transform decoded frame into output format
#define INTERLEAVE_OUTPUT(bps) \
{ \
int##bps##_t *dest = (int##bps##_t*) data; \
shift = bps - ctx->avctx->bits_per_raw_sample; \
for (sample = 0; sample < ctx->cur_frame_length; sample++) \
for (c = 0; c < avctx->channels; c++) \
*dest++ = ctx->raw_samples[c][sample] << shift; \
}
if (ctx->avctx->bits_per_raw_sample <= 16) {
INTERLEAVE_OUTPUT(16)
} else {
INTERLEAVE_OUTPUT(32)
}
bytes_read = invalid_frame ? buffer_size :
(get_bits_count(&ctx->gb) + 7) >> 3;
return bytes_read;
}
/** Uninitializes the ALS decoder.
*/
static av_cold int decode_end(AVCodecContext *avctx)
{
ALSDecContext *ctx = avctx->priv_data;
av_freep(&ctx->sconf.chan_pos);
ff_bgmc_end(&ctx->bgmc_lut, &ctx->bgmc_lut_status);
av_freep(&ctx->use_ltp);
av_freep(&ctx->ltp_lag);
av_freep(&ctx->ltp_gain);
av_freep(&ctx->ltp_gain_buffer);
av_freep(&ctx->quant_cof);
av_freep(&ctx->lpc_cof);
av_freep(&ctx->quant_cof_buffer);
av_freep(&ctx->lpc_cof_buffer);
av_freep(&ctx->lpc_cof_reversed_buffer);
av_freep(&ctx->prev_raw_samples);
av_freep(&ctx->raw_samples);
av_freep(&ctx->raw_buffer);
av_freep(&ctx->chan_data);
av_freep(&ctx->chan_data_buffer);
av_freep(&ctx->reverted_channels);
return 0;
}
/** Initializes the ALS decoder.
*/
static av_cold int decode_init(AVCodecContext *avctx)
{
unsigned int c;
unsigned int channel_size;
int num_buffers;
ALSDecContext *ctx = avctx->priv_data;
ALSSpecificConfig *sconf = &ctx->sconf;
ctx->avctx = avctx;
if (!avctx->extradata) {
av_log(avctx, AV_LOG_ERROR, "Missing required ALS extradata.\n");
return -1;
}
if (read_specific_config(ctx)) {
av_log(avctx, AV_LOG_ERROR, "Reading ALSSpecificConfig failed.\n");
decode_end(avctx);
return -1;
}
if (check_specific_config(ctx)) {
decode_end(avctx);
return -1;
}
if (sconf->bgmc)
ff_bgmc_init(avctx, &ctx->bgmc_lut, &ctx->bgmc_lut_status);
if (sconf->floating) {
avctx->sample_fmt = SAMPLE_FMT_FLT;
avctx->bits_per_raw_sample = 32;
} else {
avctx->sample_fmt = sconf->resolution > 1
? SAMPLE_FMT_S32 : SAMPLE_FMT_S16;
avctx->bits_per_raw_sample = (sconf->resolution + 1) * 8;
}
// set maximum Rice parameter for progressive decoding based on resolution
// This is not specified in 14496-3 but actually done by the reference
// codec RM22 revision 2.
ctx->s_max = sconf->resolution > 1 ? 31 : 15;
// set lag value for long-term prediction
ctx->ltp_lag_length = 8 + (avctx->sample_rate >= 96000) +
(avctx->sample_rate >= 192000);
// allocate quantized parcor coefficient buffer
num_buffers = sconf->mc_coding ? avctx->channels : 1;
ctx->quant_cof = av_malloc(sizeof(*ctx->quant_cof) * num_buffers);
ctx->lpc_cof = av_malloc(sizeof(*ctx->lpc_cof) * num_buffers);
ctx->quant_cof_buffer = av_malloc(sizeof(*ctx->quant_cof_buffer) *
num_buffers * sconf->max_order);
ctx->lpc_cof_buffer = av_malloc(sizeof(*ctx->lpc_cof_buffer) *
num_buffers * sconf->max_order);
ctx->lpc_cof_reversed_buffer = av_malloc(sizeof(*ctx->lpc_cof_buffer) *
sconf->max_order);
if (!ctx->quant_cof || !ctx->lpc_cof ||
!ctx->quant_cof_buffer || !ctx->lpc_cof_buffer ||
!ctx->lpc_cof_reversed_buffer) {
av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n");
return AVERROR(ENOMEM);
}
// assign quantized parcor coefficient buffers
for (c = 0; c < num_buffers; c++) {
ctx->quant_cof[c] = ctx->quant_cof_buffer + c * sconf->max_order;
ctx->lpc_cof[c] = ctx->lpc_cof_buffer + c * sconf->max_order;
}
// allocate and assign lag and gain data buffer for ltp mode
ctx->use_ltp = av_mallocz(sizeof(*ctx->use_ltp) * num_buffers);
ctx->ltp_lag = av_malloc (sizeof(*ctx->ltp_lag) * num_buffers);
ctx->ltp_gain = av_malloc (sizeof(*ctx->ltp_gain) * num_buffers);
ctx->ltp_gain_buffer = av_malloc (sizeof(*ctx->ltp_gain_buffer) *
num_buffers * 5);
if (!ctx->use_ltp || !ctx->ltp_lag ||
!ctx->ltp_gain || !ctx->ltp_gain_buffer) {
av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n");
decode_end(avctx);
return AVERROR(ENOMEM);
}
for (c = 0; c < num_buffers; c++)
ctx->ltp_gain[c] = ctx->ltp_gain_buffer + c * 5;
// allocate and assign channel data buffer for mcc mode
if (sconf->mc_coding) {
ctx->chan_data_buffer = av_malloc(sizeof(*ctx->chan_data_buffer) *
num_buffers * num_buffers);
ctx->chan_data = av_malloc(sizeof(*ctx->chan_data) *
num_buffers);
ctx->reverted_channels = av_malloc(sizeof(*ctx->reverted_channels) *
num_buffers);
if (!ctx->chan_data_buffer || !ctx->chan_data || !ctx->reverted_channels) {
av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n");
decode_end(avctx);
return AVERROR(ENOMEM);
}
for (c = 0; c < num_buffers; c++)
ctx->chan_data[c] = ctx->chan_data_buffer + c * num_buffers;
} else {
ctx->chan_data = NULL;
ctx->chan_data_buffer = NULL;
ctx->reverted_channels = NULL;
}
avctx->frame_size = sconf->frame_length;
channel_size = sconf->frame_length + sconf->max_order;
ctx->prev_raw_samples = av_malloc (sizeof(*ctx->prev_raw_samples) * sconf->max_order);
ctx->raw_buffer = av_mallocz(sizeof(*ctx-> raw_buffer) * avctx->channels * channel_size);
ctx->raw_samples = av_malloc (sizeof(*ctx-> raw_samples) * avctx->channels);
// allocate previous raw sample buffer
if (!ctx->prev_raw_samples || !ctx->raw_buffer|| !ctx->raw_samples) {
av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n");
decode_end(avctx);
return AVERROR(ENOMEM);
}
// assign raw samples buffers
ctx->raw_samples[0] = ctx->raw_buffer + sconf->max_order;
for (c = 1; c < avctx->channels; c++)
ctx->raw_samples[c] = ctx->raw_samples[c - 1] + channel_size;
return 0;
}
/** Flushes (resets) the frame ID after seeking.
*/
static av_cold void flush(AVCodecContext *avctx)
{
ALSDecContext *ctx = avctx->priv_data;
ctx->frame_id = 0;
}
AVCodec als_decoder = {
"als",
AVMEDIA_TYPE_AUDIO,
CODEC_ID_MP4ALS,
sizeof(ALSDecContext),
decode_init,
NULL,
decode_end,
decode_frame,
.flush = flush,
.capabilities = CODEC_CAP_SUBFRAMES,
.long_name = NULL_IF_CONFIG_SMALL("MPEG-4 Audio Lossless Coding (ALS)"),
};
| 123linslouis-android-video-cutter | jni/libavcodec/alsdec.c | C | asf20 | 56,949 |
/*
* DXVA2 HW acceleration
*
* copyright (c) 2009 Laurent Aimar
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef AVCODEC_DXVA_H
#define AVCODEC_DXVA_H
#include <stdint.h>
#include <dxva2api.h>
/**
* This structure is used to provides the necessary configurations and data
* to the DXVA2 FFmpeg HWAccel implementation.
*
* The application must make it available as AVCodecContext.hwaccel_context.
*/
struct dxva_context {
/**
* DXVA2 decoder object
*/
IDirectXVideoDecoder *decoder;
/**
* DXVA2 configuration used to create the decoder
*/
const DXVA2_ConfigPictureDecode *cfg;
/**
* The number of surface in the surface array
*/
unsigned surface_count;
/**
* The array of Direct3D surfaces used to create the decoder
*/
LPDIRECT3DSURFACE9 *surface;
/**
* A bit field configuring the workarounds needed for using the decoder
*/
uint64_t workaround;
/**
* Private to the FFmpeg AVHWAccel implementation
*/
unsigned report_id;
};
#endif /* AVCODEC_DXVA_H */
| 123linslouis-android-video-cutter | jni/libavcodec/dxva2.h | C | asf20 | 1,802 |
/*
* RoQ Video Encoder.
*
* Copyright (C) 2007 Vitor Sessak <vitor1001@gmail.com>
* Copyright (C) 2004-2007 Eric Lasota
* Based on RoQ specs (C) 2001 Tim Ferguson
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* id RoQ encoder by Vitor. Based on the Switchblade3 library and the
* Switchblade3 FFmpeg glue by Eric Lasota.
*/
/*
* COSTS:
* Level 1:
* SKIP - 2 bits
* MOTION - 2 + 8 bits
* CODEBOOK - 2 + 8 bits
* SUBDIVIDE - 2 + combined subcel cost
*
* Level 2:
* SKIP - 2 bits
* MOTION - 2 + 8 bits
* CODEBOOK - 2 + 8 bits
* SUBDIVIDE - 2 + 4*8 bits
*
* Maximum cost: 138 bits per cel
*
* Proper evaluation requires LCD fraction comparison, which requires
* Squared Error (SE) loss * savings increase
*
* Maximum savings increase: 136 bits
* Maximum SE loss without overflow: 31580641
* Components in 8x8 supercel: 192
* Maximum SE precision per component: 164482
* >65025, so no truncation is needed (phew)
*/
#include <string.h>
#include "roqvideo.h"
#include "bytestream.h"
#include "elbg.h"
#include "mathops.h"
#define CHROMA_BIAS 1
/**
* Maximum number of generated 4x4 codebooks. Can't be 256 to workaround a
* Quake 3 bug.
*/
#define MAX_CBS_4x4 255
#define MAX_CBS_2x2 256 ///< Maximum number of 2x2 codebooks.
/* The cast is useful when multiplying it by INT_MAX */
#define ROQ_LAMBDA_SCALE ((uint64_t) FF_LAMBDA_SCALE)
/* Macroblock support functions */
static void unpack_roq_cell(roq_cell *cell, uint8_t u[4*3])
{
memcpy(u , cell->y, 4);
memset(u+4, cell->u, 4);
memset(u+8, cell->v, 4);
}
static void unpack_roq_qcell(uint8_t cb2[], roq_qcell *qcell, uint8_t u[4*4*3])
{
int i,cp;
static const int offsets[4] = {0, 2, 8, 10};
for (cp=0; cp<3; cp++)
for (i=0; i<4; i++) {
u[4*4*cp + offsets[i] ] = cb2[qcell->idx[i]*2*2*3 + 4*cp ];
u[4*4*cp + offsets[i]+1] = cb2[qcell->idx[i]*2*2*3 + 4*cp+1];
u[4*4*cp + offsets[i]+4] = cb2[qcell->idx[i]*2*2*3 + 4*cp+2];
u[4*4*cp + offsets[i]+5] = cb2[qcell->idx[i]*2*2*3 + 4*cp+3];
}
}
static void enlarge_roq_mb4(uint8_t base[3*16], uint8_t u[3*64])
{
int x,y,cp;
for(cp=0; cp<3; cp++)
for(y=0; y<8; y++)
for(x=0; x<8; x++)
*u++ = base[(y/2)*4 + (x/2) + 16*cp];
}
static inline int square(int x)
{
return x*x;
}
static inline int eval_sse(uint8_t *a, uint8_t *b, int count)
{
int diff=0;
while(count--)
diff += square(*b++ - *a++);
return diff;
}
// FIXME Could use DSPContext.sse, but it is not so speed critical (used
// just for motion estimation).
static int block_sse(uint8_t **buf1, uint8_t **buf2, int x1, int y1, int x2,
int y2, int *stride1, int *stride2, int size)
{
int i, k;
int sse=0;
for (k=0; k<3; k++) {
int bias = (k ? CHROMA_BIAS : 4);
for (i=0; i<size; i++)
sse += bias*eval_sse(buf1[k] + (y1+i)*stride1[k] + x1,
buf2[k] + (y2+i)*stride2[k] + x2, size);
}
return sse;
}
static int eval_motion_dist(RoqContext *enc, int x, int y, motion_vect vect,
int size)
{
int mx=vect.d[0];
int my=vect.d[1];
if (mx < -7 || mx > 7)
return INT_MAX;
if (my < -7 || my > 7)
return INT_MAX;
mx += x;
my += y;
if ((unsigned) mx > enc->width-size || (unsigned) my > enc->height-size)
return INT_MAX;
return block_sse(enc->frame_to_enc->data, enc->last_frame->data, x, y,
mx, my,
enc->frame_to_enc->linesize, enc->last_frame->linesize,
size);
}
/**
* Returns distortion between two macroblocks
*/
static inline int squared_diff_macroblock(uint8_t a[], uint8_t b[], int size)
{
int cp, sdiff=0;
for(cp=0;cp<3;cp++) {
int bias = (cp ? CHROMA_BIAS : 4);
sdiff += bias*eval_sse(a, b, size*size);
a += size*size;
b += size*size;
}
return sdiff;
}
typedef struct
{
int eval_dist[4];
int best_bit_use;
int best_coding;
int subCels[4];
motion_vect motion;
int cbEntry;
} SubcelEvaluation;
typedef struct
{
int eval_dist[4];
int best_coding;
SubcelEvaluation subCels[4];
motion_vect motion;
int cbEntry;
int sourceX, sourceY;
} CelEvaluation;
typedef struct
{
int numCB4;
int numCB2;
int usedCB2[MAX_CBS_2x2];
int usedCB4[MAX_CBS_4x4];
uint8_t unpacked_cb2[MAX_CBS_2x2*2*2*3];
uint8_t unpacked_cb4[MAX_CBS_4x4*4*4*3];
uint8_t unpacked_cb4_enlarged[MAX_CBS_4x4*8*8*3];
} RoqCodebooks;
/**
* Temporary vars
*/
typedef struct RoqTempData
{
CelEvaluation *cel_evals;
int f2i4[MAX_CBS_4x4];
int i2f4[MAX_CBS_4x4];
int f2i2[MAX_CBS_2x2];
int i2f2[MAX_CBS_2x2];
int mainChunkSize;
int numCB4;
int numCB2;
RoqCodebooks codebooks;
int *closest_cb2;
int used_option[4];
} RoqTempdata;
/**
* Initializes cel evaluators and sets their source coordinates
*/
static void create_cel_evals(RoqContext *enc, RoqTempdata *tempData)
{
int n=0, x, y, i;
tempData->cel_evals = av_malloc(enc->width*enc->height/64 * sizeof(CelEvaluation));
/* Map to the ROQ quadtree order */
for (y=0; y<enc->height; y+=16)
for (x=0; x<enc->width; x+=16)
for(i=0; i<4; i++) {
tempData->cel_evals[n ].sourceX = x + (i&1)*8;
tempData->cel_evals[n++].sourceY = y + (i&2)*4;
}
}
/**
* Get macroblocks from parts of the image
*/
static void get_frame_mb(AVFrame *frame, int x, int y, uint8_t mb[], int dim)
{
int i, j, cp;
for (cp=0; cp<3; cp++) {
int stride = frame->linesize[cp];
for (i=0; i<dim; i++)
for (j=0; j<dim; j++)
*mb++ = frame->data[cp][(y+i)*stride + x + j];
}
}
/**
* Find the codebook with the lowest distortion from an image
*/
static int index_mb(uint8_t cluster[], uint8_t cb[], int numCB,
int *outIndex, int dim)
{
int i, lDiff = INT_MAX, pick=0;
/* Diff against the others */
for (i=0; i<numCB; i++) {
int diff = squared_diff_macroblock(cluster, cb + i*dim*dim*3, dim);
if (diff < lDiff) {
lDiff = diff;
pick = i;
}
}
*outIndex = pick;
return lDiff;
}
#define EVAL_MOTION(MOTION) \
do { \
diff = eval_motion_dist(enc, j, i, MOTION, blocksize); \
\
if (diff < lowestdiff) { \
lowestdiff = diff; \
bestpick = MOTION; \
} \
} while(0)
static void motion_search(RoqContext *enc, int blocksize)
{
static const motion_vect offsets[8] = {
{{ 0,-1}},
{{ 0, 1}},
{{-1, 0}},
{{ 1, 0}},
{{-1, 1}},
{{ 1,-1}},
{{-1,-1}},
{{ 1, 1}},
};
int diff, lowestdiff, oldbest;
int off[3];
motion_vect bestpick = {{0,0}};
int i, j, k, offset;
motion_vect *last_motion;
motion_vect *this_motion;
motion_vect vect, vect2;
int max=(enc->width/blocksize)*enc->height/blocksize;
if (blocksize == 4) {
last_motion = enc->last_motion4;
this_motion = enc->this_motion4;
} else {
last_motion = enc->last_motion8;
this_motion = enc->this_motion8;
}
for (i=0; i<enc->height; i+=blocksize)
for (j=0; j<enc->width; j+=blocksize) {
lowestdiff = eval_motion_dist(enc, j, i, (motion_vect) {{0,0}},
blocksize);
bestpick.d[0] = 0;
bestpick.d[1] = 0;
if (blocksize == 4)
EVAL_MOTION(enc->this_motion8[(i/8)*(enc->width/8) + j/8]);
offset = (i/blocksize)*enc->width/blocksize + j/blocksize;
if (offset < max && offset >= 0)
EVAL_MOTION(last_motion[offset]);
offset++;
if (offset < max && offset >= 0)
EVAL_MOTION(last_motion[offset]);
offset = (i/blocksize + 1)*enc->width/blocksize + j/blocksize;
if (offset < max && offset >= 0)
EVAL_MOTION(last_motion[offset]);
off[0]= (i/blocksize)*enc->width/blocksize + j/blocksize - 1;
off[1]= off[0] - enc->width/blocksize + 1;
off[2]= off[1] + 1;
if (i) {
for(k=0; k<2; k++)
vect.d[k]= mid_pred(this_motion[off[0]].d[k],
this_motion[off[1]].d[k],
this_motion[off[2]].d[k]);
EVAL_MOTION(vect);
for(k=0; k<3; k++)
EVAL_MOTION(this_motion[off[k]]);
} else if(j)
EVAL_MOTION(this_motion[off[0]]);
vect = bestpick;
oldbest = -1;
while (oldbest != lowestdiff) {
oldbest = lowestdiff;
for (k=0; k<8; k++) {
vect2 = vect;
vect2.d[0] += offsets[k].d[0];
vect2.d[1] += offsets[k].d[1];
EVAL_MOTION(vect2);
}
vect = bestpick;
}
offset = (i/blocksize)*enc->width/blocksize + j/blocksize;
this_motion[offset] = bestpick;
}
}
/**
* Gets distortion for all options available to a subcel
*/
static void gather_data_for_subcel(SubcelEvaluation *subcel, int x,
int y, RoqContext *enc, RoqTempdata *tempData)
{
uint8_t mb4[4*4*3];
uint8_t mb2[2*2*3];
int cluster_index;
int i, best_dist;
static const int bitsUsed[4] = {2, 10, 10, 34};
if (enc->framesSinceKeyframe >= 1) {
subcel->motion = enc->this_motion4[y*enc->width/16 + x/4];
subcel->eval_dist[RoQ_ID_FCC] =
eval_motion_dist(enc, x, y,
enc->this_motion4[y*enc->width/16 + x/4], 4);
} else
subcel->eval_dist[RoQ_ID_FCC] = INT_MAX;
if (enc->framesSinceKeyframe >= 2)
subcel->eval_dist[RoQ_ID_MOT] = block_sse(enc->frame_to_enc->data,
enc->current_frame->data, x,
y, x, y,
enc->frame_to_enc->linesize,
enc->current_frame->linesize,
4);
else
subcel->eval_dist[RoQ_ID_MOT] = INT_MAX;
cluster_index = y*enc->width/16 + x/4;
get_frame_mb(enc->frame_to_enc, x, y, mb4, 4);
subcel->eval_dist[RoQ_ID_SLD] = index_mb(mb4,
tempData->codebooks.unpacked_cb4,
tempData->codebooks.numCB4,
&subcel->cbEntry, 4);
subcel->eval_dist[RoQ_ID_CCC] = 0;
for(i=0;i<4;i++) {
subcel->subCels[i] = tempData->closest_cb2[cluster_index*4+i];
get_frame_mb(enc->frame_to_enc, x+2*(i&1),
y+(i&2), mb2, 2);
subcel->eval_dist[RoQ_ID_CCC] +=
squared_diff_macroblock(tempData->codebooks.unpacked_cb2 + subcel->subCels[i]*2*2*3, mb2, 2);
}
best_dist = INT_MAX;
for (i=0; i<4; i++)
if (ROQ_LAMBDA_SCALE*subcel->eval_dist[i] + enc->lambda*bitsUsed[i] <
best_dist) {
subcel->best_coding = i;
subcel->best_bit_use = bitsUsed[i];
best_dist = ROQ_LAMBDA_SCALE*subcel->eval_dist[i] +
enc->lambda*bitsUsed[i];
}
}
/**
* Gets distortion for all options available to a cel
*/
static void gather_data_for_cel(CelEvaluation *cel, RoqContext *enc,
RoqTempdata *tempData)
{
uint8_t mb8[8*8*3];
int index = cel->sourceY*enc->width/64 + cel->sourceX/8;
int i, j, best_dist, divide_bit_use;
int bitsUsed[4] = {2, 10, 10, 0};
if (enc->framesSinceKeyframe >= 1) {
cel->motion = enc->this_motion8[index];
cel->eval_dist[RoQ_ID_FCC] =
eval_motion_dist(enc, cel->sourceX, cel->sourceY,
enc->this_motion8[index], 8);
} else
cel->eval_dist[RoQ_ID_FCC] = INT_MAX;
if (enc->framesSinceKeyframe >= 2)
cel->eval_dist[RoQ_ID_MOT] = block_sse(enc->frame_to_enc->data,
enc->current_frame->data,
cel->sourceX, cel->sourceY,
cel->sourceX, cel->sourceY,
enc->frame_to_enc->linesize,
enc->current_frame->linesize,8);
else
cel->eval_dist[RoQ_ID_MOT] = INT_MAX;
get_frame_mb(enc->frame_to_enc, cel->sourceX, cel->sourceY, mb8, 8);
cel->eval_dist[RoQ_ID_SLD] =
index_mb(mb8, tempData->codebooks.unpacked_cb4_enlarged,
tempData->codebooks.numCB4, &cel->cbEntry, 8);
gather_data_for_subcel(cel->subCels + 0, cel->sourceX+0, cel->sourceY+0, enc, tempData);
gather_data_for_subcel(cel->subCels + 1, cel->sourceX+4, cel->sourceY+0, enc, tempData);
gather_data_for_subcel(cel->subCels + 2, cel->sourceX+0, cel->sourceY+4, enc, tempData);
gather_data_for_subcel(cel->subCels + 3, cel->sourceX+4, cel->sourceY+4, enc, tempData);
cel->eval_dist[RoQ_ID_CCC] = 0;
divide_bit_use = 0;
for (i=0; i<4; i++) {
cel->eval_dist[RoQ_ID_CCC] +=
cel->subCels[i].eval_dist[cel->subCels[i].best_coding];
divide_bit_use += cel->subCels[i].best_bit_use;
}
best_dist = INT_MAX;
bitsUsed[3] = 2 + divide_bit_use;
for (i=0; i<4; i++)
if (ROQ_LAMBDA_SCALE*cel->eval_dist[i] + enc->lambda*bitsUsed[i] <
best_dist) {
cel->best_coding = i;
best_dist = ROQ_LAMBDA_SCALE*cel->eval_dist[i] +
enc->lambda*bitsUsed[i];
}
tempData->used_option[cel->best_coding]++;
tempData->mainChunkSize += bitsUsed[cel->best_coding];
if (cel->best_coding == RoQ_ID_SLD)
tempData->codebooks.usedCB4[cel->cbEntry]++;
if (cel->best_coding == RoQ_ID_CCC)
for (i=0; i<4; i++) {
if (cel->subCels[i].best_coding == RoQ_ID_SLD)
tempData->codebooks.usedCB4[cel->subCels[i].cbEntry]++;
else if (cel->subCels[i].best_coding == RoQ_ID_CCC)
for (j=0; j<4; j++)
tempData->codebooks.usedCB2[cel->subCels[i].subCels[j]]++;
}
}
static void remap_codebooks(RoqContext *enc, RoqTempdata *tempData)
{
int i, j, idx=0;
/* Make remaps for the final codebook usage */
for (i=0; i<MAX_CBS_4x4; i++) {
if (tempData->codebooks.usedCB4[i]) {
tempData->i2f4[i] = idx;
tempData->f2i4[idx] = i;
for (j=0; j<4; j++)
tempData->codebooks.usedCB2[enc->cb4x4[i].idx[j]]++;
idx++;
}
}
tempData->numCB4 = idx;
idx = 0;
for (i=0; i<MAX_CBS_2x2; i++) {
if (tempData->codebooks.usedCB2[i]) {
tempData->i2f2[i] = idx;
tempData->f2i2[idx] = i;
idx++;
}
}
tempData->numCB2 = idx;
}
/**
* Write codebook chunk
*/
static void write_codebooks(RoqContext *enc, RoqTempdata *tempData)
{
int i, j;
uint8_t **outp= &enc->out_buf;
if (tempData->numCB2) {
bytestream_put_le16(outp, RoQ_QUAD_CODEBOOK);
bytestream_put_le32(outp, tempData->numCB2*6 + tempData->numCB4*4);
bytestream_put_byte(outp, tempData->numCB4);
bytestream_put_byte(outp, tempData->numCB2);
for (i=0; i<tempData->numCB2; i++) {
bytestream_put_buffer(outp, enc->cb2x2[tempData->f2i2[i]].y, 4);
bytestream_put_byte(outp, enc->cb2x2[tempData->f2i2[i]].u);
bytestream_put_byte(outp, enc->cb2x2[tempData->f2i2[i]].v);
}
for (i=0; i<tempData->numCB4; i++)
for (j=0; j<4; j++)
bytestream_put_byte(outp, tempData->i2f2[enc->cb4x4[tempData->f2i4[i]].idx[j]]);
}
}
static inline uint8_t motion_arg(motion_vect mot)
{
uint8_t ax = 8 - ((uint8_t) mot.d[0]);
uint8_t ay = 8 - ((uint8_t) mot.d[1]);
return ((ax&15)<<4) | (ay&15);
}
typedef struct
{
int typeSpool;
int typeSpoolLength;
uint8_t argumentSpool[64];
uint8_t *args;
uint8_t **pout;
} CodingSpool;
/* NOTE: Typecodes must be spooled AFTER arguments!! */
static void write_typecode(CodingSpool *s, uint8_t type)
{
s->typeSpool |= (type & 3) << (14 - s->typeSpoolLength);
s->typeSpoolLength += 2;
if (s->typeSpoolLength == 16) {
bytestream_put_le16(s->pout, s->typeSpool);
bytestream_put_buffer(s->pout, s->argumentSpool,
s->args - s->argumentSpool);
s->typeSpoolLength = 0;
s->typeSpool = 0;
s->args = s->argumentSpool;
}
}
static void reconstruct_and_encode_image(RoqContext *enc, RoqTempdata *tempData, int w, int h, int numBlocks)
{
int i, j, k;
int x, y;
int subX, subY;
int dist=0;
roq_qcell *qcell;
CelEvaluation *eval;
CodingSpool spool;
spool.typeSpool=0;
spool.typeSpoolLength=0;
spool.args = spool.argumentSpool;
spool.pout = &enc->out_buf;
if (tempData->used_option[RoQ_ID_CCC]%2)
tempData->mainChunkSize+=8; //FIXME
/* Write the video chunk header */
bytestream_put_le16(&enc->out_buf, RoQ_QUAD_VQ);
bytestream_put_le32(&enc->out_buf, tempData->mainChunkSize/8);
bytestream_put_byte(&enc->out_buf, 0x0);
bytestream_put_byte(&enc->out_buf, 0x0);
for (i=0; i<numBlocks; i++) {
eval = tempData->cel_evals + i;
x = eval->sourceX;
y = eval->sourceY;
dist += eval->eval_dist[eval->best_coding];
switch (eval->best_coding) {
case RoQ_ID_MOT:
write_typecode(&spool, RoQ_ID_MOT);
break;
case RoQ_ID_FCC:
bytestream_put_byte(&spool.args, motion_arg(eval->motion));
write_typecode(&spool, RoQ_ID_FCC);
ff_apply_motion_8x8(enc, x, y,
eval->motion.d[0], eval->motion.d[1]);
break;
case RoQ_ID_SLD:
bytestream_put_byte(&spool.args, tempData->i2f4[eval->cbEntry]);
write_typecode(&spool, RoQ_ID_SLD);
qcell = enc->cb4x4 + eval->cbEntry;
ff_apply_vector_4x4(enc, x , y , enc->cb2x2 + qcell->idx[0]);
ff_apply_vector_4x4(enc, x+4, y , enc->cb2x2 + qcell->idx[1]);
ff_apply_vector_4x4(enc, x , y+4, enc->cb2x2 + qcell->idx[2]);
ff_apply_vector_4x4(enc, x+4, y+4, enc->cb2x2 + qcell->idx[3]);
break;
case RoQ_ID_CCC:
write_typecode(&spool, RoQ_ID_CCC);
for (j=0; j<4; j++) {
subX = x + 4*(j&1);
subY = y + 2*(j&2);
switch(eval->subCels[j].best_coding) {
case RoQ_ID_MOT:
break;
case RoQ_ID_FCC:
bytestream_put_byte(&spool.args,
motion_arg(eval->subCels[j].motion));
ff_apply_motion_4x4(enc, subX, subY,
eval->subCels[j].motion.d[0],
eval->subCels[j].motion.d[1]);
break;
case RoQ_ID_SLD:
bytestream_put_byte(&spool.args,
tempData->i2f4[eval->subCels[j].cbEntry]);
qcell = enc->cb4x4 + eval->subCels[j].cbEntry;
ff_apply_vector_2x2(enc, subX , subY ,
enc->cb2x2 + qcell->idx[0]);
ff_apply_vector_2x2(enc, subX+2, subY ,
enc->cb2x2 + qcell->idx[1]);
ff_apply_vector_2x2(enc, subX , subY+2,
enc->cb2x2 + qcell->idx[2]);
ff_apply_vector_2x2(enc, subX+2, subY+2,
enc->cb2x2 + qcell->idx[3]);
break;
case RoQ_ID_CCC:
for (k=0; k<4; k++) {
int cb_idx = eval->subCels[j].subCels[k];
bytestream_put_byte(&spool.args,
tempData->i2f2[cb_idx]);
ff_apply_vector_2x2(enc, subX + 2*(k&1), subY + (k&2),
enc->cb2x2 + cb_idx);
}
break;
}
write_typecode(&spool, eval->subCels[j].best_coding);
}
break;
}
}
/* Flush the remainder of the argument/type spool */
while (spool.typeSpoolLength)
write_typecode(&spool, 0x0);
#if 0
uint8_t *fdata[3] = {enc->frame_to_enc->data[0],
enc->frame_to_enc->data[1],
enc->frame_to_enc->data[2]};
uint8_t *cdata[3] = {enc->current_frame->data[0],
enc->current_frame->data[1],
enc->current_frame->data[2]};
av_log(enc->avctx, AV_LOG_ERROR, "Expected distortion: %i Actual: %i\n",
dist,
block_sse(fdata, cdata, 0, 0, 0, 0,
enc->frame_to_enc->linesize,
enc->current_frame->linesize,
enc->width)); //WARNING: Square dimensions implied...
#endif
}
/**
* Create a single YUV cell from a 2x2 section of the image
*/
static inline void frame_block_to_cell(uint8_t *block, uint8_t **data,
int top, int left, int *stride)
{
int i, j, u=0, v=0;
for (i=0; i<2; i++)
for (j=0; j<2; j++) {
int x = (top+i)*stride[0] + left + j;
*block++ = data[0][x];
x = (top+i)*stride[1] + left + j;
u += data[1][x];
v += data[2][x];
}
*block++ = (u+2)/4;
*block++ = (v+2)/4;
}
/**
* Creates YUV clusters for the entire image
*/
static void create_clusters(AVFrame *frame, int w, int h, uint8_t *yuvClusters)
{
int i, j, k, l;
for (i=0; i<h; i+=4)
for (j=0; j<w; j+=4) {
for (k=0; k < 2; k++)
for (l=0; l < 2; l++)
frame_block_to_cell(yuvClusters + (l + 2*k)*6, frame->data,
i+2*k, j+2*l, frame->linesize);
yuvClusters += 24;
}
}
static void generate_codebook(RoqContext *enc, RoqTempdata *tempdata,
int *points, int inputCount, roq_cell *results,
int size, int cbsize)
{
int i, j, k;
int c_size = size*size/4;
int *buf;
int *codebook = av_malloc(6*c_size*cbsize*sizeof(int));
int *closest_cb;
if (size == 4)
closest_cb = av_malloc(6*c_size*inputCount*sizeof(int));
else
closest_cb = tempdata->closest_cb2;
ff_init_elbg(points, 6*c_size, inputCount, codebook, cbsize, 1, closest_cb, &enc->randctx);
ff_do_elbg(points, 6*c_size, inputCount, codebook, cbsize, 1, closest_cb, &enc->randctx);
if (size == 4)
av_free(closest_cb);
buf = codebook;
for (i=0; i<cbsize; i++)
for (k=0; k<c_size; k++) {
for(j=0; j<4; j++)
results->y[j] = *buf++;
results->u = (*buf++ + CHROMA_BIAS/2)/CHROMA_BIAS;
results->v = (*buf++ + CHROMA_BIAS/2)/CHROMA_BIAS;
results++;
}
av_free(codebook);
}
static void generate_new_codebooks(RoqContext *enc, RoqTempdata *tempData)
{
int i,j;
RoqCodebooks *codebooks = &tempData->codebooks;
int max = enc->width*enc->height/16;
uint8_t mb2[3*4];
roq_cell *results4 = av_malloc(sizeof(roq_cell)*MAX_CBS_4x4*4);
uint8_t *yuvClusters=av_malloc(sizeof(int)*max*6*4);
int *points = av_malloc(max*6*4*sizeof(int));
int bias;
/* Subsample YUV data */
create_clusters(enc->frame_to_enc, enc->width, enc->height, yuvClusters);
/* Cast to integer and apply chroma bias */
for (i=0; i<max*24; i++) {
bias = ((i%6)<4) ? 1 : CHROMA_BIAS;
points[i] = bias*yuvClusters[i];
}
/* Create 4x4 codebooks */
generate_codebook(enc, tempData, points, max, results4, 4, MAX_CBS_4x4);
codebooks->numCB4 = MAX_CBS_4x4;
tempData->closest_cb2 = av_malloc(max*4*sizeof(int));
/* Create 2x2 codebooks */
generate_codebook(enc, tempData, points, max*4, enc->cb2x2, 2, MAX_CBS_2x2);
codebooks->numCB2 = MAX_CBS_2x2;
/* Unpack 2x2 codebook clusters */
for (i=0; i<codebooks->numCB2; i++)
unpack_roq_cell(enc->cb2x2 + i, codebooks->unpacked_cb2 + i*2*2*3);
/* Index all 4x4 entries to the 2x2 entries, unpack, and enlarge */
for (i=0; i<codebooks->numCB4; i++) {
for (j=0; j<4; j++) {
unpack_roq_cell(&results4[4*i + j], mb2);
index_mb(mb2, codebooks->unpacked_cb2, codebooks->numCB2,
&enc->cb4x4[i].idx[j], 2);
}
unpack_roq_qcell(codebooks->unpacked_cb2, enc->cb4x4 + i,
codebooks->unpacked_cb4 + i*4*4*3);
enlarge_roq_mb4(codebooks->unpacked_cb4 + i*4*4*3,
codebooks->unpacked_cb4_enlarged + i*8*8*3);
}
av_free(yuvClusters);
av_free(points);
av_free(results4);
}
static void roq_encode_video(RoqContext *enc)
{
RoqTempdata *tempData = enc->tmpData;
int i;
memset(tempData, 0, sizeof(*tempData));
create_cel_evals(enc, tempData);
generate_new_codebooks(enc, tempData);
if (enc->framesSinceKeyframe >= 1) {
motion_search(enc, 8);
motion_search(enc, 4);
}
retry_encode:
for (i=0; i<enc->width*enc->height/64; i++)
gather_data_for_cel(tempData->cel_evals + i, enc, tempData);
/* Quake 3 can't handle chunks bigger than 65536 bytes */
if (tempData->mainChunkSize/8 > 65536) {
enc->lambda *= .8;
goto retry_encode;
}
remap_codebooks(enc, tempData);
write_codebooks(enc, tempData);
reconstruct_and_encode_image(enc, tempData, enc->width, enc->height,
enc->width*enc->height/64);
enc->avctx->coded_frame = enc->current_frame;
/* Rotate frame history */
FFSWAP(AVFrame *, enc->current_frame, enc->last_frame);
FFSWAP(motion_vect *, enc->last_motion4, enc->this_motion4);
FFSWAP(motion_vect *, enc->last_motion8, enc->this_motion8);
av_free(tempData->cel_evals);
av_free(tempData->closest_cb2);
enc->framesSinceKeyframe++;
}
static int roq_encode_init(AVCodecContext *avctx)
{
RoqContext *enc = avctx->priv_data;
av_lfg_init(&enc->randctx, 1);
enc->framesSinceKeyframe = 0;
if ((avctx->width & 0xf) || (avctx->height & 0xf)) {
av_log(avctx, AV_LOG_ERROR, "Dimensions must be divisible by 16\n");
return -1;
}
if (((avctx->width)&(avctx->width-1))||((avctx->height)&(avctx->height-1)))
av_log(avctx, AV_LOG_ERROR, "Warning: dimensions not power of two\n");
enc->width = avctx->width;
enc->height = avctx->height;
enc->framesSinceKeyframe = 0;
enc->first_frame = 1;
enc->last_frame = &enc->frames[0];
enc->current_frame = &enc->frames[1];
enc->tmpData = av_malloc(sizeof(RoqTempdata));
enc->this_motion4 =
av_mallocz((enc->width*enc->height/16)*sizeof(motion_vect));
enc->last_motion4 =
av_malloc ((enc->width*enc->height/16)*sizeof(motion_vect));
enc->this_motion8 =
av_mallocz((enc->width*enc->height/64)*sizeof(motion_vect));
enc->last_motion8 =
av_malloc ((enc->width*enc->height/64)*sizeof(motion_vect));
return 0;
}
static void roq_write_video_info_chunk(RoqContext *enc)
{
/* ROQ info chunk */
bytestream_put_le16(&enc->out_buf, RoQ_INFO);
/* Size: 8 bytes */
bytestream_put_le32(&enc->out_buf, 8);
/* Unused argument */
bytestream_put_byte(&enc->out_buf, 0x00);
bytestream_put_byte(&enc->out_buf, 0x00);
/* Width */
bytestream_put_le16(&enc->out_buf, enc->width);
/* Height */
bytestream_put_le16(&enc->out_buf, enc->height);
/* Unused in Quake 3, mimics the output of the real encoder */
bytestream_put_byte(&enc->out_buf, 0x08);
bytestream_put_byte(&enc->out_buf, 0x00);
bytestream_put_byte(&enc->out_buf, 0x04);
bytestream_put_byte(&enc->out_buf, 0x00);
}
static int roq_encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data)
{
RoqContext *enc = avctx->priv_data;
AVFrame *frame= data;
uint8_t *buf_start = buf;
enc->out_buf = buf;
enc->avctx = avctx;
enc->frame_to_enc = frame;
if (frame->quality)
enc->lambda = frame->quality - 1;
else
enc->lambda = 2*ROQ_LAMBDA_SCALE;
/* 138 bits max per 8x8 block +
* 256 codebooks*(6 bytes 2x2 + 4 bytes 4x4) + 8 bytes frame header */
if (((enc->width*enc->height/64)*138+7)/8 + 256*(6+4) + 8 > buf_size) {
av_log(avctx, AV_LOG_ERROR, " RoQ: Output buffer too small!\n");
return -1;
}
/* Check for I frame */
if (enc->framesSinceKeyframe == avctx->gop_size)
enc->framesSinceKeyframe = 0;
if (enc->first_frame) {
/* Alloc memory for the reconstruction data (we must know the stride
for that) */
if (avctx->get_buffer(avctx, enc->current_frame) ||
avctx->get_buffer(avctx, enc->last_frame)) {
av_log(avctx, AV_LOG_ERROR, " RoQ: get_buffer() failed\n");
return -1;
}
/* Before the first video frame, write a "video info" chunk */
roq_write_video_info_chunk(enc);
enc->first_frame = 0;
}
/* Encode the actual frame */
roq_encode_video(enc);
return enc->out_buf - buf_start;
}
static int roq_encode_end(AVCodecContext *avctx)
{
RoqContext *enc = avctx->priv_data;
avctx->release_buffer(avctx, enc->last_frame);
avctx->release_buffer(avctx, enc->current_frame);
av_free(enc->tmpData);
av_free(enc->this_motion4);
av_free(enc->last_motion4);
av_free(enc->this_motion8);
av_free(enc->last_motion8);
return 0;
}
AVCodec roq_encoder =
{
"roqvideo",
AVMEDIA_TYPE_VIDEO,
CODEC_ID_ROQ,
sizeof(RoqContext),
roq_encode_init,
roq_encode_frame,
roq_encode_end,
.supported_framerates = (const AVRational[]){{30,1}, {0,0}},
.pix_fmts = (const enum PixelFormat[]){PIX_FMT_YUV444P, PIX_FMT_NONE},
.long_name = NULL_IF_CONFIG_SMALL("id RoQ video"),
};
| 123linslouis-android-video-cutter | jni/libavcodec/roqvideoenc.c | C | asf20 | 31,726 |
/*
* Musepack decoder
* Copyright (c) 2006 Konstantin Shishkov
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef AVCODEC_MPCDATA_H
#define AVCODEC_MPCDATA_H
#include <stdint.h>
static const float mpc_CC[18] = {
65536.0000, 21845.3333, 13107.2000, 9362.2857, 7281.7778, 4369.0667, 2114.0645,
1040.2539, 516.0315, 257.0039, 128.2505, 64.0626, 32.0156, 16.0039, 8.0010,
4.0002, 2.0001, 1.0000
};
static const float mpc_SCF[128] = {
307.330047607421875000, 255.999984741210937500, 213.243041992187500000, 177.627334594726562500,
147.960128784179687500, 123.247924804687500000, 102.663139343261718750, 85.516410827636718750,
71.233520507812500000, 59.336143493652343750, 49.425861358642578125, 41.170787811279296875,
34.294471740722656250, 28.566631317138671875, 23.795452117919921875, 19.821151733398437500,
16.510635375976562500, 13.753040313720703125, 11.456016540527343750, 9.542640686035156250,
7.948835372924804688, 6.621226310729980469, 5.515353679656982422, 4.594182968139648438,
3.826865673065185547, 3.187705039978027344, 2.655296564102172852, 2.211810588836669922,
1.842395424842834473, 1.534679770469665527, 1.278358578681945801, 1.064847946166992188,
0.886997759342193604, 0.738851964473724365, 0.615449428558349609, 0.512657463550567627,
0.427033752202987671, 0.355710864067077637, 0.296300262212753296, 0.246812388300895691,
0.205589950084686279, 0.171252459287643433, 0.142649993300437927, 0.118824683129787445,
0.098978661000728607, 0.082447312772274017, 0.068677015602588654, 0.057206626981496811,
0.047652013599872589, 0.039693206548690796, 0.033063672482967377, 0.027541399002075195,
0.022941453382372856, 0.019109787419438362, 0.015918083488941193, 0.013259455561637878,
0.011044870130717754, 0.009200163185596466, 0.007663558237254620, 0.006383595988154411,
0.005317411851137877, 0.004429301247000694, 0.003689522389322519, 0.003073300700634718,
0.002560000168159604, 0.002132430672645569, 0.001776273478753865, 0.001479601487517357,
0.001232479466125369, 0.001026631565764546, 0.000855164253152907, 0.000712335284333676,
0.000593361502978951, 0.000494258652906865, 0.000411707907915115, 0.000342944724252447,
0.000285666319541633, 0.000237954518524930, 0.000198211506358348, 0.000165106350323185,
0.000137530398205854, 0.000114560163638089, 0.000095426403277088, 0.000079488345363643,
0.000066212254751008, 0.000055153526773211, 0.000045941822463647, 0.000038268648495432,
0.000031877043511486, 0.000026552961571724, 0.000022118103515822, 0.000018423952496960,
0.000015346795407822, 0.000012783583770215, 0.000010648477655195, 0.000008869976227288,
0.000007388518497464, 0.000006154492893984, 0.000005126573796588, 0.000004270336830814,
0.000003557107902452, 0.000002963002089018, 0.000002468123511790, 0.000002055899130937,
0.000001712524181130, 0.000001426499579793, 0.000001188246528727, 0.000000989786371974,
0.000000824472920158, 0.000000686770022185, 0.000000572066142013, 0.000000476520028769,
0.000000396931966407, 0.000000330636652279, 0.000000275413924555, 0.000000229414467867,
0.000000191097811353, 0.000000159180785886, 0.000000132594522029, 0.000000110448674207,
0.000000092001613439, 0.000000076635565449, 0.000000063835940978, 0.000000053174105119,
0.000000044293003043, 0.000000036895215771, 0.000000030733001921, 0.000000025599996789
};
#endif /* AVCODEC_MPCDATA_H */
| 123linslouis-android-video-cutter | jni/libavcodec/mpcdata.h | C | asf20 | 4,172 |
/*
* RV40 decoder motion compensation functions
* Copyright (c) 2008 Konstantin Shishkov
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* RV40 decoder motion compensation functions
*/
#include "avcodec.h"
#include "dsputil.h"
#define RV40_LOWPASS(OPNAME, OP) \
static av_unused void OPNAME ## rv40_qpel8_h_lowpass(uint8_t *dst, uint8_t *src, int dstStride, int srcStride,\
const int h, const int C1, const int C2, const int SHIFT){\
uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;\
int i;\
for(i=0; i<h; i++)\
{\
OP(dst[0], (src[-2] + src[ 3] - 5*(src[-1]+src[2]) + src[0]*C1 + src[1]*C2 + (1<<(SHIFT-1))) >> SHIFT);\
OP(dst[1], (src[-1] + src[ 4] - 5*(src[ 0]+src[3]) + src[1]*C1 + src[2]*C2 + (1<<(SHIFT-1))) >> SHIFT);\
OP(dst[2], (src[ 0] + src[ 5] - 5*(src[ 1]+src[4]) + src[2]*C1 + src[3]*C2 + (1<<(SHIFT-1))) >> SHIFT);\
OP(dst[3], (src[ 1] + src[ 6] - 5*(src[ 2]+src[5]) + src[3]*C1 + src[4]*C2 + (1<<(SHIFT-1))) >> SHIFT);\
OP(dst[4], (src[ 2] + src[ 7] - 5*(src[ 3]+src[6]) + src[4]*C1 + src[5]*C2 + (1<<(SHIFT-1))) >> SHIFT);\
OP(dst[5], (src[ 3] + src[ 8] - 5*(src[ 4]+src[7]) + src[5]*C1 + src[6]*C2 + (1<<(SHIFT-1))) >> SHIFT);\
OP(dst[6], (src[ 4] + src[ 9] - 5*(src[ 5]+src[8]) + src[6]*C1 + src[7]*C2 + (1<<(SHIFT-1))) >> SHIFT);\
OP(dst[7], (src[ 5] + src[10] - 5*(src[ 6]+src[9]) + src[7]*C1 + src[8]*C2 + (1<<(SHIFT-1))) >> SHIFT);\
dst+=dstStride;\
src+=srcStride;\
}\
}\
\
static void OPNAME ## rv40_qpel8_v_lowpass(uint8_t *dst, uint8_t *src, int dstStride, int srcStride,\
const int w, const int C1, const int C2, const int SHIFT){\
uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;\
int i;\
for(i=0; i<w; i++)\
{\
const int srcB = src[-2*srcStride];\
const int srcA = src[-1*srcStride];\
const int src0 = src[0 *srcStride];\
const int src1 = src[1 *srcStride];\
const int src2 = src[2 *srcStride];\
const int src3 = src[3 *srcStride];\
const int src4 = src[4 *srcStride];\
const int src5 = src[5 *srcStride];\
const int src6 = src[6 *srcStride];\
const int src7 = src[7 *srcStride];\
const int src8 = src[8 *srcStride];\
const int src9 = src[9 *srcStride];\
const int src10= src[10*srcStride];\
OP(dst[0*dstStride], (srcB + src3 - 5*(srcA+src2) + src0*C1 + src1*C2 + (1<<(SHIFT-1))) >> SHIFT);\
OP(dst[1*dstStride], (srcA + src4 - 5*(src0+src3) + src1*C1 + src2*C2 + (1<<(SHIFT-1))) >> SHIFT);\
OP(dst[2*dstStride], (src0 + src5 - 5*(src1+src4) + src2*C1 + src3*C2 + (1<<(SHIFT-1))) >> SHIFT);\
OP(dst[3*dstStride], (src1 + src6 - 5*(src2+src5) + src3*C1 + src4*C2 + (1<<(SHIFT-1))) >> SHIFT);\
OP(dst[4*dstStride], (src2 + src7 - 5*(src3+src6) + src4*C1 + src5*C2 + (1<<(SHIFT-1))) >> SHIFT);\
OP(dst[5*dstStride], (src3 + src8 - 5*(src4+src7) + src5*C1 + src6*C2 + (1<<(SHIFT-1))) >> SHIFT);\
OP(dst[6*dstStride], (src4 + src9 - 5*(src5+src8) + src6*C1 + src7*C2 + (1<<(SHIFT-1))) >> SHIFT);\
OP(dst[7*dstStride], (src5 + src10 - 5*(src6+src9) + src7*C1 + src8*C2 + (1<<(SHIFT-1))) >> SHIFT);\
dst++;\
src++;\
}\
}\
\
static void OPNAME ## rv40_qpel16_v_lowpass(uint8_t *dst, uint8_t *src, int dstStride, int srcStride,\
const int w, const int C1, const int C2, const int SHIFT){\
OPNAME ## rv40_qpel8_v_lowpass(dst , src , dstStride, srcStride, 8, C1, C2, SHIFT);\
OPNAME ## rv40_qpel8_v_lowpass(dst+8, src+8, dstStride, srcStride, 8, C1, C2, SHIFT);\
src += 8*srcStride;\
dst += 8*dstStride;\
OPNAME ## rv40_qpel8_v_lowpass(dst , src , dstStride, srcStride, w-8, C1, C2, SHIFT);\
OPNAME ## rv40_qpel8_v_lowpass(dst+8, src+8, dstStride, srcStride, w-8, C1, C2, SHIFT);\
}\
\
static void OPNAME ## rv40_qpel16_h_lowpass(uint8_t *dst, uint8_t *src, int dstStride, int srcStride,\
const int h, const int C1, const int C2, const int SHIFT){\
OPNAME ## rv40_qpel8_h_lowpass(dst , src , dstStride, srcStride, 8, C1, C2, SHIFT);\
OPNAME ## rv40_qpel8_h_lowpass(dst+8, src+8, dstStride, srcStride, 8, C1, C2, SHIFT);\
src += 8*srcStride;\
dst += 8*dstStride;\
OPNAME ## rv40_qpel8_h_lowpass(dst , src , dstStride, srcStride, h-8, C1, C2, SHIFT);\
OPNAME ## rv40_qpel8_h_lowpass(dst+8, src+8, dstStride, srcStride, h-8, C1, C2, SHIFT);\
}\
\
#define RV40_MC(OPNAME, SIZE) \
static void OPNAME ## rv40_qpel ## SIZE ## _mc10_c(uint8_t *dst, uint8_t *src, int stride){\
OPNAME ## rv40_qpel ## SIZE ## _h_lowpass(dst, src, stride, stride, SIZE, 52, 20, 6);\
}\
\
static void OPNAME ## rv40_qpel ## SIZE ## _mc20_c(uint8_t *dst, uint8_t *src, int stride){\
OPNAME ## rv40_qpel ## SIZE ## _h_lowpass(dst, src, stride, stride, SIZE, 20, 20, 5);\
}\
\
static void OPNAME ## rv40_qpel ## SIZE ## _mc30_c(uint8_t *dst, uint8_t *src, int stride){\
OPNAME ## rv40_qpel ## SIZE ## _h_lowpass(dst, src, stride, stride, SIZE, 20, 52, 6);\
}\
\
static void OPNAME ## rv40_qpel ## SIZE ## _mc01_c(uint8_t *dst, uint8_t *src, int stride){\
OPNAME ## rv40_qpel ## SIZE ## _v_lowpass(dst, src, stride, stride, SIZE, 52, 20, 6);\
}\
\
static void OPNAME ## rv40_qpel ## SIZE ## _mc11_c(uint8_t *dst, uint8_t *src, int stride){\
uint8_t full[SIZE*(SIZE+5)];\
uint8_t * const full_mid= full + SIZE*2;\
put_rv40_qpel ## SIZE ## _h_lowpass(full, src - 2*stride, SIZE, stride, SIZE+5, 52, 20, 6);\
OPNAME ## rv40_qpel ## SIZE ## _v_lowpass(dst, full_mid, stride, SIZE, SIZE, 52, 20, 6);\
}\
\
static void OPNAME ## rv40_qpel ## SIZE ## _mc21_c(uint8_t *dst, uint8_t *src, int stride){\
uint8_t full[SIZE*(SIZE+5)];\
uint8_t * const full_mid= full + SIZE*2;\
put_rv40_qpel ## SIZE ## _h_lowpass(full, src - 2*stride, SIZE, stride, SIZE+5, 20, 20, 5);\
OPNAME ## rv40_qpel ## SIZE ## _v_lowpass(dst, full_mid, stride, SIZE, SIZE, 52, 20, 6);\
}\
\
static void OPNAME ## rv40_qpel ## SIZE ## _mc31_c(uint8_t *dst, uint8_t *src, int stride){\
uint8_t full[SIZE*(SIZE+5)];\
uint8_t * const full_mid= full + SIZE*2;\
put_rv40_qpel ## SIZE ## _h_lowpass(full, src - 2*stride, SIZE, stride, SIZE+5, 20, 52, 6);\
OPNAME ## rv40_qpel ## SIZE ## _v_lowpass(dst, full_mid, stride, SIZE, SIZE, 52, 20, 6);\
}\
\
static void OPNAME ## rv40_qpel ## SIZE ## _mc02_c(uint8_t *dst, uint8_t *src, int stride){\
OPNAME ## rv40_qpel ## SIZE ## _v_lowpass(dst, src, stride, stride, SIZE, 20, 20, 5);\
}\
\
static void OPNAME ## rv40_qpel ## SIZE ## _mc12_c(uint8_t *dst, uint8_t *src, int stride){\
uint8_t full[SIZE*(SIZE+5)];\
uint8_t * const full_mid= full + SIZE*2;\
put_rv40_qpel ## SIZE ## _h_lowpass(full, src - 2*stride, SIZE, stride, SIZE+5, 52, 20, 6);\
OPNAME ## rv40_qpel ## SIZE ## _v_lowpass(dst, full_mid, stride, SIZE, SIZE, 20, 20, 5);\
}\
\
static void OPNAME ## rv40_qpel ## SIZE ## _mc22_c(uint8_t *dst, uint8_t *src, int stride){\
uint8_t full[SIZE*(SIZE+5)];\
uint8_t * const full_mid= full + SIZE*2;\
put_rv40_qpel ## SIZE ## _h_lowpass(full, src - 2*stride, SIZE, stride, SIZE+5, 20, 20, 5);\
OPNAME ## rv40_qpel ## SIZE ## _v_lowpass(dst, full_mid, stride, SIZE, SIZE, 20, 20, 5);\
}\
\
static void OPNAME ## rv40_qpel ## SIZE ## _mc32_c(uint8_t *dst, uint8_t *src, int stride){\
uint8_t full[SIZE*(SIZE+5)];\
uint8_t * const full_mid= full + SIZE*2;\
put_rv40_qpel ## SIZE ## _h_lowpass(full, src - 2*stride, SIZE, stride, SIZE+5, 20, 52, 6);\
OPNAME ## rv40_qpel ## SIZE ## _v_lowpass(dst, full_mid, stride, SIZE, SIZE, 20, 20, 5);\
}\
\
static void OPNAME ## rv40_qpel ## SIZE ## _mc03_c(uint8_t *dst, uint8_t *src, int stride){\
OPNAME ## rv40_qpel ## SIZE ## _v_lowpass(dst, src, stride, stride, SIZE, 20, 52, 6);\
}\
\
static void OPNAME ## rv40_qpel ## SIZE ## _mc13_c(uint8_t *dst, uint8_t *src, int stride){\
uint8_t full[SIZE*(SIZE+5)];\
uint8_t * const full_mid= full + SIZE*2;\
put_rv40_qpel ## SIZE ## _h_lowpass(full, src - 2*stride, SIZE, stride, SIZE+5, 52, 20, 6);\
OPNAME ## rv40_qpel ## SIZE ## _v_lowpass(dst, full_mid, stride, SIZE, SIZE, 20, 52, 6);\
}\
\
static void OPNAME ## rv40_qpel ## SIZE ## _mc23_c(uint8_t *dst, uint8_t *src, int stride){\
uint8_t full[SIZE*(SIZE+5)];\
uint8_t * const full_mid= full + SIZE*2;\
put_rv40_qpel ## SIZE ## _h_lowpass(full, src - 2*stride, SIZE, stride, SIZE+5, 20, 20, 5);\
OPNAME ## rv40_qpel ## SIZE ## _v_lowpass(dst, full_mid, stride, SIZE, SIZE, 20, 52, 6);\
}\
\
#define op_avg(a, b) a = (((a)+cm[b]+1)>>1)
#define op_put(a, b) a = cm[b]
RV40_LOWPASS(put_ , op_put)
RV40_LOWPASS(avg_ , op_avg)
#undef op_avg
#undef op_put
RV40_MC(put_, 8)
RV40_MC(put_, 16)
RV40_MC(avg_, 8)
RV40_MC(avg_, 16)
static const int rv40_bias[4][4] = {
{ 0, 16, 32, 16 },
{ 32, 28, 32, 28 },
{ 0, 32, 16, 32 },
{ 32, 28, 32, 28 }
};
#define RV40_CHROMA_MC(OPNAME, OP)\
static void OPNAME ## rv40_chroma_mc4_c(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int stride, int h, int x, int y){\
const int A=(8-x)*(8-y);\
const int B=( x)*(8-y);\
const int C=(8-x)*( y);\
const int D=( x)*( y);\
int i;\
int bias = rv40_bias[y>>1][x>>1];\
\
assert(x<8 && y<8 && x>=0 && y>=0);\
\
if(D){\
for(i=0; i<h; i++){\
OP(dst[0], (A*src[0] + B*src[1] + C*src[stride+0] + D*src[stride+1] + bias));\
OP(dst[1], (A*src[1] + B*src[2] + C*src[stride+1] + D*src[stride+2] + bias));\
OP(dst[2], (A*src[2] + B*src[3] + C*src[stride+2] + D*src[stride+3] + bias));\
OP(dst[3], (A*src[3] + B*src[4] + C*src[stride+3] + D*src[stride+4] + bias));\
dst+= stride;\
src+= stride;\
}\
}else{\
const int E= B+C;\
const int step= C ? stride : 1;\
for(i=0; i<h; i++){\
OP(dst[0], (A*src[0] + E*src[step+0] + bias));\
OP(dst[1], (A*src[1] + E*src[step+1] + bias));\
OP(dst[2], (A*src[2] + E*src[step+2] + bias));\
OP(dst[3], (A*src[3] + E*src[step+3] + bias));\
dst+= stride;\
src+= stride;\
}\
}\
}\
\
static void OPNAME ## rv40_chroma_mc8_c(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int stride, int h, int x, int y){\
const int A=(8-x)*(8-y);\
const int B=( x)*(8-y);\
const int C=(8-x)*( y);\
const int D=( x)*( y);\
int i;\
int bias = rv40_bias[y>>1][x>>1];\
\
assert(x<8 && y<8 && x>=0 && y>=0);\
\
if(D){\
for(i=0; i<h; i++){\
OP(dst[0], (A*src[0] + B*src[1] + C*src[stride+0] + D*src[stride+1] + bias));\
OP(dst[1], (A*src[1] + B*src[2] + C*src[stride+1] + D*src[stride+2] + bias));\
OP(dst[2], (A*src[2] + B*src[3] + C*src[stride+2] + D*src[stride+3] + bias));\
OP(dst[3], (A*src[3] + B*src[4] + C*src[stride+3] + D*src[stride+4] + bias));\
OP(dst[4], (A*src[4] + B*src[5] + C*src[stride+4] + D*src[stride+5] + bias));\
OP(dst[5], (A*src[5] + B*src[6] + C*src[stride+5] + D*src[stride+6] + bias));\
OP(dst[6], (A*src[6] + B*src[7] + C*src[stride+6] + D*src[stride+7] + bias));\
OP(dst[7], (A*src[7] + B*src[8] + C*src[stride+7] + D*src[stride+8] + bias));\
dst+= stride;\
src+= stride;\
}\
}else{\
const int E= B+C;\
const int step= C ? stride : 1;\
for(i=0; i<h; i++){\
OP(dst[0], (A*src[0] + E*src[step+0] + bias));\
OP(dst[1], (A*src[1] + E*src[step+1] + bias));\
OP(dst[2], (A*src[2] + E*src[step+2] + bias));\
OP(dst[3], (A*src[3] + E*src[step+3] + bias));\
OP(dst[4], (A*src[4] + E*src[step+4] + bias));\
OP(dst[5], (A*src[5] + E*src[step+5] + bias));\
OP(dst[6], (A*src[6] + E*src[step+6] + bias));\
OP(dst[7], (A*src[7] + E*src[step+7] + bias));\
dst+= stride;\
src+= stride;\
}\
}\
}
#define op_avg(a, b) a = (((a)+((b)>>6)+1)>>1)
#define op_put(a, b) a = ((b)>>6)
RV40_CHROMA_MC(put_, op_put)
RV40_CHROMA_MC(avg_, op_avg)
void ff_rv40dsp_init(DSPContext* c, AVCodecContext *avctx) {
c->put_rv40_qpel_pixels_tab[0][ 0] = c->put_h264_qpel_pixels_tab[0][0];
c->put_rv40_qpel_pixels_tab[0][ 1] = put_rv40_qpel16_mc10_c;
c->put_rv40_qpel_pixels_tab[0][ 2] = put_rv40_qpel16_mc20_c;
c->put_rv40_qpel_pixels_tab[0][ 3] = put_rv40_qpel16_mc30_c;
c->put_rv40_qpel_pixels_tab[0][ 4] = put_rv40_qpel16_mc01_c;
c->put_rv40_qpel_pixels_tab[0][ 5] = put_rv40_qpel16_mc11_c;
c->put_rv40_qpel_pixels_tab[0][ 6] = put_rv40_qpel16_mc21_c;
c->put_rv40_qpel_pixels_tab[0][ 7] = put_rv40_qpel16_mc31_c;
c->put_rv40_qpel_pixels_tab[0][ 8] = put_rv40_qpel16_mc02_c;
c->put_rv40_qpel_pixels_tab[0][ 9] = put_rv40_qpel16_mc12_c;
c->put_rv40_qpel_pixels_tab[0][10] = put_rv40_qpel16_mc22_c;
c->put_rv40_qpel_pixels_tab[0][11] = put_rv40_qpel16_mc32_c;
c->put_rv40_qpel_pixels_tab[0][12] = put_rv40_qpel16_mc03_c;
c->put_rv40_qpel_pixels_tab[0][13] = put_rv40_qpel16_mc13_c;
c->put_rv40_qpel_pixels_tab[0][14] = put_rv40_qpel16_mc23_c;
c->avg_rv40_qpel_pixels_tab[0][ 0] = c->avg_h264_qpel_pixels_tab[0][0];
c->avg_rv40_qpel_pixels_tab[0][ 1] = avg_rv40_qpel16_mc10_c;
c->avg_rv40_qpel_pixels_tab[0][ 2] = avg_rv40_qpel16_mc20_c;
c->avg_rv40_qpel_pixels_tab[0][ 3] = avg_rv40_qpel16_mc30_c;
c->avg_rv40_qpel_pixels_tab[0][ 4] = avg_rv40_qpel16_mc01_c;
c->avg_rv40_qpel_pixels_tab[0][ 5] = avg_rv40_qpel16_mc11_c;
c->avg_rv40_qpel_pixels_tab[0][ 6] = avg_rv40_qpel16_mc21_c;
c->avg_rv40_qpel_pixels_tab[0][ 7] = avg_rv40_qpel16_mc31_c;
c->avg_rv40_qpel_pixels_tab[0][ 8] = avg_rv40_qpel16_mc02_c;
c->avg_rv40_qpel_pixels_tab[0][ 9] = avg_rv40_qpel16_mc12_c;
c->avg_rv40_qpel_pixels_tab[0][10] = avg_rv40_qpel16_mc22_c;
c->avg_rv40_qpel_pixels_tab[0][11] = avg_rv40_qpel16_mc32_c;
c->avg_rv40_qpel_pixels_tab[0][12] = avg_rv40_qpel16_mc03_c;
c->avg_rv40_qpel_pixels_tab[0][13] = avg_rv40_qpel16_mc13_c;
c->avg_rv40_qpel_pixels_tab[0][14] = avg_rv40_qpel16_mc23_c;
c->put_rv40_qpel_pixels_tab[1][ 0] = c->put_h264_qpel_pixels_tab[1][0];
c->put_rv40_qpel_pixels_tab[1][ 1] = put_rv40_qpel8_mc10_c;
c->put_rv40_qpel_pixels_tab[1][ 2] = put_rv40_qpel8_mc20_c;
c->put_rv40_qpel_pixels_tab[1][ 3] = put_rv40_qpel8_mc30_c;
c->put_rv40_qpel_pixels_tab[1][ 4] = put_rv40_qpel8_mc01_c;
c->put_rv40_qpel_pixels_tab[1][ 5] = put_rv40_qpel8_mc11_c;
c->put_rv40_qpel_pixels_tab[1][ 6] = put_rv40_qpel8_mc21_c;
c->put_rv40_qpel_pixels_tab[1][ 7] = put_rv40_qpel8_mc31_c;
c->put_rv40_qpel_pixels_tab[1][ 8] = put_rv40_qpel8_mc02_c;
c->put_rv40_qpel_pixels_tab[1][ 9] = put_rv40_qpel8_mc12_c;
c->put_rv40_qpel_pixels_tab[1][10] = put_rv40_qpel8_mc22_c;
c->put_rv40_qpel_pixels_tab[1][11] = put_rv40_qpel8_mc32_c;
c->put_rv40_qpel_pixels_tab[1][12] = put_rv40_qpel8_mc03_c;
c->put_rv40_qpel_pixels_tab[1][13] = put_rv40_qpel8_mc13_c;
c->put_rv40_qpel_pixels_tab[1][14] = put_rv40_qpel8_mc23_c;
c->avg_rv40_qpel_pixels_tab[1][ 0] = c->avg_h264_qpel_pixels_tab[1][0];
c->avg_rv40_qpel_pixels_tab[1][ 1] = avg_rv40_qpel8_mc10_c;
c->avg_rv40_qpel_pixels_tab[1][ 2] = avg_rv40_qpel8_mc20_c;
c->avg_rv40_qpel_pixels_tab[1][ 3] = avg_rv40_qpel8_mc30_c;
c->avg_rv40_qpel_pixels_tab[1][ 4] = avg_rv40_qpel8_mc01_c;
c->avg_rv40_qpel_pixels_tab[1][ 5] = avg_rv40_qpel8_mc11_c;
c->avg_rv40_qpel_pixels_tab[1][ 6] = avg_rv40_qpel8_mc21_c;
c->avg_rv40_qpel_pixels_tab[1][ 7] = avg_rv40_qpel8_mc31_c;
c->avg_rv40_qpel_pixels_tab[1][ 8] = avg_rv40_qpel8_mc02_c;
c->avg_rv40_qpel_pixels_tab[1][ 9] = avg_rv40_qpel8_mc12_c;
c->avg_rv40_qpel_pixels_tab[1][10] = avg_rv40_qpel8_mc22_c;
c->avg_rv40_qpel_pixels_tab[1][11] = avg_rv40_qpel8_mc32_c;
c->avg_rv40_qpel_pixels_tab[1][12] = avg_rv40_qpel8_mc03_c;
c->avg_rv40_qpel_pixels_tab[1][13] = avg_rv40_qpel8_mc13_c;
c->avg_rv40_qpel_pixels_tab[1][14] = avg_rv40_qpel8_mc23_c;
c->put_rv40_chroma_pixels_tab[0]= put_rv40_chroma_mc8_c;
c->put_rv40_chroma_pixels_tab[1]= put_rv40_chroma_mc4_c;
c->avg_rv40_chroma_pixels_tab[0]= avg_rv40_chroma_mc8_c;
c->avg_rv40_chroma_pixels_tab[1]= avg_rv40_chroma_mc4_c;
}
| 123linslouis-android-video-cutter | jni/libavcodec/rv40dsp.c | C | asf20 | 17,320 |
/*
* Intel Indeo 2 codec
* Copyright (c) 2005 Konstantin Shishkov
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* Intel Indeo 2 decoder.
*/
#define ALT_BITSTREAM_READER_LE
#include "avcodec.h"
#include "get_bits.h"
#include "indeo2data.h"
#include "libavutil/common.h"
typedef struct Ir2Context{
AVCodecContext *avctx;
AVFrame picture;
GetBitContext gb;
int decode_delta;
} Ir2Context;
#define CODE_VLC_BITS 14
static VLC ir2_vlc;
/* Indeo 2 codes are in range 0x01..0x7F and 0x81..0x90 */
static inline int ir2_get_code(GetBitContext *gb)
{
return get_vlc2(gb, ir2_vlc.table, CODE_VLC_BITS, 1) + 1;
}
static int ir2_decode_plane(Ir2Context *ctx, int width, int height, uint8_t *dst, int stride,
const uint8_t *table)
{
int i;
int j;
int out = 0;
int c;
int t;
if(width&1)
return -1;
/* first line contain absolute values, other lines contain deltas */
while (out < width){
c = ir2_get_code(&ctx->gb);
if(c >= 0x80) { /* we have a run */
c -= 0x7F;
if(out + c*2 > width)
return -1;
for (i = 0; i < c * 2; i++)
dst[out++] = 0x80;
} else { /* copy two values from table */
dst[out++] = table[c * 2];
dst[out++] = table[(c * 2) + 1];
}
}
dst += stride;
for (j = 1; j < height; j++){
out = 0;
while (out < width){
c = ir2_get_code(&ctx->gb);
if(c >= 0x80) { /* we have a skip */
c -= 0x7F;
if(out + c*2 > width)
return -1;
for (i = 0; i < c * 2; i++) {
dst[out] = dst[out - stride];
out++;
}
} else { /* add two deltas from table */
t = dst[out - stride] + (table[c * 2] - 128);
t= av_clip_uint8(t);
dst[out] = t;
out++;
t = dst[out - stride] + (table[(c * 2) + 1] - 128);
t= av_clip_uint8(t);
dst[out] = t;
out++;
}
}
dst += stride;
}
return 0;
}
static int ir2_decode_plane_inter(Ir2Context *ctx, int width, int height, uint8_t *dst, int stride,
const uint8_t *table)
{
int j;
int out = 0;
int c;
int t;
if(width&1)
return -1;
for (j = 0; j < height; j++){
out = 0;
while (out < width){
c = ir2_get_code(&ctx->gb);
if(c >= 0x80) { /* we have a skip */
c -= 0x7F;
out += c * 2;
} else { /* add two deltas from table */
t = dst[out] + (((table[c * 2] - 128)*3) >> 2);
t= av_clip_uint8(t);
dst[out] = t;
out++;
t = dst[out] + (((table[(c * 2) + 1] - 128)*3) >> 2);
t= av_clip_uint8(t);
dst[out] = t;
out++;
}
}
dst += stride;
}
return 0;
}
static int ir2_decode_frame(AVCodecContext *avctx,
void *data, int *data_size,
AVPacket *avpkt)
{
const uint8_t *buf = avpkt->data;
int buf_size = avpkt->size;
Ir2Context * const s = avctx->priv_data;
AVFrame *picture = data;
AVFrame * const p= (AVFrame*)&s->picture;
int start;
if(p->data[0])
avctx->release_buffer(avctx, p);
p->reference = 1;
p->buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
if (avctx->reget_buffer(avctx, p)) {
av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
return -1;
}
s->decode_delta = buf[18];
/* decide whether frame uses deltas or not */
#ifndef ALT_BITSTREAM_READER_LE
for (i = 0; i < buf_size; i++)
buf[i] = av_reverse[buf[i]];
#endif
start = 48; /* hardcoded for now */
init_get_bits(&s->gb, buf + start, buf_size - start);
if (s->decode_delta) { /* intraframe */
ir2_decode_plane(s, avctx->width, avctx->height,
s->picture.data[0], s->picture.linesize[0], ir2_luma_table);
/* swapped U and V */
ir2_decode_plane(s, avctx->width >> 2, avctx->height >> 2,
s->picture.data[2], s->picture.linesize[2], ir2_luma_table);
ir2_decode_plane(s, avctx->width >> 2, avctx->height >> 2,
s->picture.data[1], s->picture.linesize[1], ir2_luma_table);
} else { /* interframe */
ir2_decode_plane_inter(s, avctx->width, avctx->height,
s->picture.data[0], s->picture.linesize[0], ir2_luma_table);
/* swapped U and V */
ir2_decode_plane_inter(s, avctx->width >> 2, avctx->height >> 2,
s->picture.data[2], s->picture.linesize[2], ir2_luma_table);
ir2_decode_plane_inter(s, avctx->width >> 2, avctx->height >> 2,
s->picture.data[1], s->picture.linesize[1], ir2_luma_table);
}
*picture= *(AVFrame*)&s->picture;
*data_size = sizeof(AVPicture);
return buf_size;
}
static av_cold int ir2_decode_init(AVCodecContext *avctx){
Ir2Context * const ic = avctx->priv_data;
static VLC_TYPE vlc_tables[1 << CODE_VLC_BITS][2];
ic->avctx = avctx;
avctx->pix_fmt= PIX_FMT_YUV410P;
ir2_vlc.table = vlc_tables;
ir2_vlc.table_allocated = 1 << CODE_VLC_BITS;
#ifdef ALT_BITSTREAM_READER_LE
init_vlc(&ir2_vlc, CODE_VLC_BITS, IR2_CODES,
&ir2_codes[0][1], 4, 2,
&ir2_codes[0][0], 4, 2, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
#else
init_vlc(&ir2_vlc, CODE_VLC_BITS, IR2_CODES,
&ir2_codes[0][1], 4, 2,
&ir2_codes[0][0], 4, 2, INIT_VLC_USE_NEW_STATIC);
#endif
return 0;
}
static av_cold int ir2_decode_end(AVCodecContext *avctx){
Ir2Context * const ic = avctx->priv_data;
AVFrame *pic = &ic->picture;
if (pic->data[0])
avctx->release_buffer(avctx, pic);
return 0;
}
AVCodec indeo2_decoder = {
"indeo2",
AVMEDIA_TYPE_VIDEO,
CODEC_ID_INDEO2,
sizeof(Ir2Context),
ir2_decode_init,
NULL,
ir2_decode_end,
ir2_decode_frame,
CODEC_CAP_DR1,
.long_name = NULL_IF_CONFIG_SMALL("Intel Indeo 2"),
};
| 123linslouis-android-video-cutter | jni/libavcodec/indeo2.c | C | asf20 | 7,157 |
/*
* reference discrete cosine transform (double precision)
* Copyright (C) 2009 Dylan Yudaken
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef AVCODEC_DCTREF_H
#define AVCODEC_DCTREF_H
#include "dsputil.h"
void ff_ref_fdct(DCTELEM *block);
void ff_ref_idct(DCTELEM *block);
void ff_ref_dct_init(void);
#endif
| 123linslouis-android-video-cutter | jni/libavcodec/dctref.h | C | asf20 | 1,038 |
/*
* AC-3 parser
* Copyright (c) 2003 Fabrice Bellard
* Copyright (c) 2003 Michael Niedermayer
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "parser.h"
#include "ac3_parser.h"
#include "aac_ac3_parser.h"
#include "get_bits.h"
#define AC3_HEADER_SIZE 7
static const uint8_t eac3_blocks[4] = {
1, 2, 3, 6
};
int ff_ac3_parse_header(GetBitContext *gbc, AC3HeaderInfo *hdr)
{
int frame_size_code;
memset(hdr, 0, sizeof(*hdr));
hdr->sync_word = get_bits(gbc, 16);
if(hdr->sync_word != 0x0B77)
return AAC_AC3_PARSE_ERROR_SYNC;
/* read ahead to bsid to distinguish between AC-3 and E-AC-3 */
hdr->bitstream_id = show_bits_long(gbc, 29) & 0x1F;
if(hdr->bitstream_id > 16)
return AAC_AC3_PARSE_ERROR_BSID;
hdr->num_blocks = 6;
/* set default mix levels */
hdr->center_mix_level = 1; // -4.5dB
hdr->surround_mix_level = 1; // -6.0dB
if(hdr->bitstream_id <= 10) {
/* Normal AC-3 */
hdr->crc1 = get_bits(gbc, 16);
hdr->sr_code = get_bits(gbc, 2);
if(hdr->sr_code == 3)
return AAC_AC3_PARSE_ERROR_SAMPLE_RATE;
frame_size_code = get_bits(gbc, 6);
if(frame_size_code > 37)
return AAC_AC3_PARSE_ERROR_FRAME_SIZE;
skip_bits(gbc, 5); // skip bsid, already got it
skip_bits(gbc, 3); // skip bitstream mode
hdr->channel_mode = get_bits(gbc, 3);
if(hdr->channel_mode == AC3_CHMODE_STEREO) {
skip_bits(gbc, 2); // skip dsurmod
} else {
if((hdr->channel_mode & 1) && hdr->channel_mode != AC3_CHMODE_MONO)
hdr->center_mix_level = get_bits(gbc, 2);
if(hdr->channel_mode & 4)
hdr->surround_mix_level = get_bits(gbc, 2);
}
hdr->lfe_on = get_bits1(gbc);
hdr->sr_shift = FFMAX(hdr->bitstream_id, 8) - 8;
hdr->sample_rate = ff_ac3_sample_rate_tab[hdr->sr_code] >> hdr->sr_shift;
hdr->bit_rate = (ff_ac3_bitrate_tab[frame_size_code>>1] * 1000) >> hdr->sr_shift;
hdr->channels = ff_ac3_channels_tab[hdr->channel_mode] + hdr->lfe_on;
hdr->frame_size = ff_ac3_frame_size_tab[frame_size_code][hdr->sr_code] * 2;
hdr->frame_type = EAC3_FRAME_TYPE_AC3_CONVERT; //EAC3_FRAME_TYPE_INDEPENDENT;
hdr->substreamid = 0;
} else {
/* Enhanced AC-3 */
hdr->crc1 = 0;
hdr->frame_type = get_bits(gbc, 2);
if(hdr->frame_type == EAC3_FRAME_TYPE_RESERVED)
return AAC_AC3_PARSE_ERROR_FRAME_TYPE;
hdr->substreamid = get_bits(gbc, 3);
hdr->frame_size = (get_bits(gbc, 11) + 1) << 1;
if(hdr->frame_size < AC3_HEADER_SIZE)
return AAC_AC3_PARSE_ERROR_FRAME_SIZE;
hdr->sr_code = get_bits(gbc, 2);
if (hdr->sr_code == 3) {
int sr_code2 = get_bits(gbc, 2);
if(sr_code2 == 3)
return AAC_AC3_PARSE_ERROR_SAMPLE_RATE;
hdr->sample_rate = ff_ac3_sample_rate_tab[sr_code2] / 2;
hdr->sr_shift = 1;
} else {
hdr->num_blocks = eac3_blocks[get_bits(gbc, 2)];
hdr->sample_rate = ff_ac3_sample_rate_tab[hdr->sr_code];
hdr->sr_shift = 0;
}
hdr->channel_mode = get_bits(gbc, 3);
hdr->lfe_on = get_bits1(gbc);
hdr->bit_rate = (uint32_t)(8.0 * hdr->frame_size * hdr->sample_rate /
(hdr->num_blocks * 256.0));
hdr->channels = ff_ac3_channels_tab[hdr->channel_mode] + hdr->lfe_on;
}
hdr->channel_layout = ff_ac3_channel_layout_tab[hdr->channel_mode];
if (hdr->lfe_on)
hdr->channel_layout |= CH_LOW_FREQUENCY;
return 0;
}
int ff_ac3_parse_header_full(GetBitContext *gbc, AC3HeaderInfo *hdr){
int ret, i;
ret = ff_ac3_parse_header(gbc, hdr);
if(!ret){
if(hdr->bitstream_id>10){
/* Enhanced AC-3 */
skip_bits(gbc, 5); // skip bitstream id
/* skip dialog normalization and compression gain */
for (i = 0; i < (hdr->channel_mode ? 1 : 2); i++) {
skip_bits(gbc, 5); // skip dialog normalization
if (get_bits1(gbc)) {
skip_bits(gbc, 8); //skip Compression gain word
}
}
/* dependent stream channel map */
if (hdr->frame_type == EAC3_FRAME_TYPE_DEPENDENT && get_bits1(gbc)) {
hdr->channel_map = get_bits(gbc, 16); //custom channel map
return 0;
}
}
//default channel map based on acmod and lfeon
hdr->channel_map = ff_eac3_default_chmap[hdr->channel_mode];
if(hdr->lfe_on)
hdr->channel_map |= AC3_CHMAP_LFE;
}
return ret;
}
static int ac3_sync(uint64_t state, AACAC3ParseContext *hdr_info,
int *need_next_header, int *new_frame_start)
{
int err;
union {
uint64_t u64;
uint8_t u8[8];
} tmp = { be2me_64(state) };
AC3HeaderInfo hdr;
GetBitContext gbc;
init_get_bits(&gbc, tmp.u8+8-AC3_HEADER_SIZE, 54);
err = ff_ac3_parse_header(&gbc, &hdr);
if(err < 0)
return 0;
hdr_info->sample_rate = hdr.sample_rate;
hdr_info->bit_rate = hdr.bit_rate;
hdr_info->channels = hdr.channels;
hdr_info->channel_layout = hdr.channel_layout;
hdr_info->samples = hdr.num_blocks * 256;
if(hdr.bitstream_id>10)
hdr_info->codec_id = CODEC_ID_EAC3;
else if (hdr_info->codec_id == CODEC_ID_NONE)
hdr_info->codec_id = CODEC_ID_AC3;
*need_next_header = (hdr.frame_type != EAC3_FRAME_TYPE_AC3_CONVERT);
*new_frame_start = (hdr.frame_type != EAC3_FRAME_TYPE_DEPENDENT);
return hdr.frame_size;
}
static av_cold int ac3_parse_init(AVCodecParserContext *s1)
{
AACAC3ParseContext *s = s1->priv_data;
s->header_size = AC3_HEADER_SIZE;
s->sync = ac3_sync;
return 0;
}
AVCodecParser ac3_parser = {
{ CODEC_ID_AC3, CODEC_ID_EAC3 },
sizeof(AACAC3ParseContext),
ac3_parse_init,
ff_aac_ac3_parse,
ff_parse_close,
};
| 123linslouis-android-video-cutter | jni/libavcodec/ac3_parser.c | C | asf20 | 6,804 |
/*
* Wing Commander/Xan Video Decoder
* Copyright (C) 2003 the ffmpeg project
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* Xan video decoder for Wing Commander III computer game
* by Mario Brito (mbrito@student.dei.uc.pt)
* and Mike Melanson (melanson@pcisys.net)
*
* The xan_wc3 decoder outputs PAL8 data.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "libavutil/intreadwrite.h"
#include "avcodec.h"
#include "bytestream.h"
#define ALT_BITSTREAM_READER_LE
#include "get_bits.h"
// for av_memcpy_backptr
#include "libavutil/lzo.h"
typedef struct XanContext {
AVCodecContext *avctx;
AVFrame last_frame;
AVFrame current_frame;
const unsigned char *buf;
int size;
/* scratch space */
unsigned char *buffer1;
int buffer1_size;
unsigned char *buffer2;
int buffer2_size;
int frame_size;
} XanContext;
static av_cold int xan_decode_init(AVCodecContext *avctx)
{
XanContext *s = avctx->priv_data;
s->avctx = avctx;
s->frame_size = 0;
if ((avctx->codec->id == CODEC_ID_XAN_WC3) &&
(s->avctx->palctrl == NULL)) {
av_log(avctx, AV_LOG_ERROR, " WC3 Xan video: palette expected.\n");
return -1;
}
avctx->pix_fmt = PIX_FMT_PAL8;
s->buffer1_size = avctx->width * avctx->height;
s->buffer1 = av_malloc(s->buffer1_size);
if (!s->buffer1)
return -1;
s->buffer2_size = avctx->width * avctx->height;
s->buffer2 = av_malloc(s->buffer2_size + 130);
if (!s->buffer2) {
av_freep(&s->buffer1);
return -1;
}
return 0;
}
static int xan_huffman_decode(unsigned char *dest, const unsigned char *src,
int dest_len)
{
unsigned char byte = *src++;
unsigned char ival = byte + 0x16;
const unsigned char * ptr = src + byte*2;
unsigned char val = ival;
unsigned char *dest_end = dest + dest_len;
GetBitContext gb;
init_get_bits(&gb, ptr, 0); // FIXME: no src size available
while ( val != 0x16 ) {
val = src[val - 0x17 + get_bits1(&gb) * byte];
if ( val < 0x16 ) {
if (dest >= dest_end)
return 0;
*dest++ = val;
val = ival;
}
}
return 0;
}
/**
* unpack simple compression
*
* @param dest destination buffer of dest_len, must be padded with at least 130 bytes
*/
static void xan_unpack(unsigned char *dest, const unsigned char *src, int dest_len)
{
unsigned char opcode;
int size;
unsigned char *dest_end = dest + dest_len;
while (dest < dest_end) {
opcode = *src++;
if (opcode < 0xe0) {
int size2, back;
if ( (opcode & 0x80) == 0 ) {
size = opcode & 3;
back = ((opcode & 0x60) << 3) + *src++ + 1;
size2 = ((opcode & 0x1c) >> 2) + 3;
} else if ( (opcode & 0x40) == 0 ) {
size = *src >> 6;
back = (bytestream_get_be16(&src) & 0x3fff) + 1;
size2 = (opcode & 0x3f) + 4;
} else {
size = opcode & 3;
back = ((opcode & 0x10) << 12) + bytestream_get_be16(&src) + 1;
size2 = ((opcode & 0x0c) << 6) + *src++ + 5;
if (size + size2 > dest_end - dest)
return;
}
memcpy(dest, src, size); dest += size; src += size;
av_memcpy_backptr(dest, back, size2);
dest += size2;
} else {
int finish = opcode >= 0xfc;
size = finish ? opcode & 3 : ((opcode & 0x1f) << 2) + 4;
memcpy(dest, src, size); dest += size; src += size;
if (finish)
return;
}
}
}
static inline void xan_wc3_output_pixel_run(XanContext *s,
const unsigned char *pixel_buffer, int x, int y, int pixel_count)
{
int stride;
int line_inc;
int index;
int current_x;
int width = s->avctx->width;
unsigned char *palette_plane;
palette_plane = s->current_frame.data[0];
stride = s->current_frame.linesize[0];
line_inc = stride - width;
index = y * stride + x;
current_x = x;
while(pixel_count && (index < s->frame_size)) {
int count = FFMIN(pixel_count, width - current_x);
memcpy(palette_plane + index, pixel_buffer, count);
pixel_count -= count;
index += count;
pixel_buffer += count;
current_x += count;
if (current_x >= width) {
index += line_inc;
current_x = 0;
}
}
}
static inline void xan_wc3_copy_pixel_run(XanContext *s,
int x, int y, int pixel_count, int motion_x, int motion_y)
{
int stride;
int line_inc;
int curframe_index, prevframe_index;
int curframe_x, prevframe_x;
int width = s->avctx->width;
unsigned char *palette_plane, *prev_palette_plane;
palette_plane = s->current_frame.data[0];
prev_palette_plane = s->last_frame.data[0];
stride = s->current_frame.linesize[0];
line_inc = stride - width;
curframe_index = y * stride + x;
curframe_x = x;
prevframe_index = (y + motion_y) * stride + x + motion_x;
prevframe_x = x + motion_x;
while(pixel_count && (curframe_index < s->frame_size)) {
int count = FFMIN3(pixel_count, width - curframe_x, width - prevframe_x);
memcpy(palette_plane + curframe_index, prev_palette_plane + prevframe_index, count);
pixel_count -= count;
curframe_index += count;
prevframe_index += count;
curframe_x += count;
prevframe_x += count;
if (curframe_x >= width) {
curframe_index += line_inc;
curframe_x = 0;
}
if (prevframe_x >= width) {
prevframe_index += line_inc;
prevframe_x = 0;
}
}
}
static void xan_wc3_decode_frame(XanContext *s) {
int width = s->avctx->width;
int height = s->avctx->height;
int total_pixels = width * height;
unsigned char opcode;
unsigned char flag = 0;
int size = 0;
int motion_x, motion_y;
int x, y;
unsigned char *opcode_buffer = s->buffer1;
int opcode_buffer_size = s->buffer1_size;
const unsigned char *imagedata_buffer = s->buffer2;
/* pointers to segments inside the compressed chunk */
const unsigned char *huffman_segment;
const unsigned char *size_segment;
const unsigned char *vector_segment;
const unsigned char *imagedata_segment;
huffman_segment = s->buf + AV_RL16(&s->buf[0]);
size_segment = s->buf + AV_RL16(&s->buf[2]);
vector_segment = s->buf + AV_RL16(&s->buf[4]);
imagedata_segment = s->buf + AV_RL16(&s->buf[6]);
xan_huffman_decode(opcode_buffer, huffman_segment, opcode_buffer_size);
if (imagedata_segment[0] == 2)
xan_unpack(s->buffer2, &imagedata_segment[1], s->buffer2_size);
else
imagedata_buffer = &imagedata_segment[1];
/* use the decoded data segments to build the frame */
x = y = 0;
while (total_pixels) {
opcode = *opcode_buffer++;
size = 0;
switch (opcode) {
case 0:
flag ^= 1;
continue;
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
size = opcode;
break;
case 12:
case 13:
case 14:
case 15:
case 16:
case 17:
case 18:
size += (opcode - 10);
break;
case 9:
case 19:
size = *size_segment++;
break;
case 10:
case 20:
size = AV_RB16(&size_segment[0]);
size_segment += 2;
break;
case 11:
case 21:
size = AV_RB24(size_segment);
size_segment += 3;
break;
}
if (opcode < 12) {
flag ^= 1;
if (flag) {
/* run of (size) pixels is unchanged from last frame */
xan_wc3_copy_pixel_run(s, x, y, size, 0, 0);
} else {
/* output a run of pixels from imagedata_buffer */
xan_wc3_output_pixel_run(s, imagedata_buffer, x, y, size);
imagedata_buffer += size;
}
} else {
/* run-based motion compensation from last frame */
motion_x = sign_extend(*vector_segment >> 4, 4);
motion_y = sign_extend(*vector_segment & 0xF, 4);
vector_segment++;
/* copy a run of pixels from the previous frame */
xan_wc3_copy_pixel_run(s, x, y, size, motion_x, motion_y);
flag = 0;
}
/* coordinate accounting */
total_pixels -= size;
y += (x + size) / width;
x = (x + size) % width;
}
}
static void xan_wc4_decode_frame(XanContext *s) {
}
static int xan_decode_frame(AVCodecContext *avctx,
void *data, int *data_size,
AVPacket *avpkt)
{
const uint8_t *buf = avpkt->data;
int buf_size = avpkt->size;
XanContext *s = avctx->priv_data;
AVPaletteControl *palette_control = avctx->palctrl;
if (avctx->get_buffer(avctx, &s->current_frame)) {
av_log(s->avctx, AV_LOG_ERROR, " Xan Video: get_buffer() failed\n");
return -1;
}
s->current_frame.reference = 3;
if (!s->frame_size)
s->frame_size = s->current_frame.linesize[0] * s->avctx->height;
palette_control->palette_changed = 0;
memcpy(s->current_frame.data[1], palette_control->palette,
AVPALETTE_SIZE);
s->current_frame.palette_has_changed = 1;
s->buf = buf;
s->size = buf_size;
if (avctx->codec->id == CODEC_ID_XAN_WC3)
xan_wc3_decode_frame(s);
else if (avctx->codec->id == CODEC_ID_XAN_WC4)
xan_wc4_decode_frame(s);
/* release the last frame if it is allocated */
if (s->last_frame.data[0])
avctx->release_buffer(avctx, &s->last_frame);
*data_size = sizeof(AVFrame);
*(AVFrame*)data = s->current_frame;
/* shuffle frames */
FFSWAP(AVFrame, s->current_frame, s->last_frame);
/* always report that the buffer was completely consumed */
return buf_size;
}
static av_cold int xan_decode_end(AVCodecContext *avctx)
{
XanContext *s = avctx->priv_data;
/* release the frames */
if (s->last_frame.data[0])
avctx->release_buffer(avctx, &s->last_frame);
if (s->current_frame.data[0])
avctx->release_buffer(avctx, &s->current_frame);
av_freep(&s->buffer1);
av_freep(&s->buffer2);
return 0;
}
AVCodec xan_wc3_decoder = {
"xan_wc3",
AVMEDIA_TYPE_VIDEO,
CODEC_ID_XAN_WC3,
sizeof(XanContext),
xan_decode_init,
NULL,
xan_decode_end,
xan_decode_frame,
CODEC_CAP_DR1,
.long_name = NULL_IF_CONFIG_SMALL("Wing Commander III / Xan"),
};
/*
AVCodec xan_wc4_decoder = {
"xan_wc4",
AVMEDIA_TYPE_VIDEO,
CODEC_ID_XAN_WC4,
sizeof(XanContext),
xan_decode_init,
NULL,
xan_decode_end,
xan_decode_frame,
CODEC_CAP_DR1,
.long_name = NULL_IF_CONFIG_SMALL("Wing Commander IV / Xxan"),
};
*/
| 123linslouis-android-video-cutter | jni/libavcodec/xan.c | C | asf20 | 11,987 |
/*
* H.26L/H.264/AVC/JVT/14496-10/... parser
* Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* H.264 / AVC / MPEG4 part10 parser.
* @author Michael Niedermayer <michaelni@gmx.at>
*/
#include "parser.h"
#include "h264_parser.h"
#include "h264data.h"
#include "golomb.h"
#include <assert.h>
int ff_h264_find_frame_end(H264Context *h, const uint8_t *buf, int buf_size)
{
int i;
uint32_t state;
ParseContext *pc = &(h->s.parse_context);
//printf("first %02X%02X%02X%02X\n", buf[0], buf[1],buf[2],buf[3]);
// mb_addr= pc->mb_addr - 1;
state= pc->state;
if(state>13)
state= 7;
for(i=0; i<buf_size; i++){
if(state==7){
#if HAVE_FAST_UNALIGNED
/* we check i<buf_size instead of i+3/7 because its simpler
* and there should be FF_INPUT_BUFFER_PADDING_SIZE bytes at the end
*/
# if HAVE_FAST_64BIT
while(i<buf_size && !((~*(const uint64_t*)(buf+i) & (*(const uint64_t*)(buf+i) - 0x0101010101010101ULL)) & 0x8080808080808080ULL))
i+=8;
# else
while(i<buf_size && !((~*(const uint32_t*)(buf+i) & (*(const uint32_t*)(buf+i) - 0x01010101U)) & 0x80808080U))
i+=4;
# endif
#endif
for(; i<buf_size; i++){
if(!buf[i]){
state=2;
break;
}
}
}else if(state<=2){
if(buf[i]==1) state^= 5; //2->7, 1->4, 0->5
else if(buf[i]) state = 7;
else state>>=1; //2->1, 1->0, 0->0
}else if(state<=5){
int v= buf[i] & 0x1F;
if(v==6 || v==7 || v==8 || v==9){
if(pc->frame_start_found){
i++;
goto found;
}
}else if(v==1 || v==2 || v==5){
if(pc->frame_start_found){
state+=8;
continue;
}else
pc->frame_start_found = 1;
}
state= 7;
}else{
if(buf[i] & 0x80)
goto found;
state= 7;
}
}
pc->state= state;
return END_NOT_FOUND;
found:
pc->state=7;
pc->frame_start_found= 0;
return i-(state&5);
}
/*!
* Parse NAL units of found picture and decode some basic information.
*
* @param s parser context.
* @param avctx codec context.
* @param buf buffer with field/frame data.
* @param buf_size size of the buffer.
*/
static inline int parse_nal_units(AVCodecParserContext *s,
AVCodecContext *avctx,
const uint8_t *buf, int buf_size)
{
H264Context *h = s->priv_data;
const uint8_t *buf_end = buf + buf_size;
unsigned int pps_id;
unsigned int slice_type;
int state = -1;
const uint8_t *ptr;
/* set some sane default values */
s->pict_type = FF_I_TYPE;
s->key_frame = 0;
h->s.avctx= avctx;
h->sei_recovery_frame_cnt = -1;
h->sei_dpb_output_delay = 0;
h->sei_cpb_removal_delay = -1;
h->sei_buffering_period_present = 0;
for(;;) {
int src_length, dst_length, consumed;
buf = ff_find_start_code(buf, buf_end, &state);
if(buf >= buf_end)
break;
--buf;
src_length = buf_end - buf;
switch (state & 0x1f) {
case NAL_SLICE:
case NAL_IDR_SLICE:
// Do not walk the whole buffer just to decode slice header
if (src_length > 20)
src_length = 20;
break;
}
ptr= ff_h264_decode_nal(h, buf, &dst_length, &consumed, src_length);
if (ptr==NULL || dst_length < 0)
break;
init_get_bits(&h->s.gb, ptr, 8*dst_length);
switch(h->nal_unit_type) {
case NAL_SPS:
ff_h264_decode_seq_parameter_set(h);
break;
case NAL_PPS:
ff_h264_decode_picture_parameter_set(h, h->s.gb.size_in_bits);
break;
case NAL_SEI:
ff_h264_decode_sei(h);
break;
case NAL_IDR_SLICE:
s->key_frame = 1;
/* fall through */
case NAL_SLICE:
get_ue_golomb(&h->s.gb); // skip first_mb_in_slice
slice_type = get_ue_golomb_31(&h->s.gb);
s->pict_type = golomb_to_pict_type[slice_type % 5];
if (h->sei_recovery_frame_cnt >= 0) {
/* key frame, since recovery_frame_cnt is set */
s->key_frame = 1;
}
pps_id= get_ue_golomb(&h->s.gb);
if(pps_id>=MAX_PPS_COUNT) {
av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
return -1;
}
if(!h->pps_buffers[pps_id]) {
av_log(h->s.avctx, AV_LOG_ERROR, "non-existing PPS referenced\n");
return -1;
}
h->pps= *h->pps_buffers[pps_id];
if(!h->sps_buffers[h->pps.sps_id]) {
av_log(h->s.avctx, AV_LOG_ERROR, "non-existing SPS referenced\n");
return -1;
}
h->sps = *h->sps_buffers[h->pps.sps_id];
h->frame_num = get_bits(&h->s.gb, h->sps.log2_max_frame_num);
avctx->profile = h->sps.profile_idc;
avctx->level = h->sps.level_idc;
if(h->sps.frame_mbs_only_flag){
h->s.picture_structure= PICT_FRAME;
}else{
if(get_bits1(&h->s.gb)) { //field_pic_flag
h->s.picture_structure= PICT_TOP_FIELD + get_bits1(&h->s.gb); //bottom_field_flag
} else {
h->s.picture_structure= PICT_FRAME;
}
}
if(h->sps.pic_struct_present_flag) {
switch (h->sei_pic_struct) {
case SEI_PIC_STRUCT_TOP_FIELD:
case SEI_PIC_STRUCT_BOTTOM_FIELD:
s->repeat_pict = 0;
break;
case SEI_PIC_STRUCT_FRAME:
case SEI_PIC_STRUCT_TOP_BOTTOM:
case SEI_PIC_STRUCT_BOTTOM_TOP:
s->repeat_pict = 1;
break;
case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
s->repeat_pict = 2;
break;
case SEI_PIC_STRUCT_FRAME_DOUBLING:
s->repeat_pict = 3;
break;
case SEI_PIC_STRUCT_FRAME_TRIPLING:
s->repeat_pict = 5;
break;
default:
s->repeat_pict = h->s.picture_structure == PICT_FRAME ? 1 : 0;
break;
}
} else {
s->repeat_pict = h->s.picture_structure == PICT_FRAME ? 1 : 0;
}
return 0; /* no need to evaluate the rest */
}
buf += consumed;
}
/* didn't find a picture! */
av_log(h->s.avctx, AV_LOG_ERROR, "missing picture in access unit\n");
return -1;
}
static int h264_parse(AVCodecParserContext *s,
AVCodecContext *avctx,
const uint8_t **poutbuf, int *poutbuf_size,
const uint8_t *buf, int buf_size)
{
H264Context *h = s->priv_data;
ParseContext *pc = &h->s.parse_context;
int next;
if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
next= buf_size;
}else{
next= ff_h264_find_frame_end(h, buf, buf_size);
if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
*poutbuf = NULL;
*poutbuf_size = 0;
return buf_size;
}
if(next<0 && next != END_NOT_FOUND){
assert(pc->last_index + next >= 0 );
ff_h264_find_frame_end(h, &pc->buffer[pc->last_index + next], -next); //update state
}
parse_nal_units(s, avctx, buf, buf_size);
if (h->sei_cpb_removal_delay >= 0) {
s->dts_sync_point = h->sei_buffering_period_present;
s->dts_ref_dts_delta = h->sei_cpb_removal_delay;
s->pts_dts_delta = h->sei_dpb_output_delay;
} else {
s->dts_sync_point = INT_MIN;
s->dts_ref_dts_delta = INT_MIN;
s->pts_dts_delta = INT_MIN;
}
}
*poutbuf = buf;
*poutbuf_size = buf_size;
return next;
}
static int h264_split(AVCodecContext *avctx,
const uint8_t *buf, int buf_size)
{
int i;
uint32_t state = -1;
int has_sps= 0;
for(i=0; i<=buf_size; i++){
if((state&0xFFFFFF1F) == 0x107)
has_sps=1;
/* if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){
}*/
if((state&0xFFFFFF00) == 0x100 && (state&0xFFFFFF1F) != 0x107 && (state&0xFFFFFF1F) != 0x108 && (state&0xFFFFFF1F) != 0x109){
if(has_sps){
while(i>4 && buf[i-5]==0) i--;
return i-4;
}
}
if (i<buf_size)
state= (state<<8) | buf[i];
}
return 0;
}
static void close(AVCodecParserContext *s)
{
H264Context *h = s->priv_data;
ParseContext *pc = &h->s.parse_context;
av_free(pc->buffer);
ff_h264_free_context(h);
}
static int init(AVCodecParserContext *s)
{
H264Context *h = s->priv_data;
h->thread_context[0] = h;
return 0;
}
AVCodecParser h264_parser = {
{ CODEC_ID_H264 },
sizeof(H264Context),
init,
h264_parse,
close,
h264_split,
};
| 123linslouis-android-video-cutter | jni/libavcodec/h264_parser.c | C | asf20 | 10,496 |
/*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* AAN (Arai Agui Aakajima) (I)DCT tables
*/
#include <stdint.h>
const uint16_t ff_aanscales[64] = {
/* precomputed values scaled up by 14 bits */
16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
8867 , 12299, 11585, 10426, 8867, 6967, 4799, 2446,
4520 , 6270, 5906, 5315, 4520, 3552, 2446, 1247
};
const uint16_t ff_inv_aanscales[64] = {
4096, 2953, 3135, 3483, 4096, 5213, 7568, 14846,
2953, 2129, 2260, 2511, 2953, 3759, 5457, 10703,
3135, 2260, 2399, 2666, 3135, 3990, 5793, 11363,
3483, 2511, 2666, 2962, 3483, 4433, 6436, 12625,
4096, 2953, 3135, 3483, 4096, 5213, 7568, 14846,
5213, 3759, 3990, 4433, 5213, 6635, 9633, 18895,
7568, 5457, 5793, 6436, 7568, 9633, 13985, 27432,
14846, 10703, 11363, 12625, 14846, 18895, 27432, 53809,
};
| 123linslouis-android-video-cutter | jni/libavcodec/aandcttab.c | C | asf20 | 1,913 |
/*
* Musepack SV8 decoder
* Copyright (c) 2007 Konstantin Shishkov
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef AVCODEC_MPC8DATA_H
#define AVCODEC_MPC8DATA_H
#include <stdint.h>
static const int8_t mpc8_idx50[125] = {
-2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2,
-2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2,
-2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2,
-2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2,
-2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2
};
static const int8_t mpc8_idx51[125] = {
-2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
-2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
-2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
-2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
-2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2
};
static const int8_t mpc8_idx52[125] = {
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
};
static const unsigned int mpc8_thres[] = {0, 0, 3, 0, 0, 1, 3, 4, 8};
static const int8_t mpc8_huffq2[5*5*5] = {
6, 5, 4, 5, 6, 5, 4, 3, 4, 5, 4, 3, 2, 3, 4, 5, 4, 3, 4, 5, 6, 5, 4, 5,
6, 5, 4, 3, 4, 5, 4, 3, 2, 3, 4, 3, 2, 1, 2, 3, 4, 3, 2, 3, 4, 5, 4, 3,
4, 5, 4, 3, 2, 3, 4, 3, 2, 1, 2, 3, 2, 1, 0, 1, 2, 3, 2, 1, 2, 3, 4, 3,
2, 3, 4, 5, 4, 3, 4, 5, 4, 3, 2, 3, 4, 3, 2, 1, 2, 3, 4, 3, 2, 3, 4, 5,
4, 3, 4, 5, 6, 5, 4, 5, 6, 5, 4, 3, 4, 5, 4, 3, 2, 3, 4, 5, 4, 3, 4, 5,
6, 5, 4, 5, 6
};
static const uint32_t mpc8_cnk[16][32] =
{
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31},
{0, 0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, 325, 351, 378, 406, 435, 465},
{0, 0, 0, 1, 4, 10, 20, 35, 56, 84, 120, 165, 220, 286, 364, 455, 560, 680, 816, 969, 1140, 1330, 1540, 1771, 2024, 2300, 2600, 2925, 3276, 3654, 4060, 4495},
{0, 0, 0, 0, 1, 5, 15, 35, 70, 126, 210, 330, 495, 715, 1001, 1365, 1820, 2380, 3060, 3876, 4845, 5985, 7315, 8855, 10626, 12650, 14950, 17550, 20475, 23751, 27405, 31465},
{0, 0, 0, 0, 0, 1, 6, 21, 56, 126, 252, 462, 792, 1287, 2002, 3003, 4368, 6188, 8568, 11628, 15504, 20349, 26334, 33649, 42504, 53130, 65780, 80730, 98280, 118755, 142506, 169911},
{0, 0, 0, 0, 0, 0, 1, 7, 28, 84, 210, 462, 924, 1716, 3003, 5005, 8008, 12376, 18564, 27132, 38760, 54264, 74613, 100947, 134596, 177100, 230230, 296010, 376740, 475020, 593775, 736281},
{0, 0, 0, 0, 0, 0, 0, 1, 8, 36, 120, 330, 792, 1716, 3432, 6435, 11440, 19448, 31824, 50388, 77520, 116280, 170544, 245157, 346104, 480700, 657800, 888030, 1184040, 1560780, 2035800, 2629575},
{0, 0, 0, 0, 0, 0, 0, 0, 1, 9, 45, 165, 495, 1287, 3003, 6435, 12870, 24310, 43758, 75582, 125970, 203490, 319770, 490314, 735471, 1081575, 1562275, 2220075, 3108105, 4292145, 5852925, 7888725},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 10, 55, 220, 715, 2002, 5005, 11440, 24310, 48620, 92378, 167960, 293930, 497420, 817190, 1307504, 2042975, 3124550, 4686825, 6906900, 10015005, 14307150, 20160075},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 11, 66, 286, 1001, 3003, 8008, 19448, 43758, 92378, 184756, 352716, 646646, 1144066, 1961256, 3268760, 5311735, 8436285, 13123110, 20030010, 30045015, 44352165},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 12, 78, 364, 1365, 4368, 12376, 31824, 75582, 167960, 352716, 705432, 1352078, 2496144, 4457400, 7726160, 13037895, 21474180, 34597290, 54627300, 84672315},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 13, 91, 455, 1820, 6188, 18564, 50388, 125970, 293930, 646646, 1352078, 2704156, 5200300, 9657700, 17383860, 30421755, 51895935, 86493225, 141120525},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 14, 105, 560, 2380, 8568, 27132, 77520, 203490, 497420, 1144066, 2496144, 5200300, 10400600, 20058300, 37442160, 67863915, 119759850, 206253075},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 15, 120, 680, 3060, 11628, 38760, 116280, 319770, 817190, 1961256, 4457400, 9657700, 20058300, 40116600, 77558760, 145422675, 265182525},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 16, 136, 816, 3876, 15504, 54264, 170544, 490314, 1307504, 3268760, 7726160, 17383860, 37442160, 77558760, 155117520, 300540195},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 17, 153, 969, 4845, 20349, 74613, 245157, 735471, 2042975, 5311735, 13037895, 30421755, 67863915, 145422675, 300540195}
};
static const uint8_t mpc8_cnk_len[16][33] =
{
{0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6},
{0, 0, 2, 3, 4, 4, 5, 5, 6, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 0},
{0, 0, 0, 2, 4, 5, 6, 6, 7, 7, 8, 8, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 13, 13, 0},
{0, 0, 0, 0, 3, 4, 6, 7, 7, 8, 9, 9, 10, 10, 11, 11, 12, 12, 12, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 15, 16, 0},
{0, 0, 0, 0, 0, 3, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 13, 14, 14, 14, 15, 15, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 0},
{0, 0, 0, 0, 0, 0, 3, 5, 7, 8, 9, 10, 11, 12, 13, 13, 14, 15, 15, 16, 16, 17, 17, 18, 18, 18, 19, 19, 19, 20, 20, 20, 0},
{0, 0, 0, 0, 0, 0, 0, 3, 6, 7, 9, 10, 11, 12, 13, 14, 15, 15, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 21, 22, 22, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 4, 6, 8, 9, 11, 12, 13, 14, 15, 16, 17, 17, 18, 19, 19, 20, 21, 21, 22, 22, 23, 23, 23, 24, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 6, 8, 10, 11, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 21, 22, 23, 23, 24, 24, 25, 25, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 7, 9, 10, 12, 13, 15, 16, 17, 18, 19, 20, 21, 21, 22, 23, 24, 24, 25, 25, 26, 26, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 7, 9, 11, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 23, 24, 25, 26, 26, 27, 27, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 7, 9, 11, 13, 15, 16, 17, 19, 20, 21, 22, 23, 24, 25, 25, 26, 27, 28, 28, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 7, 10, 12, 14, 15, 17, 18, 19, 21, 22, 23, 24, 25, 26, 27, 27, 28, 29, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 7, 10, 12, 14, 16, 17, 19, 20, 21, 23, 24, 25, 26, 27, 28, 28, 29, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 8, 10, 12, 14, 16, 18, 19, 21, 22, 23, 25, 26, 27, 28, 29, 30, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 8, 10, 13, 15, 17, 18, 20, 21, 23, 24, 25, 27, 28, 29, 30, 0}
};
static const uint32_t mpc8_cnk_lost[16][33] =
{
{0, 0, 1, 0, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 31},
{0, 0, 1, 2, 6, 1, 11, 4, 28, 19, 9, 62, 50, 37, 23, 8, 120, 103, 85, 66, 46, 25, 3, 236, 212, 187, 161, 134, 106, 77, 47, 16, 0},
{0, 0, 0, 0, 6, 12, 29, 8, 44, 8, 91, 36, 226, 148, 57, 464, 344, 208, 55, 908, 718, 508, 277, 24, 1796, 1496, 1171, 820, 442, 36, 3697, 3232, 0},
{0, 0, 0, 0, 3, 1, 29, 58, 2, 46, 182, 17, 309, 23, 683, 228, 1716, 1036, 220, 3347, 2207, 877, 7529, 5758, 3734, 1434, 15218, 12293, 9017, 5363, 1303, 29576, 0},
{0, 0, 0, 0, 0, 2, 11, 8, 2, 4, 50, 232, 761, 46, 1093, 3824, 2004, 7816, 4756, 880, 12419, 6434, 31887, 23032, 12406, 65292, 50342, 32792, 12317, 119638, 92233, 60768, 0},
{0, 0, 0, 0, 0, 0, 1, 4, 44, 46, 50, 100, 332, 1093, 3187, 184, 4008, 14204, 5636, 26776, 11272, 56459, 30125, 127548, 85044, 31914, 228278, 147548, 49268, 454801, 312295, 142384, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 28, 8, 182, 232, 332, 664, 1757, 4944, 13320, 944, 15148, 53552, 14792, 91600, 16987, 178184, 43588, 390776, 160546, 913112, 536372, 61352, 1564729, 828448, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 7, 19, 91, 17, 761, 1093, 1757, 3514, 8458, 21778, 55490, 5102, 58654, 204518, 33974, 313105, 1015577, 534877, 1974229, 1086199, 4096463, 2535683, 499883, 6258916, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 9, 36, 309, 46, 3187, 4944, 8458, 16916, 38694, 94184, 230358, 26868, 231386, 789648, 54177, 1069754, 3701783, 1481708, 6762211, 2470066, 13394357, 5505632, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 62, 226, 23, 1093, 184, 13320, 21778, 38694, 77388, 171572, 401930, 953086, 135896, 925544, 3076873, 8340931, 3654106, 13524422, 3509417, 22756699, 2596624, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 50, 148, 683, 3824, 4008, 944, 55490, 94184, 171572, 343144, 745074, 1698160, 3931208, 662448, 3739321, 12080252, 32511574, 12481564, 49545413, 5193248, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 37, 57, 228, 2004, 14204, 15148, 5102, 230358, 401930, 745074, 1490148, 3188308, 7119516, 16170572, 3132677, 15212929, 47724503, 127314931, 42642616, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 23, 464, 1716, 7816, 5636, 53552, 58654, 26868, 953086, 1698160, 3188308, 6376616, 13496132, 29666704, 66353813, 14457878, 62182381, 189497312, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 8, 344, 1036, 4756, 26776, 14792, 204518, 231386, 135896, 3931208, 7119516, 13496132, 26992264, 56658968, 123012781, 3252931, 65435312, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 120, 208, 220, 880, 11272, 91600, 33974, 789648, 925544, 662448, 16170572, 29666704, 56658968, 113317936, 236330717, 508019104, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 103, 55, 3347, 12419, 56459, 16987, 313105, 54177, 3076873, 3739321, 3132677, 66353813, 123012781, 236330717, 0}
};
#endif /* AVCODEC_MPC8DATA_H */
| 123linslouis-android-video-cutter | jni/libavcodec/mpc8data.h | C | asf20 | 10,768 |
/*
* Nellymoser encoder
* This code is developed as part of Google Summer of Code 2008 Program.
*
* Copyright (c) 2008 Bartlomiej Wolowiec
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* Nellymoser encoder
* by Bartlomiej Wolowiec
*
* Generic codec information: libavcodec/nellymoserdec.c
*
* Some information also from: http://samples.mplayerhq.hu/A-codecs/Nelly_Moser/ASAO/ASAO.zip
* (Copyright Joseph Artsimovich and UAB "DKD")
*
* for more information about nellymoser format, visit:
* http://wiki.multimedia.cx/index.php?title=Nellymoser
*/
#include "nellymoser.h"
#include "avcodec.h"
#include "dsputil.h"
#include "fft.h"
#define BITSTREAM_WRITER_LE
#include "put_bits.h"
#define POW_TABLE_SIZE (1<<11)
#define POW_TABLE_OFFSET 3
#define OPT_SIZE ((1<<15) + 3000)
typedef struct NellyMoserEncodeContext {
AVCodecContext *avctx;
int last_frame;
int bufsel;
int have_saved;
DSPContext dsp;
FFTContext mdct_ctx;
DECLARE_ALIGNED(16, float, mdct_out)[NELLY_SAMPLES];
DECLARE_ALIGNED(16, float, in_buff)[NELLY_SAMPLES];
DECLARE_ALIGNED(16, float, buf)[2][3 * NELLY_BUF_LEN]; ///< sample buffer
float (*opt )[NELLY_BANDS];
uint8_t (*path)[NELLY_BANDS];
} NellyMoserEncodeContext;
static float pow_table[POW_TABLE_SIZE]; ///< -pow(2, -i / 2048.0 - 3.0);
static const uint8_t sf_lut[96] = {
0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 4, 4,
5, 5, 5, 6, 7, 7, 8, 8, 9, 10, 11, 11, 12, 13, 13, 14,
15, 15, 16, 17, 17, 18, 19, 19, 20, 21, 22, 22, 23, 24, 25, 26,
27, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40,
41, 41, 42, 43, 44, 45, 45, 46, 47, 48, 49, 50, 51, 52, 52, 53,
54, 55, 55, 56, 57, 57, 58, 59, 59, 60, 60, 60, 61, 61, 61, 62,
};
static const uint8_t sf_delta_lut[78] = {
0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 4, 4,
4, 5, 5, 5, 6, 6, 7, 7, 8, 8, 9, 10, 10, 11, 11, 12,
13, 13, 14, 15, 16, 17, 17, 18, 19, 19, 20, 21, 21, 22, 22, 23,
23, 24, 24, 25, 25, 25, 26, 26, 26, 26, 27, 27, 27, 27, 27, 28,
28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 30,
};
static const uint8_t quant_lut[230] = {
0,
0, 1, 2,
0, 1, 2, 3, 4, 5, 6,
0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11,
12, 13, 13, 13, 14,
0, 1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 8,
8, 9, 10, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
22, 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 29,
30,
0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3,
4, 4, 4, 5, 5, 5, 6, 6, 7, 7, 7, 8, 8, 9, 9, 9,
10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 13, 14, 14, 14, 15, 15,
15, 15, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 20, 20, 20,
21, 21, 22, 22, 23, 23, 24, 25, 26, 26, 27, 28, 29, 30, 31, 32,
33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 42, 43, 44, 44, 45, 45,
46, 47, 47, 48, 48, 49, 49, 50, 50, 50, 51, 51, 51, 52, 52, 52,
53, 53, 53, 54, 54, 54, 55, 55, 55, 56, 56, 56, 57, 57, 57, 57,
58, 58, 58, 58, 59, 59, 59, 59, 60, 60, 60, 60, 60, 61, 61, 61,
61, 61, 61, 61, 62,
};
static const float quant_lut_mul[7] = { 0.0, 0.0, 2.0, 2.0, 5.0, 12.0, 36.6 };
static const float quant_lut_add[7] = { 0.0, 0.0, 2.0, 7.0, 21.0, 56.0, 157.0 };
static const uint8_t quant_lut_offset[8] = { 0, 0, 1, 4, 11, 32, 81, 230 };
static void apply_mdct(NellyMoserEncodeContext *s)
{
memcpy(s->in_buff, s->buf[s->bufsel], NELLY_BUF_LEN * sizeof(float));
s->dsp.vector_fmul(s->in_buff, ff_sine_128, NELLY_BUF_LEN);
s->dsp.vector_fmul_reverse(s->in_buff + NELLY_BUF_LEN, s->buf[s->bufsel] + NELLY_BUF_LEN, ff_sine_128,
NELLY_BUF_LEN);
ff_mdct_calc(&s->mdct_ctx, s->mdct_out, s->in_buff);
s->dsp.vector_fmul(s->buf[s->bufsel] + NELLY_BUF_LEN, ff_sine_128, NELLY_BUF_LEN);
s->dsp.vector_fmul_reverse(s->buf[s->bufsel] + 2 * NELLY_BUF_LEN, s->buf[1 - s->bufsel], ff_sine_128,
NELLY_BUF_LEN);
ff_mdct_calc(&s->mdct_ctx, s->mdct_out + NELLY_BUF_LEN, s->buf[s->bufsel] + NELLY_BUF_LEN);
}
static av_cold int encode_init(AVCodecContext *avctx)
{
NellyMoserEncodeContext *s = avctx->priv_data;
int i;
if (avctx->channels != 1) {
av_log(avctx, AV_LOG_ERROR, "Nellymoser supports only 1 channel\n");
return -1;
}
if (avctx->sample_rate != 8000 && avctx->sample_rate != 16000 &&
avctx->sample_rate != 11025 &&
avctx->sample_rate != 22050 && avctx->sample_rate != 44100 &&
avctx->strict_std_compliance >= FF_COMPLIANCE_NORMAL) {
av_log(avctx, AV_LOG_ERROR, "Nellymoser works only with 8000, 16000, 11025, 22050 and 44100 sample rate\n");
return -1;
}
avctx->frame_size = NELLY_SAMPLES;
s->avctx = avctx;
ff_mdct_init(&s->mdct_ctx, 8, 0, 1.0);
dsputil_init(&s->dsp, avctx);
/* Generate overlap window */
ff_sine_window_init(ff_sine_128, 128);
for (i = 0; i < POW_TABLE_SIZE; i++)
pow_table[i] = -pow(2, -i / 2048.0 - 3.0 + POW_TABLE_OFFSET);
if (s->avctx->trellis) {
s->opt = av_malloc(NELLY_BANDS * OPT_SIZE * sizeof(float ));
s->path = av_malloc(NELLY_BANDS * OPT_SIZE * sizeof(uint8_t));
}
return 0;
}
static av_cold int encode_end(AVCodecContext *avctx)
{
NellyMoserEncodeContext *s = avctx->priv_data;
ff_mdct_end(&s->mdct_ctx);
if (s->avctx->trellis) {
av_free(s->opt);
av_free(s->path);
}
return 0;
}
#define find_best(val, table, LUT, LUT_add, LUT_size) \
best_idx = \
LUT[av_clip ((lrintf(val) >> 8) + LUT_add, 0, LUT_size - 1)]; \
if (fabs(val - table[best_idx]) > fabs(val - table[best_idx + 1])) \
best_idx++;
static void get_exponent_greedy(NellyMoserEncodeContext *s, float *cand, int *idx_table)
{
int band, best_idx, power_idx = 0;
float power_candidate;
//base exponent
find_best(cand[0], ff_nelly_init_table, sf_lut, -20, 96);
idx_table[0] = best_idx;
power_idx = ff_nelly_init_table[best_idx];
for (band = 1; band < NELLY_BANDS; band++) {
power_candidate = cand[band] - power_idx;
find_best(power_candidate, ff_nelly_delta_table, sf_delta_lut, 37, 78);
idx_table[band] = best_idx;
power_idx += ff_nelly_delta_table[best_idx];
}
}
static inline float distance(float x, float y, int band)
{
//return pow(fabs(x-y), 2.0);
float tmp = x - y;
return tmp * tmp;
}
static void get_exponent_dynamic(NellyMoserEncodeContext *s, float *cand, int *idx_table)
{
int i, j, band, best_idx;
float power_candidate, best_val;
float (*opt )[NELLY_BANDS] = s->opt ;
uint8_t(*path)[NELLY_BANDS] = s->path;
for (i = 0; i < NELLY_BANDS * OPT_SIZE; i++) {
opt[0][i] = INFINITY;
}
for (i = 0; i < 64; i++) {
opt[0][ff_nelly_init_table[i]] = distance(cand[0], ff_nelly_init_table[i], 0);
path[0][ff_nelly_init_table[i]] = i;
}
for (band = 1; band < NELLY_BANDS; band++) {
int q, c = 0;
float tmp;
int idx_min, idx_max, idx;
power_candidate = cand[band];
for (q = 1000; !c && q < OPT_SIZE; q <<= 2) {
idx_min = FFMAX(0, cand[band] - q);
idx_max = FFMIN(OPT_SIZE, cand[band - 1] + q);
for (i = FFMAX(0, cand[band - 1] - q); i < FFMIN(OPT_SIZE, cand[band - 1] + q); i++) {
if ( isinf(opt[band - 1][i]) )
continue;
for (j = 0; j < 32; j++) {
idx = i + ff_nelly_delta_table[j];
if (idx > idx_max)
break;
if (idx >= idx_min) {
tmp = opt[band - 1][i] + distance(idx, power_candidate, band);
if (opt[band][idx] > tmp) {
opt[band][idx] = tmp;
path[band][idx] = j;
c = 1;
}
}
}
}
}
assert(c); //FIXME
}
best_val = INFINITY;
best_idx = -1;
band = NELLY_BANDS - 1;
for (i = 0; i < OPT_SIZE; i++) {
if (best_val > opt[band][i]) {
best_val = opt[band][i];
best_idx = i;
}
}
for (band = NELLY_BANDS - 1; band >= 0; band--) {
idx_table[band] = path[band][best_idx];
if (band) {
best_idx -= ff_nelly_delta_table[path[band][best_idx]];
}
}
}
/**
* Encodes NELLY_SAMPLES samples. It assumes, that samples contains 3 * NELLY_BUF_LEN values
* @param s encoder context
* @param output output buffer
* @param output_size size of output buffer
*/
static void encode_block(NellyMoserEncodeContext *s, unsigned char *output, int output_size)
{
PutBitContext pb;
int i, j, band, block, best_idx, power_idx = 0;
float power_val, coeff, coeff_sum;
float pows[NELLY_FILL_LEN];
int bits[NELLY_BUF_LEN], idx_table[NELLY_BANDS];
float cand[NELLY_BANDS];
apply_mdct(s);
init_put_bits(&pb, output, output_size * 8);
i = 0;
for (band = 0; band < NELLY_BANDS; band++) {
coeff_sum = 0;
for (j = 0; j < ff_nelly_band_sizes_table[band]; i++, j++) {
coeff_sum += s->mdct_out[i ] * s->mdct_out[i ]
+ s->mdct_out[i + NELLY_BUF_LEN] * s->mdct_out[i + NELLY_BUF_LEN];
}
cand[band] =
log(FFMAX(1.0, coeff_sum / (ff_nelly_band_sizes_table[band] << 7))) * 1024.0 / M_LN2;
}
if (s->avctx->trellis) {
get_exponent_dynamic(s, cand, idx_table);
} else {
get_exponent_greedy(s, cand, idx_table);
}
i = 0;
for (band = 0; band < NELLY_BANDS; band++) {
if (band) {
power_idx += ff_nelly_delta_table[idx_table[band]];
put_bits(&pb, 5, idx_table[band]);
} else {
power_idx = ff_nelly_init_table[idx_table[0]];
put_bits(&pb, 6, idx_table[0]);
}
power_val = pow_table[power_idx & 0x7FF] / (1 << ((power_idx >> 11) + POW_TABLE_OFFSET));
for (j = 0; j < ff_nelly_band_sizes_table[band]; i++, j++) {
s->mdct_out[i] *= power_val;
s->mdct_out[i + NELLY_BUF_LEN] *= power_val;
pows[i] = power_idx;
}
}
ff_nelly_get_sample_bits(pows, bits);
for (block = 0; block < 2; block++) {
for (i = 0; i < NELLY_FILL_LEN; i++) {
if (bits[i] > 0) {
const float *table = ff_nelly_dequantization_table + (1 << bits[i]) - 1;
coeff = s->mdct_out[block * NELLY_BUF_LEN + i];
best_idx =
quant_lut[av_clip (
coeff * quant_lut_mul[bits[i]] + quant_lut_add[bits[i]],
quant_lut_offset[bits[i]],
quant_lut_offset[bits[i]+1] - 1
)];
if (fabs(coeff - table[best_idx]) > fabs(coeff - table[best_idx + 1]))
best_idx++;
put_bits(&pb, bits[i], best_idx);
}
}
if (!block)
put_bits(&pb, NELLY_HEADER_BITS + NELLY_DETAIL_BITS - put_bits_count(&pb), 0);
}
flush_put_bits(&pb);
}
static int encode_frame(AVCodecContext *avctx, uint8_t *frame, int buf_size, void *data)
{
NellyMoserEncodeContext *s = avctx->priv_data;
int16_t *samples = data;
int i;
if (s->last_frame)
return 0;
if (data) {
for (i = 0; i < avctx->frame_size; i++) {
s->buf[s->bufsel][i] = samples[i];
}
for (; i < NELLY_SAMPLES; i++) {
s->buf[s->bufsel][i] = 0;
}
s->bufsel = 1 - s->bufsel;
if (!s->have_saved) {
s->have_saved = 1;
return 0;
}
} else {
memset(s->buf[s->bufsel], 0, sizeof(s->buf[0][0]) * NELLY_BUF_LEN);
s->bufsel = 1 - s->bufsel;
s->last_frame = 1;
}
if (s->have_saved) {
encode_block(s, frame, buf_size);
return NELLY_BLOCK_LEN;
}
return 0;
}
AVCodec nellymoser_encoder = {
.name = "nellymoser",
.type = AVMEDIA_TYPE_AUDIO,
.id = CODEC_ID_NELLYMOSER,
.priv_data_size = sizeof(NellyMoserEncodeContext),
.init = encode_init,
.encode = encode_frame,
.close = encode_end,
.capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY,
.long_name = NULL_IF_CONFIG_SMALL("Nellymoser Asao"),
};
| 123linslouis-android-video-cutter | jni/libavcodec/nellymoserenc.c | C | asf20 | 13,493 |
/*
* H.264 MP4 to Annex B byte stream format filter
* Copyright (c) 2007 Benoit Fouet <benoit.fouet@free.fr>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "libavutil/intreadwrite.h"
#include "avcodec.h"
typedef struct H264BSFContext {
uint8_t length_size;
uint8_t first_idr;
uint8_t *sps_pps_data;
uint32_t size;
} H264BSFContext;
static void alloc_and_copy(uint8_t **poutbuf, int *poutbuf_size,
const uint8_t *sps_pps, uint32_t sps_pps_size,
const uint8_t *in, uint32_t in_size) {
uint32_t offset = *poutbuf_size;
uint8_t nal_header_size = offset ? 3 : 4;
*poutbuf_size += sps_pps_size+in_size+nal_header_size;
*poutbuf = av_realloc(*poutbuf, *poutbuf_size);
if (sps_pps)
memcpy(*poutbuf+offset, sps_pps, sps_pps_size);
memcpy(*poutbuf+sps_pps_size+nal_header_size+offset, in, in_size);
if (!offset)
AV_WB32(*poutbuf+sps_pps_size, 1);
else {
(*poutbuf+offset+sps_pps_size)[0] = (*poutbuf+offset+sps_pps_size)[1] = 0;
(*poutbuf+offset+sps_pps_size)[2] = 1;
}
}
static int h264_mp4toannexb_filter(AVBitStreamFilterContext *bsfc,
AVCodecContext *avctx, const char *args,
uint8_t **poutbuf, int *poutbuf_size,
const uint8_t *buf, int buf_size,
int keyframe) {
H264BSFContext *ctx = bsfc->priv_data;
uint8_t unit_type;
int32_t nal_size;
uint32_t cumul_size = 0;
const uint8_t *buf_end = buf + buf_size;
/* nothing to filter */
if (!avctx->extradata || avctx->extradata_size < 6) {
*poutbuf = (uint8_t*) buf;
*poutbuf_size = buf_size;
return 0;
}
/* retrieve sps and pps NAL units from extradata */
if (!ctx->sps_pps_data) {
uint16_t unit_size;
uint32_t total_size = 0;
uint8_t *out = NULL, unit_nb, sps_done = 0;
const uint8_t *extradata = avctx->extradata+4;
static const uint8_t nalu_header[4] = {0, 0, 0, 1};
/* retrieve length coded size */
ctx->length_size = (*extradata++ & 0x3) + 1;
if (ctx->length_size == 3)
return AVERROR(EINVAL);
/* retrieve sps and pps unit(s) */
unit_nb = *extradata++ & 0x1f; /* number of sps unit(s) */
if (!unit_nb) {
unit_nb = *extradata++; /* number of pps unit(s) */
sps_done++;
}
while (unit_nb--) {
unit_size = AV_RB16(extradata);
total_size += unit_size+4;
if (extradata+2+unit_size > avctx->extradata+avctx->extradata_size) {
av_free(out);
return AVERROR(EINVAL);
}
out = av_realloc(out, total_size);
if (!out)
return AVERROR(ENOMEM);
memcpy(out+total_size-unit_size-4, nalu_header, 4);
memcpy(out+total_size-unit_size, extradata+2, unit_size);
extradata += 2+unit_size;
if (!unit_nb && !sps_done++)
unit_nb = *extradata++; /* number of pps unit(s) */
}
ctx->sps_pps_data = out;
ctx->size = total_size;
ctx->first_idr = 1;
}
*poutbuf_size = 0;
*poutbuf = NULL;
do {
if (buf + ctx->length_size > buf_end)
goto fail;
if (ctx->length_size == 1)
nal_size = buf[0];
else if (ctx->length_size == 2)
nal_size = AV_RB16(buf);
else
nal_size = AV_RB32(buf);
buf += ctx->length_size;
unit_type = *buf & 0x1f;
if (buf + nal_size > buf_end || nal_size < 0)
goto fail;
/* prepend only to the first type 5 NAL unit of an IDR picture */
if (ctx->first_idr && unit_type == 5) {
alloc_and_copy(poutbuf, poutbuf_size,
ctx->sps_pps_data, ctx->size,
buf, nal_size);
ctx->first_idr = 0;
}
else {
alloc_and_copy(poutbuf, poutbuf_size,
NULL, 0,
buf, nal_size);
if (!ctx->first_idr && unit_type == 1)
ctx->first_idr = 1;
}
buf += nal_size;
cumul_size += nal_size + ctx->length_size;
} while (cumul_size < buf_size);
return 1;
fail:
av_freep(poutbuf);
*poutbuf_size = 0;
return AVERROR(EINVAL);
}
static void h264_mp4toannexb_close(AVBitStreamFilterContext *bsfc)
{
H264BSFContext *ctx = bsfc->priv_data;
av_freep(&ctx->sps_pps_data);
}
AVBitStreamFilter h264_mp4toannexb_bsf = {
"h264_mp4toannexb",
sizeof(H264BSFContext),
h264_mp4toannexb_filter,
h264_mp4toannexb_close,
};
| 123linslouis-android-video-cutter | jni/libavcodec/h264_mp4toannexb_bsf.c | C | asf20 | 5,562 |
/*
* Electronic Arts TGQ/TQI/MAD IDCT algorithm
* Copyright (c) 2007-2008 Peter Ross <pross@xvid.org>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* Electronic Arts TGQ/TQI/MAD IDCT algorithm
* @author Peter Ross <pross@xvid.org>
*/
#include "dsputil.h"
#define ASQRT 181 /* (1/sqrt(2))<<8 */
#define A4 669 /* cos(pi/8)*sqrt(2)<<9 */
#define A2 277 /* sin(pi/8)*sqrt(2)<<9 */
#define A5 196 /* sin(pi/8)<<9 */
#define IDCT_TRANSFORM(dest,s0,s1,s2,s3,s4,s5,s6,s7,d0,d1,d2,d3,d4,d5,d6,d7,munge,src) {\
const int a1 = (src)[s1] + (src)[s7]; \
const int a7 = (src)[s1] - (src)[s7]; \
const int a5 = (src)[s5] + (src)[s3]; \
const int a3 = (src)[s5] - (src)[s3]; \
const int a2 = (src)[s2] + (src)[s6]; \
const int a6 = (ASQRT*((src)[s2] - (src)[s6]))>>8; \
const int a0 = (src)[s0] + (src)[s4]; \
const int a4 = (src)[s0] - (src)[s4]; \
const int b0 = (((A4-A5)*a7 - A5*a3)>>9) + a1+a5; \
const int b1 = (((A4-A5)*a7 - A5*a3)>>9) + ((ASQRT*(a1-a5))>>8); \
const int b2 = (((A2+A5)*a3 + A5*a7)>>9) + ((ASQRT*(a1-a5))>>8); \
const int b3 = ((A2+A5)*a3 + A5*a7)>>9; \
(dest)[d0] = munge(a0+a2+a6+b0); \
(dest)[d1] = munge(a4+a6 +b1); \
(dest)[d2] = munge(a4-a6 +b2); \
(dest)[d3] = munge(a0-a2-a6+b3); \
(dest)[d4] = munge(a0-a2-a6-b3); \
(dest)[d5] = munge(a4-a6 -b2); \
(dest)[d6] = munge(a4+a6 -b1); \
(dest)[d7] = munge(a0+a2+a6-b0); \
}
/* end IDCT_TRANSFORM macro */
#define MUNGE_NONE(x) (x)
#define IDCT_COL(dest,src) IDCT_TRANSFORM(dest,0,8,16,24,32,40,48,56,0,8,16,24,32,40,48,56,MUNGE_NONE,src)
#define MUNGE_8BIT(x) av_clip_uint8((x)>>4)
#define IDCT_ROW(dest,src) IDCT_TRANSFORM(dest,0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7,MUNGE_8BIT,src)
static inline void ea_idct_col(DCTELEM *dest, const DCTELEM *src) {
if ((src[8]|src[16]|src[24]|src[32]|src[40]|src[48]|src[56])==0) {
dest[0] =
dest[8] =
dest[16] =
dest[24] =
dest[32] =
dest[40] =
dest[48] =
dest[56] = src[0];
}else
IDCT_COL(dest, src);
}
void ff_ea_idct_put_c(uint8_t *dest, int linesize, DCTELEM *block) {
int i;
DCTELEM temp[64];
block[0] += 4;
for (i=0; i<8; i++)
ea_idct_col(&temp[i], &block[i]);
for (i=0; i<8; i++)
IDCT_ROW( (&dest[i*linesize]), (&temp[8*i]) );
}
| 123linslouis-android-video-cutter | jni/libavcodec/eaidct.c | C | asf20 | 3,089 |
/*
* Copyright (C) 2003 Mike Melanson
* Copyright (C) 2003 Dr. Tim Ferguson
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* id RoQ Video common functions based on work by Dr. Tim Ferguson
*/
#include "avcodec.h"
#include "roqvideo.h"
static inline void block_copy(unsigned char *out, unsigned char *in,
int outstride, int instride, int sz)
{
int rows = sz;
while(rows--) {
memcpy(out, in, sz);
out += outstride;
in += instride;
}
}
void ff_apply_vector_2x2(RoqContext *ri, int x, int y, roq_cell *cell)
{
unsigned char *bptr;
int boffs,stride;
stride = ri->current_frame->linesize[0];
boffs = y*stride + x;
bptr = ri->current_frame->data[0] + boffs;
bptr[0 ] = cell->y[0];
bptr[1 ] = cell->y[1];
bptr[stride ] = cell->y[2];
bptr[stride+1] = cell->y[3];
stride = ri->current_frame->linesize[1];
boffs = y*stride + x;
bptr = ri->current_frame->data[1] + boffs;
bptr[0 ] =
bptr[1 ] =
bptr[stride ] =
bptr[stride+1] = cell->u;
bptr = ri->current_frame->data[2] + boffs;
bptr[0 ] =
bptr[1 ] =
bptr[stride ] =
bptr[stride+1] = cell->v;
}
void ff_apply_vector_4x4(RoqContext *ri, int x, int y, roq_cell *cell)
{
unsigned char *bptr;
int boffs,stride;
stride = ri->current_frame->linesize[0];
boffs = y*stride + x;
bptr = ri->current_frame->data[0] + boffs;
bptr[ 0] = bptr[ 1] = bptr[stride ] = bptr[stride +1] = cell->y[0];
bptr[ 2] = bptr[ 3] = bptr[stride +2] = bptr[stride +3] = cell->y[1];
bptr[stride*2 ] = bptr[stride*2+1] = bptr[stride*3 ] = bptr[stride*3+1] = cell->y[2];
bptr[stride*2+2] = bptr[stride*2+3] = bptr[stride*3+2] = bptr[stride*3+3] = cell->y[3];
stride = ri->current_frame->linesize[1];
boffs = y*stride + x;
bptr = ri->current_frame->data[1] + boffs;
bptr[ 0] = bptr[ 1] = bptr[stride ] = bptr[stride +1] =
bptr[ 2] = bptr[ 3] = bptr[stride +2] = bptr[stride +3] =
bptr[stride*2 ] = bptr[stride*2+1] = bptr[stride*3 ] = bptr[stride*3+1] =
bptr[stride*2+2] = bptr[stride*2+3] = bptr[stride*3+2] = bptr[stride*3+3] = cell->u;
bptr = ri->current_frame->data[2] + boffs;
bptr[ 0] = bptr[ 1] = bptr[stride ] = bptr[stride +1] =
bptr[ 2] = bptr[ 3] = bptr[stride +2] = bptr[stride +3] =
bptr[stride*2 ] = bptr[stride*2+1] = bptr[stride*3 ] = bptr[stride*3+1] =
bptr[stride*2+2] = bptr[stride*2+3] = bptr[stride*3+2] = bptr[stride*3+3] = cell->v;
}
static inline void apply_motion_generic(RoqContext *ri, int x, int y, int deltax,
int deltay, int sz)
{
int mx, my, cp;
mx = x + deltax;
my = y + deltay;
/* check MV against frame boundaries */
if ((mx < 0) || (mx > ri->width - sz) ||
(my < 0) || (my > ri->height - sz)) {
av_log(ri->avctx, AV_LOG_ERROR, "motion vector out of bounds: MV = (%d, %d), boundaries = (0, 0, %d, %d)\n",
mx, my, ri->width, ri->height);
return;
}
for(cp = 0; cp < 3; cp++) {
int outstride = ri->current_frame->linesize[cp];
int instride = ri->last_frame ->linesize[cp];
block_copy(ri->current_frame->data[cp] + y*outstride + x,
ri->last_frame->data[cp] + my*instride + mx,
outstride, instride, sz);
}
}
void ff_apply_motion_4x4(RoqContext *ri, int x, int y,
int deltax, int deltay)
{
apply_motion_generic(ri, x, y, deltax, deltay, 4);
}
void ff_apply_motion_8x8(RoqContext *ri, int x, int y,
int deltax, int deltay)
{
apply_motion_generic(ri, x, y, deltax, deltay, 8);
}
| 123linslouis-android-video-cutter | jni/libavcodec/roqvideo.c | C | asf20 | 4,596 |
/*
* MPEG1 / MPEG2 video parser
* Copyright (c) 2000,2001 Fabrice Bellard
* Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "parser.h"
#include "mpegvideo.h"
static void mpegvideo_extract_headers(AVCodecParserContext *s,
AVCodecContext *avctx,
const uint8_t *buf, int buf_size)
{
ParseContext1 *pc = s->priv_data;
const uint8_t *buf_end = buf + buf_size;
uint32_t start_code;
int frame_rate_index, ext_type, bytes_left;
int frame_rate_ext_n, frame_rate_ext_d;
int picture_structure, top_field_first, repeat_first_field, progressive_frame;
int horiz_size_ext, vert_size_ext, bit_rate_ext;
int did_set_size=0;
//FIXME replace the crap with get_bits()
s->repeat_pict = 0;
while (buf < buf_end) {
start_code= -1;
buf= ff_find_start_code(buf, buf_end, &start_code);
bytes_left = buf_end - buf;
switch(start_code) {
case PICTURE_START_CODE:
if (bytes_left >= 2) {
s->pict_type = (buf[1] >> 3) & 7;
}
break;
case SEQ_START_CODE:
if (bytes_left >= 7) {
pc->width = (buf[0] << 4) | (buf[1] >> 4);
pc->height = ((buf[1] & 0x0f) << 8) | buf[2];
if(!avctx->width || !avctx->height || !avctx->coded_width || !avctx->coded_height){
avcodec_set_dimensions(avctx, pc->width, pc->height);
did_set_size=1;
}
frame_rate_index = buf[3] & 0xf;
pc->frame_rate.den = avctx->time_base.den = ff_frame_rate_tab[frame_rate_index].num;
pc->frame_rate.num = avctx->time_base.num = ff_frame_rate_tab[frame_rate_index].den;
avctx->bit_rate = ((buf[4]<<10) | (buf[5]<<2) | (buf[6]>>6))*400;
avctx->codec_id = CODEC_ID_MPEG1VIDEO;
avctx->sub_id = 1;
}
break;
case EXT_START_CODE:
if (bytes_left >= 1) {
ext_type = (buf[0] >> 4);
switch(ext_type) {
case 0x1: /* sequence extension */
if (bytes_left >= 6) {
horiz_size_ext = ((buf[1] & 1) << 1) | (buf[2] >> 7);
vert_size_ext = (buf[2] >> 5) & 3;
bit_rate_ext = ((buf[2] & 0x1F)<<7) | (buf[3]>>1);
frame_rate_ext_n = (buf[5] >> 5) & 3;
frame_rate_ext_d = (buf[5] & 0x1f);
pc->progressive_sequence = buf[1] & (1 << 3);
avctx->has_b_frames= !(buf[5] >> 7);
pc->width |=(horiz_size_ext << 12);
pc->height |=( vert_size_ext << 12);
avctx->bit_rate += (bit_rate_ext << 18) * 400;
if(did_set_size)
avcodec_set_dimensions(avctx, pc->width, pc->height);
avctx->time_base.den = pc->frame_rate.den * (frame_rate_ext_n + 1) * 2;
avctx->time_base.num = pc->frame_rate.num * (frame_rate_ext_d + 1);
avctx->codec_id = CODEC_ID_MPEG2VIDEO;
avctx->sub_id = 2; /* forces MPEG2 */
}
break;
case 0x8: /* picture coding extension */
if (bytes_left >= 5) {
picture_structure = buf[2]&3;
top_field_first = buf[3] & (1 << 7);
repeat_first_field = buf[3] & (1 << 1);
progressive_frame = buf[4] & (1 << 7);
/* check if we must repeat the frame */
s->repeat_pict = 1;
if (repeat_first_field) {
if (pc->progressive_sequence) {
if (top_field_first)
s->repeat_pict = 5;
else
s->repeat_pict = 3;
} else if (progressive_frame) {
s->repeat_pict = 2;
}
}
}
break;
}
}
break;
case -1:
goto the_end;
default:
/* we stop parsing when we encounter a slice. It ensures
that this function takes a negligible amount of time */
if (start_code >= SLICE_MIN_START_CODE &&
start_code <= SLICE_MAX_START_CODE)
goto the_end;
break;
}
}
the_end: ;
}
static int mpegvideo_parse(AVCodecParserContext *s,
AVCodecContext *avctx,
const uint8_t **poutbuf, int *poutbuf_size,
const uint8_t *buf, int buf_size)
{
ParseContext1 *pc1 = s->priv_data;
ParseContext *pc= &pc1->pc;
int next;
if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
next= buf_size;
}else{
next= ff_mpeg1_find_frame_end(pc, buf, buf_size, s);
if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
*poutbuf = NULL;
*poutbuf_size = 0;
return buf_size;
}
}
/* we have a full frame : we just parse the first few MPEG headers
to have the full timing information. The time take by this
function should be negligible for uncorrupted streams */
mpegvideo_extract_headers(s, avctx, buf, buf_size);
#if 0
printf("pict_type=%d frame_rate=%0.3f repeat_pict=%d\n",
s->pict_type, (double)avctx->time_base.den / avctx->time_base.num, s->repeat_pict);
#endif
*poutbuf = buf;
*poutbuf_size = buf_size;
return next;
}
static int mpegvideo_split(AVCodecContext *avctx,
const uint8_t *buf, int buf_size)
{
int i;
uint32_t state= -1;
for(i=0; i<buf_size; i++){
state= (state<<8) | buf[i];
if(state != 0x1B3 && state != 0x1B5 && state < 0x200 && state >= 0x100)
return i-3;
}
return 0;
}
AVCodecParser mpegvideo_parser = {
{ CODEC_ID_MPEG1VIDEO, CODEC_ID_MPEG2VIDEO },
sizeof(ParseContext1),
NULL,
mpegvideo_parse,
ff_parse1_close,
mpegvideo_split,
};
| 123linslouis-android-video-cutter | jni/libavcodec/mpegvideo_parser.c | C | asf20 | 7,232 |
/*
* SVQ1 decoder
* ported to MPlayer by Arpi <arpi@thot.banki.hu>
* ported to libavcodec by Nick Kurshev <nickols_k@mail.ru>
*
* Copyright (C) 2002 the xine project
* Copyright (C) 2002 the ffmpeg project
*
* SVQ1 Encoder (c) 2004 Mike Melanson <melanson@pcisys.net>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* Sorenson Vector Quantizer #1 (SVQ1) video codec.
* For more information of the SVQ1 algorithm, visit:
* http://www.pcisys.net/~melanson/codecs/
*/
//#define DEBUG_SVQ1
#include "avcodec.h"
#include "dsputil.h"
#include "mpegvideo.h"
#include "mathops.h"
#include "svq1.h"
#undef NDEBUG
#include <assert.h>
extern const uint8_t mvtab[33][2];
static VLC svq1_block_type;
static VLC svq1_motion_component;
static VLC svq1_intra_multistage[6];
static VLC svq1_inter_multistage[6];
static VLC svq1_intra_mean;
static VLC svq1_inter_mean;
/* motion vector (prediction) */
typedef struct svq1_pmv_s {
int x;
int y;
} svq1_pmv;
static const uint16_t checksum_table[256] = {
0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0
};
static const uint8_t string_table[256] = {
0x00, 0xD5, 0x7F, 0xAA, 0xFE, 0x2B, 0x81, 0x54,
0x29, 0xFC, 0x56, 0x83, 0xD7, 0x02, 0xA8, 0x7D,
0x52, 0x87, 0x2D, 0xF8, 0xAC, 0x79, 0xD3, 0x06,
0x7B, 0xAE, 0x04, 0xD1, 0x85, 0x50, 0xFA, 0x2F,
0xA4, 0x71, 0xDB, 0x0E, 0x5A, 0x8F, 0x25, 0xF0,
0x8D, 0x58, 0xF2, 0x27, 0x73, 0xA6, 0x0C, 0xD9,
0xF6, 0x23, 0x89, 0x5C, 0x08, 0xDD, 0x77, 0xA2,
0xDF, 0x0A, 0xA0, 0x75, 0x21, 0xF4, 0x5E, 0x8B,
0x9D, 0x48, 0xE2, 0x37, 0x63, 0xB6, 0x1C, 0xC9,
0xB4, 0x61, 0xCB, 0x1E, 0x4A, 0x9F, 0x35, 0xE0,
0xCF, 0x1A, 0xB0, 0x65, 0x31, 0xE4, 0x4E, 0x9B,
0xE6, 0x33, 0x99, 0x4C, 0x18, 0xCD, 0x67, 0xB2,
0x39, 0xEC, 0x46, 0x93, 0xC7, 0x12, 0xB8, 0x6D,
0x10, 0xC5, 0x6F, 0xBA, 0xEE, 0x3B, 0x91, 0x44,
0x6B, 0xBE, 0x14, 0xC1, 0x95, 0x40, 0xEA, 0x3F,
0x42, 0x97, 0x3D, 0xE8, 0xBC, 0x69, 0xC3, 0x16,
0xEF, 0x3A, 0x90, 0x45, 0x11, 0xC4, 0x6E, 0xBB,
0xC6, 0x13, 0xB9, 0x6C, 0x38, 0xED, 0x47, 0x92,
0xBD, 0x68, 0xC2, 0x17, 0x43, 0x96, 0x3C, 0xE9,
0x94, 0x41, 0xEB, 0x3E, 0x6A, 0xBF, 0x15, 0xC0,
0x4B, 0x9E, 0x34, 0xE1, 0xB5, 0x60, 0xCA, 0x1F,
0x62, 0xB7, 0x1D, 0xC8, 0x9C, 0x49, 0xE3, 0x36,
0x19, 0xCC, 0x66, 0xB3, 0xE7, 0x32, 0x98, 0x4D,
0x30, 0xE5, 0x4F, 0x9A, 0xCE, 0x1B, 0xB1, 0x64,
0x72, 0xA7, 0x0D, 0xD8, 0x8C, 0x59, 0xF3, 0x26,
0x5B, 0x8E, 0x24, 0xF1, 0xA5, 0x70, 0xDA, 0x0F,
0x20, 0xF5, 0x5F, 0x8A, 0xDE, 0x0B, 0xA1, 0x74,
0x09, 0xDC, 0x76, 0xA3, 0xF7, 0x22, 0x88, 0x5D,
0xD6, 0x03, 0xA9, 0x7C, 0x28, 0xFD, 0x57, 0x82,
0xFF, 0x2A, 0x80, 0x55, 0x01, 0xD4, 0x7E, 0xAB,
0x84, 0x51, 0xFB, 0x2E, 0x7A, 0xAF, 0x05, 0xD0,
0xAD, 0x78, 0xD2, 0x07, 0x53, 0x86, 0x2C, 0xF9
};
#define SVQ1_PROCESS_VECTOR()\
for (; level > 0; i++) {\
/* process next depth */\
if (i == m) {\
m = n;\
if (--level == 0)\
break;\
}\
/* divide block if next bit set */\
if (get_bits1 (bitbuf) == 0)\
break;\
/* add child nodes */\
list[n++] = list[i];\
list[n++] = list[i] + (((level & 1) ? pitch : 1) << ((level / 2) + 1));\
}
#define SVQ1_ADD_CODEBOOK()\
/* add codebook entries to vector */\
for (j=0; j < stages; j++) {\
n3 = codebook[entries[j]] ^ 0x80808080;\
n1 += ((n3 & 0xFF00FF00) >> 8);\
n2 += (n3 & 0x00FF00FF);\
}\
\
/* clip to [0..255] */\
if (n1 & 0xFF00FF00) {\
n3 = ((( n1 >> 15) & 0x00010001) | 0x01000100) - 0x00010001;\
n1 += 0x7F007F00;\
n1 |= (((~n1 >> 15) & 0x00010001) | 0x01000100) - 0x00010001;\
n1 &= (n3 & 0x00FF00FF);\
}\
\
if (n2 & 0xFF00FF00) {\
n3 = ((( n2 >> 15) & 0x00010001) | 0x01000100) - 0x00010001;\
n2 += 0x7F007F00;\
n2 |= (((~n2 >> 15) & 0x00010001) | 0x01000100) - 0x00010001;\
n2 &= (n3 & 0x00FF00FF);\
}
#define SVQ1_DO_CODEBOOK_INTRA()\
for (y=0; y < height; y++) {\
for (x=0; x < (width / 4); x++, codebook++) {\
n1 = n4;\
n2 = n4;\
SVQ1_ADD_CODEBOOK()\
/* store result */\
dst[x] = (n1 << 8) | n2;\
}\
dst += (pitch / 4);\
}
#define SVQ1_DO_CODEBOOK_NONINTRA()\
for (y=0; y < height; y++) {\
for (x=0; x < (width / 4); x++, codebook++) {\
n3 = dst[x];\
/* add mean value to vector */\
n1 = ((n3 & 0xFF00FF00) >> 8) + n4;\
n2 = (n3 & 0x00FF00FF) + n4;\
SVQ1_ADD_CODEBOOK()\
/* store result */\
dst[x] = (n1 << 8) | n2;\
}\
dst += (pitch / 4);\
}
#define SVQ1_CALC_CODEBOOK_ENTRIES(cbook)\
codebook = (const uint32_t *) cbook[level];\
bit_cache = get_bits (bitbuf, 4*stages);\
/* calculate codebook entries for this vector */\
for (j=0; j < stages; j++) {\
entries[j] = (((bit_cache >> (4*(stages - j - 1))) & 0xF) + 16*j) << (level + 1);\
}\
mean -= (stages * 128);\
n4 = ((mean + (mean >> 31)) << 16) | (mean & 0xFFFF);
static int svq1_decode_block_intra (GetBitContext *bitbuf, uint8_t *pixels, int pitch ) {
uint32_t bit_cache;
uint8_t *list[63];
uint32_t *dst;
const uint32_t *codebook;
int entries[6];
int i, j, m, n;
int mean, stages;
unsigned x, y, width, height, level;
uint32_t n1, n2, n3, n4;
/* initialize list for breadth first processing of vectors */
list[0] = pixels;
/* recursively process vector */
for (i=0, m=1, n=1, level=5; i < n; i++) {
SVQ1_PROCESS_VECTOR();
/* destination address and vector size */
dst = (uint32_t *) list[i];
width = 1 << ((4 + level) /2);
height = 1 << ((3 + level) /2);
/* get number of stages (-1 skips vector, 0 for mean only) */
stages = get_vlc2(bitbuf, svq1_intra_multistage[level].table, 3, 3) - 1;
if (stages == -1) {
for (y=0; y < height; y++) {
memset (&dst[y*(pitch / 4)], 0, width);
}
continue; /* skip vector */
}
if ((stages > 0) && (level >= 4)) {
#ifdef DEBUG_SVQ1
av_log(s->avctx, AV_LOG_INFO, "Error (svq1_decode_block_intra): invalid vector: stages=%i level=%i\n",stages,level);
#endif
return -1; /* invalid vector */
}
mean = get_vlc2(bitbuf, svq1_intra_mean.table, 8, 3);
if (stages == 0) {
for (y=0; y < height; y++) {
memset (&dst[y*(pitch / 4)], mean, width);
}
} else {
SVQ1_CALC_CODEBOOK_ENTRIES(ff_svq1_intra_codebooks);
SVQ1_DO_CODEBOOK_INTRA()
}
}
return 0;
}
static int svq1_decode_block_non_intra (GetBitContext *bitbuf, uint8_t *pixels, int pitch ) {
uint32_t bit_cache;
uint8_t *list[63];
uint32_t *dst;
const uint32_t *codebook;
int entries[6];
int i, j, m, n;
int mean, stages;
int x, y, width, height, level;
uint32_t n1, n2, n3, n4;
/* initialize list for breadth first processing of vectors */
list[0] = pixels;
/* recursively process vector */
for (i=0, m=1, n=1, level=5; i < n; i++) {
SVQ1_PROCESS_VECTOR();
/* destination address and vector size */
dst = (uint32_t *) list[i];
width = 1 << ((4 + level) /2);
height = 1 << ((3 + level) /2);
/* get number of stages (-1 skips vector, 0 for mean only) */
stages = get_vlc2(bitbuf, svq1_inter_multistage[level].table, 3, 2) - 1;
if (stages == -1) continue; /* skip vector */
if ((stages > 0) && (level >= 4)) {
#ifdef DEBUG_SVQ1
av_log(s->avctx, AV_LOG_INFO, "Error (svq1_decode_block_non_intra): invalid vector: stages=%i level=%i\n",stages,level);
#endif
return -1; /* invalid vector */
}
mean = get_vlc2(bitbuf, svq1_inter_mean.table, 9, 3) - 256;
SVQ1_CALC_CODEBOOK_ENTRIES(ff_svq1_inter_codebooks);
SVQ1_DO_CODEBOOK_NONINTRA()
}
return 0;
}
static int svq1_decode_motion_vector (GetBitContext *bitbuf, svq1_pmv *mv, svq1_pmv **pmv) {
int diff;
int i;
for (i=0; i < 2; i++) {
/* get motion code */
diff = get_vlc2(bitbuf, svq1_motion_component.table, 7, 2);
if(diff<0)
return -1;
else if(diff){
if(get_bits1(bitbuf)) diff= -diff;
}
/* add median of motion vector predictors and clip result */
if (i == 1)
mv->y = ((diff + mid_pred(pmv[0]->y, pmv[1]->y, pmv[2]->y)) << 26) >> 26;
else
mv->x = ((diff + mid_pred(pmv[0]->x, pmv[1]->x, pmv[2]->x)) << 26) >> 26;
}
return 0;
}
static void svq1_skip_block (uint8_t *current, uint8_t *previous, int pitch, int x, int y) {
uint8_t *src;
uint8_t *dst;
int i;
src = &previous[x + y*pitch];
dst = current;
for (i=0; i < 16; i++) {
memcpy (dst, src, 16);
src += pitch;
dst += pitch;
}
}
static int svq1_motion_inter_block (MpegEncContext *s, GetBitContext *bitbuf,
uint8_t *current, uint8_t *previous, int pitch,
svq1_pmv *motion, int x, int y) {
uint8_t *src;
uint8_t *dst;
svq1_pmv mv;
svq1_pmv *pmv[3];
int result;
/* predict and decode motion vector */
pmv[0] = &motion[0];
if (y == 0) {
pmv[1] =
pmv[2] = pmv[0];
}
else {
pmv[1] = &motion[(x / 8) + 2];
pmv[2] = &motion[(x / 8) + 4];
}
result = svq1_decode_motion_vector (bitbuf, &mv, pmv);
if (result != 0)
return result;
motion[0].x =
motion[(x / 8) + 2].x =
motion[(x / 8) + 3].x = mv.x;
motion[0].y =
motion[(x / 8) + 2].y =
motion[(x / 8) + 3].y = mv.y;
if(y + (mv.y >> 1)<0)
mv.y= 0;
if(x + (mv.x >> 1)<0)
mv.x= 0;
#if 0
int w= (s->width+15)&~15;
int h= (s->height+15)&~15;
if(x + (mv.x >> 1)<0 || y + (mv.y >> 1)<0 || x + (mv.x >> 1) + 16 > w || y + (mv.y >> 1) + 16> h)
av_log(s->avctx, AV_LOG_INFO, "%d %d %d %d\n", x, y, x + (mv.x >> 1), y + (mv.y >> 1));
#endif
src = &previous[(x + (mv.x >> 1)) + (y + (mv.y >> 1))*pitch];
dst = current;
s->dsp.put_pixels_tab[0][((mv.y & 1) << 1) | (mv.x & 1)](dst,src,pitch,16);
return 0;
}
static int svq1_motion_inter_4v_block (MpegEncContext *s, GetBitContext *bitbuf,
uint8_t *current, uint8_t *previous, int pitch,
svq1_pmv *motion,int x, int y) {
uint8_t *src;
uint8_t *dst;
svq1_pmv mv;
svq1_pmv *pmv[4];
int i, result;
/* predict and decode motion vector (0) */
pmv[0] = &motion[0];
if (y == 0) {
pmv[1] =
pmv[2] = pmv[0];
}
else {
pmv[1] = &motion[(x / 8) + 2];
pmv[2] = &motion[(x / 8) + 4];
}
result = svq1_decode_motion_vector (bitbuf, &mv, pmv);
if (result != 0)
return result;
/* predict and decode motion vector (1) */
pmv[0] = &mv;
if (y == 0) {
pmv[1] =
pmv[2] = pmv[0];
}
else {
pmv[1] = &motion[(x / 8) + 3];
}
result = svq1_decode_motion_vector (bitbuf, &motion[0], pmv);
if (result != 0)
return result;
/* predict and decode motion vector (2) */
pmv[1] = &motion[0];
pmv[2] = &motion[(x / 8) + 1];
result = svq1_decode_motion_vector (bitbuf, &motion[(x / 8) + 2], pmv);
if (result != 0)
return result;
/* predict and decode motion vector (3) */
pmv[2] = &motion[(x / 8) + 2];
pmv[3] = &motion[(x / 8) + 3];
result = svq1_decode_motion_vector (bitbuf, pmv[3], pmv);
if (result != 0)
return result;
/* form predictions */
for (i=0; i < 4; i++) {
int mvx= pmv[i]->x + (i&1)*16;
int mvy= pmv[i]->y + (i>>1)*16;
///XXX /FIXME clipping or padding?
if(y + (mvy >> 1)<0)
mvy= 0;
if(x + (mvx >> 1)<0)
mvx= 0;
#if 0
int w= (s->width+15)&~15;
int h= (s->height+15)&~15;
if(x + (mvx >> 1)<0 || y + (mvy >> 1)<0 || x + (mvx >> 1) + 8 > w || y + (mvy >> 1) + 8> h)
av_log(s->avctx, AV_LOG_INFO, "%d %d %d %d\n", x, y, x + (mvx >> 1), y + (mvy >> 1));
#endif
src = &previous[(x + (mvx >> 1)) + (y + (mvy >> 1))*pitch];
dst = current;
s->dsp.put_pixels_tab[1][((mvy & 1) << 1) | (mvx & 1)](dst,src,pitch,8);
/* select next block */
if (i & 1) {
current += 8*(pitch - 1);
} else {
current += 8;
}
}
return 0;
}
static int svq1_decode_delta_block (MpegEncContext *s, GetBitContext *bitbuf,
uint8_t *current, uint8_t *previous, int pitch,
svq1_pmv *motion, int x, int y) {
uint32_t block_type;
int result = 0;
/* get block type */
block_type = get_vlc2(bitbuf, svq1_block_type.table, 2, 2);
/* reset motion vectors */
if (block_type == SVQ1_BLOCK_SKIP || block_type == SVQ1_BLOCK_INTRA) {
motion[0].x =
motion[0].y =
motion[(x / 8) + 2].x =
motion[(x / 8) + 2].y =
motion[(x / 8) + 3].x =
motion[(x / 8) + 3].y = 0;
}
switch (block_type) {
case SVQ1_BLOCK_SKIP:
svq1_skip_block (current, previous, pitch, x, y);
break;
case SVQ1_BLOCK_INTER:
result = svq1_motion_inter_block (s, bitbuf, current, previous, pitch, motion, x, y);
if (result != 0)
{
#ifdef DEBUG_SVQ1
av_log(s->avctx, AV_LOG_INFO, "Error in svq1_motion_inter_block %i\n",result);
#endif
break;
}
result = svq1_decode_block_non_intra (bitbuf, current, pitch);
break;
case SVQ1_BLOCK_INTER_4V:
result = svq1_motion_inter_4v_block (s, bitbuf, current, previous, pitch, motion, x, y);
if (result != 0)
{
#ifdef DEBUG_SVQ1
av_log(s->avctx, AV_LOG_INFO, "Error in svq1_motion_inter_4v_block %i\n",result);
#endif
break;
}
result = svq1_decode_block_non_intra (bitbuf, current, pitch);
break;
case SVQ1_BLOCK_INTRA:
result = svq1_decode_block_intra (bitbuf, current, pitch);
break;
}
return result;
}
uint16_t ff_svq1_packet_checksum (const uint8_t *data, const int length, int value) {
int i;
for (i=0; i < length; i++) {
value = checksum_table[data[i] ^ (value >> 8)] ^ ((value & 0xFF) << 8);
}
return value;
}
static void svq1_parse_string (GetBitContext *bitbuf, uint8_t *out) {
uint8_t seed;
int i;
out[0] = get_bits (bitbuf, 8);
seed = string_table[out[0]];
for (i=1; i <= out[0]; i++) {
out[i] = get_bits (bitbuf, 8) ^ seed;
seed = string_table[out[i] ^ seed];
}
}
static int svq1_decode_frame_header (GetBitContext *bitbuf,MpegEncContext *s) {
int frame_size_code;
int temporal_reference;
temporal_reference = get_bits (bitbuf, 8);
/* frame type */
s->pict_type= get_bits (bitbuf, 2)+1;
if(s->pict_type==4)
return -1;
if (s->pict_type == FF_I_TYPE) {
/* unknown fields */
if (s->f_code == 0x50 || s->f_code == 0x60) {
int csum = get_bits (bitbuf, 16);
csum = ff_svq1_packet_checksum (bitbuf->buffer, bitbuf->size_in_bits>>3, csum);
// av_log(s->avctx, AV_LOG_INFO, "%s checksum (%02x) for packet data\n",
// (csum == 0) ? "correct" : "incorrect", csum);
}
if ((s->f_code ^ 0x10) >= 0x50) {
uint8_t msg[256];
svq1_parse_string (bitbuf, msg);
av_log(s->avctx, AV_LOG_INFO, "embedded message: \"%s\"\n", (char *) msg);
}
skip_bits (bitbuf, 2);
skip_bits (bitbuf, 2);
skip_bits1 (bitbuf);
/* load frame size */
frame_size_code = get_bits (bitbuf, 3);
if (frame_size_code == 7) {
/* load width, height (12 bits each) */
s->width = get_bits (bitbuf, 12);
s->height = get_bits (bitbuf, 12);
if (!s->width || !s->height)
return -1;
} else {
/* get width, height from table */
s->width = ff_svq1_frame_size_table[frame_size_code].width;
s->height = ff_svq1_frame_size_table[frame_size_code].height;
}
}
/* unknown fields */
if (get_bits1 (bitbuf) == 1) {
skip_bits1 (bitbuf); /* use packet checksum if (1) */
skip_bits1 (bitbuf); /* component checksums after image data if (1) */
if (get_bits (bitbuf, 2) != 0)
return -1;
}
if (get_bits1 (bitbuf) == 1) {
skip_bits1 (bitbuf);
skip_bits (bitbuf, 4);
skip_bits1 (bitbuf);
skip_bits (bitbuf, 2);
while (get_bits1 (bitbuf) == 1) {
skip_bits (bitbuf, 8);
}
}
return 0;
}
static int svq1_decode_frame(AVCodecContext *avctx,
void *data, int *data_size,
AVPacket *avpkt)
{
const uint8_t *buf = avpkt->data;
int buf_size = avpkt->size;
MpegEncContext *s=avctx->priv_data;
uint8_t *current, *previous;
int result, i, x, y, width, height;
AVFrame *pict = data;
/* initialize bit buffer */
init_get_bits(&s->gb,buf,buf_size*8);
/* decode frame header */
s->f_code = get_bits (&s->gb, 22);
if ((s->f_code & ~0x70) || !(s->f_code & 0x60))
return -1;
/* swap some header bytes (why?) */
if (s->f_code != 0x20) {
uint32_t *src = (uint32_t *) (buf + 4);
for (i=0; i < 4; i++) {
src[i] = ((src[i] << 16) | (src[i] >> 16)) ^ src[7 - i];
}
}
result = svq1_decode_frame_header (&s->gb, s);
if (result != 0)
{
#ifdef DEBUG_SVQ1
av_log(s->avctx, AV_LOG_INFO, "Error in svq1_decode_frame_header %i\n",result);
#endif
return result;
}
//FIXME this avoids some confusion for "B frames" without 2 references
//this should be removed after libavcodec can handle more flexible picture types & ordering
if(s->pict_type==FF_B_TYPE && s->last_picture_ptr==NULL) return buf_size;
if(avctx->hurry_up && s->pict_type==FF_B_TYPE) return buf_size;
if( (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==FF_B_TYPE)
||(avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=FF_I_TYPE)
|| avctx->skip_frame >= AVDISCARD_ALL)
return buf_size;
if(MPV_frame_start(s, avctx) < 0)
return -1;
/* decode y, u and v components */
for (i=0; i < 3; i++) {
int linesize;
if (i == 0) {
width = FFALIGN(s->width, 16);
height = FFALIGN(s->height, 16);
linesize= s->linesize;
} else {
if(s->flags&CODEC_FLAG_GRAY) break;
width = FFALIGN(s->width/4, 16);
height = FFALIGN(s->height/4, 16);
linesize= s->uvlinesize;
}
current = s->current_picture.data[i];
if(s->pict_type==FF_B_TYPE){
previous = s->next_picture.data[i];
}else{
previous = s->last_picture.data[i];
}
if (s->pict_type == FF_I_TYPE) {
/* keyframe */
for (y=0; y < height; y+=16) {
for (x=0; x < width; x+=16) {
result = svq1_decode_block_intra (&s->gb, ¤t[x], linesize);
if (result != 0)
{
//#ifdef DEBUG_SVQ1
av_log(s->avctx, AV_LOG_INFO, "Error in svq1_decode_block %i (keyframe)\n",result);
//#endif
return result;
}
}
current += 16*linesize;
}
} else {
svq1_pmv pmv[width/8+3];
/* delta frame */
memset (pmv, 0, ((width / 8) + 3) * sizeof(svq1_pmv));
for (y=0; y < height; y+=16) {
for (x=0; x < width; x+=16) {
result = svq1_decode_delta_block (s, &s->gb, ¤t[x], previous,
linesize, pmv, x, y);
if (result != 0)
{
#ifdef DEBUG_SVQ1
av_log(s->avctx, AV_LOG_INFO, "Error in svq1_decode_delta_block %i\n",result);
#endif
return result;
}
}
pmv[0].x =
pmv[0].y = 0;
current += 16*linesize;
}
}
}
*pict = *(AVFrame*)&s->current_picture;
MPV_frame_end(s);
*data_size=sizeof(AVFrame);
return buf_size;
}
static av_cold int svq1_decode_init(AVCodecContext *avctx)
{
MpegEncContext *s = avctx->priv_data;
int i;
int offset = 0;
MPV_decode_defaults(s);
s->avctx = avctx;
s->width = (avctx->width+3)&~3;
s->height = (avctx->height+3)&~3;
s->codec_id= avctx->codec->id;
avctx->pix_fmt = PIX_FMT_YUV410P;
avctx->has_b_frames= 1; // not true, but DP frames and these behave like unidirectional b frames
s->flags= avctx->flags;
if (MPV_common_init(s) < 0) return -1;
INIT_VLC_STATIC(&svq1_block_type, 2, 4,
&ff_svq1_block_type_vlc[0][1], 2, 1,
&ff_svq1_block_type_vlc[0][0], 2, 1, 6);
INIT_VLC_STATIC(&svq1_motion_component, 7, 33,
&mvtab[0][1], 2, 1,
&mvtab[0][0], 2, 1, 176);
for (i = 0; i < 6; i++) {
static const uint8_t sizes[2][6] = {{14, 10, 14, 18, 16, 18}, {10, 10, 14, 14, 14, 16}};
static VLC_TYPE table[168][2];
svq1_intra_multistage[i].table = &table[offset];
svq1_intra_multistage[i].table_allocated = sizes[0][i];
offset += sizes[0][i];
init_vlc(&svq1_intra_multistage[i], 3, 8,
&ff_svq1_intra_multistage_vlc[i][0][1], 2, 1,
&ff_svq1_intra_multistage_vlc[i][0][0], 2, 1, INIT_VLC_USE_NEW_STATIC);
svq1_inter_multistage[i].table = &table[offset];
svq1_inter_multistage[i].table_allocated = sizes[1][i];
offset += sizes[1][i];
init_vlc(&svq1_inter_multistage[i], 3, 8,
&ff_svq1_inter_multistage_vlc[i][0][1], 2, 1,
&ff_svq1_inter_multistage_vlc[i][0][0], 2, 1, INIT_VLC_USE_NEW_STATIC);
}
INIT_VLC_STATIC(&svq1_intra_mean, 8, 256,
&ff_svq1_intra_mean_vlc[0][1], 4, 2,
&ff_svq1_intra_mean_vlc[0][0], 4, 2, 632);
INIT_VLC_STATIC(&svq1_inter_mean, 9, 512,
&ff_svq1_inter_mean_vlc[0][1], 4, 2,
&ff_svq1_inter_mean_vlc[0][0], 4, 2, 1434);
return 0;
}
static av_cold int svq1_decode_end(AVCodecContext *avctx)
{
MpegEncContext *s = avctx->priv_data;
MPV_common_end(s);
return 0;
}
AVCodec svq1_decoder = {
"svq1",
AVMEDIA_TYPE_VIDEO,
CODEC_ID_SVQ1,
sizeof(MpegEncContext),
svq1_decode_init,
NULL,
svq1_decode_end,
svq1_decode_frame,
CODEC_CAP_DR1,
.flush= ff_mpeg_flush,
.pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV410P, PIX_FMT_NONE},
.long_name= NULL_IF_CONFIG_SMALL("Sorenson Vector Quantizer 1 / Sorenson Video 1 / SVQ1"),
};
| 123linslouis-android-video-cutter | jni/libavcodec/svq1dec.c | C | asf20 | 24,974 |
/*
* Common AAC and AC-3 parser prototypes
* Copyright (c) 2003 Fabrice Bellard
* Copyright (c) 2003 Michael Niedermayer
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef AVCODEC_AAC_AC3_PARSER_H
#define AVCODEC_AAC_AC3_PARSER_H
#include <stdint.h>
#include "avcodec.h"
#include "parser.h"
typedef enum {
AAC_AC3_PARSE_ERROR_SYNC = -1,
AAC_AC3_PARSE_ERROR_BSID = -2,
AAC_AC3_PARSE_ERROR_SAMPLE_RATE = -3,
AAC_AC3_PARSE_ERROR_FRAME_SIZE = -4,
AAC_AC3_PARSE_ERROR_FRAME_TYPE = -5,
AAC_AC3_PARSE_ERROR_CRC = -6,
AAC_AC3_PARSE_ERROR_CHANNEL_CFG = -7,
} AACAC3ParseError;
typedef struct AACAC3ParseContext {
ParseContext pc;
int frame_size;
int header_size;
int (*sync)(uint64_t state, struct AACAC3ParseContext *hdr_info,
int *need_next_header, int *new_frame_start);
int channels;
int sample_rate;
int bit_rate;
int samples;
int64_t channel_layout;
int remaining_size;
uint64_t state;
int need_next_header;
enum CodecID codec_id;
} AACAC3ParseContext;
int ff_aac_ac3_parse(AVCodecParserContext *s1,
AVCodecContext *avctx,
const uint8_t **poutbuf, int *poutbuf_size,
const uint8_t *buf, int buf_size);
#endif /* AVCODEC_AAC_AC3_PARSER_H */
| 123linslouis-android-video-cutter | jni/libavcodec/aac_ac3_parser.h | C | asf20 | 2,048 |
/*
* Video Decode and Presentation API for UNIX (VDPAU) is used for
* HW decode acceleration for MPEG-1/2, H.264 and VC-1.
*
* Copyright (C) 2008 NVIDIA
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef AVCODEC_VDPAU_INTERNAL_H
#define AVCODEC_VDPAU_INTERNAL_H
#include <stdint.h>
#include "mpegvideo.h"
void ff_vdpau_add_data_chunk(MpegEncContext *s, const uint8_t *buf,
int buf_size);
void ff_vdpau_mpeg_picture_complete(MpegEncContext *s, const uint8_t *buf,
int buf_size, int slice_count);
void ff_vdpau_h264_picture_start(MpegEncContext *s);
void ff_vdpau_h264_set_reference_frames(MpegEncContext *s);
void ff_vdpau_h264_picture_complete(MpegEncContext *s);
void ff_vdpau_vc1_decode_picture(MpegEncContext *s, const uint8_t *buf,
int buf_size);
void ff_vdpau_mpeg4_decode_picture(MpegEncContext *s, const uint8_t *buf,
int buf_size);
#endif /* AVCODEC_VDPAU_INTERNAL_H */
| 123linslouis-android-video-cutter | jni/libavcodec/vdpau_internal.h | C | asf20 | 1,743 |
/*
* Generate a file for hardcoded tables
*
* Copyright (c) 2009 Reimar Döffinger <Reimar.Doeffinger@gmx.de>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef AVCODEC_TABLEPRINT_H
#define AVCODEC_TABLEPRINT_H
#include <stdint.h>
#include <stdio.h>
#define WRITE_1D_FUNC_ARGV(name, type, linebrk, fmtstr, ...)\
void write_##name##_array(const type *data, int len)\
{\
int i;\
printf(" ");\
for (i = 0; i < len - 1; i++) {\
printf(" "fmtstr",", __VA_ARGS__);\
if ((i & linebrk) == linebrk) printf("\n ");\
}\
printf(" "fmtstr"\n", __VA_ARGS__);\
}
#define WRITE_1D_FUNC(name, type, fmtstr, linebrk)\
WRITE_1D_FUNC_ARGV(name, type, linebrk, fmtstr, data[i])
#define WRITE_2D_FUNC(name, type)\
void write_##name##_2d_array(const void *arg, int len, int len2)\
{\
const type *data = arg;\
int i;\
printf(" {\n");\
for (i = 0; i < len; i++) {\
write_##name##_array(data + i * len2, len2);\
printf(i == len - 1 ? " }\n" : " }, {\n");\
}\
}
/**
* \defgroup printfuncs Predefined functions for printing tables
*
* \{
*/
void write_int8_array (const int8_t *, int);
void write_uint8_array (const uint8_t *, int);
void write_uint16_array (const uint16_t *, int);
void write_uint32_array (const uint32_t *, int);
void write_float_array (const float *, int);
void write_int8_2d_array (const void *, int, int);
void write_uint8_2d_array (const void *, int, int);
void write_uint32_2d_array(const void *, int, int);
/** \} */ // end of printfuncs group
/** Write a standard file header */
void write_fileheader(void);
#endif /* AVCODEC_TABLEPRINT_H */
| 123linslouis-android-video-cutter | jni/libavcodec/tableprint.h | C | asf20 | 2,382 |
/*
* Copyright (C) 2004-2010 Michael Niedermayer <michaelni@gmx.at>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef AVCODEC_DWT_H
#define AVCODEC_DWT_H
#include <stdint.h>
typedef int DWTELEM;
typedef short IDWTELEM;
typedef struct {
IDWTELEM *b0;
IDWTELEM *b1;
IDWTELEM *b2;
IDWTELEM *b3;
int y;
} DWTCompose;
/** Used to minimize the amount of memory used in order to optimize cache performance. **/
typedef struct slice_buffer_s {
IDWTELEM * * line; ///< For use by idwt and predict_slices.
IDWTELEM * * data_stack; ///< Used for internal purposes.
int data_stack_top;
int line_count;
int line_width;
int data_count;
IDWTELEM * base_buffer; ///< Buffer that this structure is caching.
} slice_buffer;
typedef struct DWTContext {
void (*vertical_compose97i)(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, IDWTELEM *b3, IDWTELEM *b4, IDWTELEM *b5, int width);
void (*horizontal_compose97i)(IDWTELEM *b, int width);
void (*inner_add_yblock)(const uint8_t *obmc, const int obmc_stride, uint8_t * * block, int b_w, int b_h, int src_x, int src_y, int src_stride, slice_buffer * sb, int add, uint8_t * dst8);
} DWTContext;
#define MAX_DECOMPOSITIONS 8
#define DWT_97 0
#define DWT_53 1
#define liftS lift
#if 1
#define W_AM 3
#define W_AO 0
#define W_AS 1
#undef liftS
#define W_BM 1
#define W_BO 8
#define W_BS 4
#define W_CM 1
#define W_CO 0
#define W_CS 0
#define W_DM 3
#define W_DO 4
#define W_DS 3
#elif 0
#define W_AM 55
#define W_AO 16
#define W_AS 5
#define W_BM 3
#define W_BO 32
#define W_BS 6
#define W_CM 127
#define W_CO 64
#define W_CS 7
#define W_DM 7
#define W_DO 8
#define W_DS 4
#elif 0
#define W_AM 97
#define W_AO 32
#define W_AS 6
#define W_BM 63
#define W_BO 512
#define W_BS 10
#define W_CM 13
#define W_CO 8
#define W_CS 4
#define W_DM 15
#define W_DO 16
#define W_DS 5
#else
#define W_AM 203
#define W_AO 64
#define W_AS 7
#define W_BM 217
#define W_BO 2048
#define W_BS 12
#define W_CM 113
#define W_CO 64
#define W_CS 7
#define W_DM 227
#define W_DO 128
#define W_DS 9
#endif
#define slice_buffer_get_line(slice_buf, line_num) ((slice_buf)->line[line_num] ? (slice_buf)->line[line_num] : ff_slice_buffer_load_line((slice_buf), (line_num)))
//#define slice_buffer_get_line(slice_buf, line_num) (ff_slice_buffer_load_line((slice_buf), (line_num)))
void ff_slice_buffer_init(slice_buffer * buf, int line_count, int max_allocated_lines, int line_width, IDWTELEM * base_buffer);
void ff_slice_buffer_release(slice_buffer * buf, int line);
void ff_slice_buffer_flush(slice_buffer * buf);
void ff_slice_buffer_destroy(slice_buffer * buf);
IDWTELEM * ff_slice_buffer_load_line(slice_buffer * buf, int line);
void ff_snow_vertical_compose97i(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, IDWTELEM *b3, IDWTELEM *b4, IDWTELEM *b5, int width);
void ff_snow_horizontal_compose97i(IDWTELEM *b, int width);
void ff_snow_inner_add_yblock(const uint8_t *obmc, const int obmc_stride, uint8_t * * block, int b_w, int b_h, int src_x, int src_y, int src_stride, slice_buffer * sb, int add, uint8_t * dst8);
int ff_w53_32_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h);
int ff_w97_32_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h);
void ff_spatial_dwt(int *buffer, int width, int height, int stride, int type, int decomposition_count);
void ff_spatial_idwt_buffered_init(DWTCompose *cs, slice_buffer * sb, int width, int height, int stride_line, int type, int decomposition_count);
void ff_spatial_idwt_buffered_slice(DWTContext *dsp, DWTCompose *cs, slice_buffer * slice_buf, int width, int height, int stride_line, int type, int decomposition_count, int y);
void ff_spatial_idwt_init(DWTCompose *cs, IDWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count);
void ff_spatial_idwt_slice(DWTCompose *cs, IDWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count, int y);
void ff_spatial_idwt(IDWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count);
void ff_dwt_init(DWTContext *c);
void ff_dwt_init_x86(DWTContext *c);
#endif /* AVCODEC_DWT_H */
| 123linslouis-android-video-cutter | jni/libavcodec/dwt.h | C | asf20 | 4,882 |
/*
* H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
* Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* H.264 / AVC / MPEG4 part10 codec.
* @author Michael Niedermayer <michaelni@gmx.at>
*/
#include "internal.h"
#include "dsputil.h"
#include "avcodec.h"
#include "mpegvideo.h"
#include "h264.h"
#include "h264data.h"
#include "h264_mvpred.h"
#include "h264_parser.h"
#include "golomb.h"
#include "mathops.h"
#include "rectangle.h"
#include "vdpau_internal.h"
#include "cabac.h"
//#undef NDEBUG
#include <assert.h>
static const uint8_t rem6[52]={
0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3,
};
static const uint8_t div6[52]={
0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8,
};
void ff_h264_write_back_intra_pred_mode(H264Context *h){
int8_t *mode= h->intra4x4_pred_mode + h->mb2br_xy[h->mb_xy];
AV_COPY32(mode, h->intra4x4_pred_mode_cache + 4 + 8*4);
mode[4]= h->intra4x4_pred_mode_cache[7+8*3];
mode[5]= h->intra4x4_pred_mode_cache[7+8*2];
mode[6]= h->intra4x4_pred_mode_cache[7+8*1];
}
/**
* checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
*/
int ff_h264_check_intra4x4_pred_mode(H264Context *h){
MpegEncContext * const s = &h->s;
static const int8_t top [12]= {-1, 0,LEFT_DC_PRED,-1,-1,-1,-1,-1, 0};
static const int8_t left[12]= { 0,-1, TOP_DC_PRED, 0,-1,-1,-1, 0,-1,DC_128_PRED};
int i;
if(!(h->top_samples_available&0x8000)){
for(i=0; i<4; i++){
int status= top[ h->intra4x4_pred_mode_cache[scan8[0] + i] ];
if(status<0){
av_log(h->s.avctx, AV_LOG_ERROR, "top block unavailable for requested intra4x4 mode %d at %d %d\n", status, s->mb_x, s->mb_y);
return -1;
} else if(status){
h->intra4x4_pred_mode_cache[scan8[0] + i]= status;
}
}
}
if((h->left_samples_available&0x8888)!=0x8888){
static const int mask[4]={0x8000,0x2000,0x80,0x20};
for(i=0; i<4; i++){
if(!(h->left_samples_available&mask[i])){
int status= left[ h->intra4x4_pred_mode_cache[scan8[0] + 8*i] ];
if(status<0){
av_log(h->s.avctx, AV_LOG_ERROR, "left block unavailable for requested intra4x4 mode %d at %d %d\n", status, s->mb_x, s->mb_y);
return -1;
} else if(status){
h->intra4x4_pred_mode_cache[scan8[0] + 8*i]= status;
}
}
}
}
return 0;
} //FIXME cleanup like ff_h264_check_intra_pred_mode
/**
* checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
*/
int ff_h264_check_intra_pred_mode(H264Context *h, int mode){
MpegEncContext * const s = &h->s;
static const int8_t top [7]= {LEFT_DC_PRED8x8, 1,-1,-1};
static const int8_t left[7]= { TOP_DC_PRED8x8,-1, 2,-1,DC_128_PRED8x8};
if(mode > 6U) {
av_log(h->s.avctx, AV_LOG_ERROR, "out of range intra chroma pred mode at %d %d\n", s->mb_x, s->mb_y);
return -1;
}
if(!(h->top_samples_available&0x8000)){
mode= top[ mode ];
if(mode<0){
av_log(h->s.avctx, AV_LOG_ERROR, "top block unavailable for requested intra mode at %d %d\n", s->mb_x, s->mb_y);
return -1;
}
}
if((h->left_samples_available&0x8080) != 0x8080){
mode= left[ mode ];
if(h->left_samples_available&0x8080){ //mad cow disease mode, aka MBAFF + constrained_intra_pred
mode= ALZHEIMER_DC_L0T_PRED8x8 + (!(h->left_samples_available&0x8000)) + 2*(mode == DC_128_PRED8x8);
}
if(mode<0){
av_log(h->s.avctx, AV_LOG_ERROR, "left block unavailable for requested intra mode at %d %d\n", s->mb_x, s->mb_y);
return -1;
}
}
return mode;
}
const uint8_t *ff_h264_decode_nal(H264Context *h, const uint8_t *src, int *dst_length, int *consumed, int length){
int i, si, di;
uint8_t *dst;
int bufidx;
// src[0]&0x80; //forbidden bit
h->nal_ref_idc= src[0]>>5;
h->nal_unit_type= src[0]&0x1F;
src++; length--;
#if 0
for(i=0; i<length; i++)
printf("%2X ", src[i]);
#endif
#if HAVE_FAST_UNALIGNED
# if HAVE_FAST_64BIT
# define RS 7
for(i=0; i+1<length; i+=9){
if(!((~AV_RN64A(src+i) & (AV_RN64A(src+i) - 0x0100010001000101ULL)) & 0x8000800080008080ULL))
# else
# define RS 3
for(i=0; i+1<length; i+=5){
if(!((~AV_RN32A(src+i) & (AV_RN32A(src+i) - 0x01000101U)) & 0x80008080U))
# endif
continue;
if(i>0 && !src[i]) i--;
while(src[i]) i++;
#else
# define RS 0
for(i=0; i+1<length; i+=2){
if(src[i]) continue;
if(i>0 && src[i-1]==0) i--;
#endif
if(i+2<length && src[i+1]==0 && src[i+2]<=3){
if(src[i+2]!=3){
/* startcode, so we must be past the end */
length=i;
}
break;
}
i-= RS;
}
if(i>=length-1){ //no escaped 0
*dst_length= length;
*consumed= length+1; //+1 for the header
return src;
}
bufidx = h->nal_unit_type == NAL_DPC ? 1 : 0; // use second escape buffer for inter data
av_fast_malloc(&h->rbsp_buffer[bufidx], &h->rbsp_buffer_size[bufidx], length+FF_INPUT_BUFFER_PADDING_SIZE);
dst= h->rbsp_buffer[bufidx];
if (dst == NULL){
return NULL;
}
//printf("decoding esc\n");
memcpy(dst, src, i);
si=di=i;
while(si+2<length){
//remove escapes (very rare 1:2^22)
if(src[si+2]>3){
dst[di++]= src[si++];
dst[di++]= src[si++];
}else if(src[si]==0 && src[si+1]==0){
if(src[si+2]==3){ //escape
dst[di++]= 0;
dst[di++]= 0;
si+=3;
continue;
}else //next start code
goto nsc;
}
dst[di++]= src[si++];
}
while(si<length)
dst[di++]= src[si++];
nsc:
memset(dst+di, 0, FF_INPUT_BUFFER_PADDING_SIZE);
*dst_length= di;
*consumed= si + 1;//+1 for the header
//FIXME store exact number of bits in the getbitcontext (it is needed for decoding)
return dst;
}
int ff_h264_decode_rbsp_trailing(H264Context *h, const uint8_t *src){
int v= *src;
int r;
tprintf(h->s.avctx, "rbsp trailing %X\n", v);
for(r=1; r<9; r++){
if(v&1) return r;
v>>=1;
}
return 0;
}
/**
* IDCT transforms the 16 dc values and dequantizes them.
* @param qp quantization parameter
*/
static void h264_luma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
#define stride 16
int i;
int temp[16]; //FIXME check if this is a good idea
static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
//memset(block, 64, 2*256);
//return;
for(i=0; i<4; i++){
const int offset= y_offset[i];
const int z0= block[offset+stride*0] + block[offset+stride*4];
const int z1= block[offset+stride*0] - block[offset+stride*4];
const int z2= block[offset+stride*1] - block[offset+stride*5];
const int z3= block[offset+stride*1] + block[offset+stride*5];
temp[4*i+0]= z0+z3;
temp[4*i+1]= z1+z2;
temp[4*i+2]= z1-z2;
temp[4*i+3]= z0-z3;
}
for(i=0; i<4; i++){
const int offset= x_offset[i];
const int z0= temp[4*0+i] + temp[4*2+i];
const int z1= temp[4*0+i] - temp[4*2+i];
const int z2= temp[4*1+i] - temp[4*3+i];
const int z3= temp[4*1+i] + temp[4*3+i];
block[stride*0 +offset]= ((((z0 + z3)*qmul + 128 ) >> 8)); //FIXME think about merging this into decode_residual
block[stride*2 +offset]= ((((z1 + z2)*qmul + 128 ) >> 8));
block[stride*8 +offset]= ((((z1 - z2)*qmul + 128 ) >> 8));
block[stride*10+offset]= ((((z0 - z3)*qmul + 128 ) >> 8));
}
}
#if 0
/**
* DCT transforms the 16 dc values.
* @param qp quantization parameter ??? FIXME
*/
static void h264_luma_dc_dct_c(DCTELEM *block/*, int qp*/){
// const int qmul= dequant_coeff[qp][0];
int i;
int temp[16]; //FIXME check if this is a good idea
static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
for(i=0; i<4; i++){
const int offset= y_offset[i];
const int z0= block[offset+stride*0] + block[offset+stride*4];
const int z1= block[offset+stride*0] - block[offset+stride*4];
const int z2= block[offset+stride*1] - block[offset+stride*5];
const int z3= block[offset+stride*1] + block[offset+stride*5];
temp[4*i+0]= z0+z3;
temp[4*i+1]= z1+z2;
temp[4*i+2]= z1-z2;
temp[4*i+3]= z0-z3;
}
for(i=0; i<4; i++){
const int offset= x_offset[i];
const int z0= temp[4*0+i] + temp[4*2+i];
const int z1= temp[4*0+i] - temp[4*2+i];
const int z2= temp[4*1+i] - temp[4*3+i];
const int z3= temp[4*1+i] + temp[4*3+i];
block[stride*0 +offset]= (z0 + z3)>>1;
block[stride*2 +offset]= (z1 + z2)>>1;
block[stride*8 +offset]= (z1 - z2)>>1;
block[stride*10+offset]= (z0 - z3)>>1;
}
}
#endif
#undef xStride
#undef stride
static void chroma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
const int stride= 16*2;
const int xStride= 16;
int a,b,c,d,e;
a= block[stride*0 + xStride*0];
b= block[stride*0 + xStride*1];
c= block[stride*1 + xStride*0];
d= block[stride*1 + xStride*1];
e= a-b;
a= a+b;
b= c-d;
c= c+d;
block[stride*0 + xStride*0]= ((a+c)*qmul) >> 7;
block[stride*0 + xStride*1]= ((e+b)*qmul) >> 7;
block[stride*1 + xStride*0]= ((a-c)*qmul) >> 7;
block[stride*1 + xStride*1]= ((e-b)*qmul) >> 7;
}
#if 0
static void chroma_dc_dct_c(DCTELEM *block){
const int stride= 16*2;
const int xStride= 16;
int a,b,c,d,e;
a= block[stride*0 + xStride*0];
b= block[stride*0 + xStride*1];
c= block[stride*1 + xStride*0];
d= block[stride*1 + xStride*1];
e= a-b;
a= a+b;
b= c-d;
c= c+d;
block[stride*0 + xStride*0]= (a+c);
block[stride*0 + xStride*1]= (e+b);
block[stride*1 + xStride*0]= (a-c);
block[stride*1 + xStride*1]= (e-b);
}
#endif
static inline void mc_dir_part(H264Context *h, Picture *pic, int n, int square, int chroma_height, int delta, int list,
uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
int src_x_offset, int src_y_offset,
qpel_mc_func *qpix_op, h264_chroma_mc_func chroma_op){
MpegEncContext * const s = &h->s;
const int mx= h->mv_cache[list][ scan8[n] ][0] + src_x_offset*8;
int my= h->mv_cache[list][ scan8[n] ][1] + src_y_offset*8;
const int luma_xy= (mx&3) + ((my&3)<<2);
uint8_t * src_y = pic->data[0] + (mx>>2) + (my>>2)*h->mb_linesize;
uint8_t * src_cb, * src_cr;
int extra_width= h->emu_edge_width;
int extra_height= h->emu_edge_height;
int emu=0;
const int full_mx= mx>>2;
const int full_my= my>>2;
const int pic_width = 16*s->mb_width;
const int pic_height = 16*s->mb_height >> MB_FIELD;
if(mx&7) extra_width -= 3;
if(my&7) extra_height -= 3;
if( full_mx < 0-extra_width
|| full_my < 0-extra_height
|| full_mx + 16/*FIXME*/ > pic_width + extra_width
|| full_my + 16/*FIXME*/ > pic_height + extra_height){
ff_emulated_edge_mc(s->edge_emu_buffer, src_y - 2 - 2*h->mb_linesize, h->mb_linesize, 16+5, 16+5/*FIXME*/, full_mx-2, full_my-2, pic_width, pic_height);
src_y= s->edge_emu_buffer + 2 + 2*h->mb_linesize;
emu=1;
}
qpix_op[luma_xy](dest_y, src_y, h->mb_linesize); //FIXME try variable height perhaps?
if(!square){
qpix_op[luma_xy](dest_y + delta, src_y + delta, h->mb_linesize);
}
if(CONFIG_GRAY && s->flags&CODEC_FLAG_GRAY) return;
if(MB_FIELD){
// chroma offset when predicting from a field of opposite parity
my += 2 * ((s->mb_y & 1) - (pic->reference - 1));
emu |= (my>>3) < 0 || (my>>3) + 8 >= (pic_height>>1);
}
src_cb= pic->data[1] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
src_cr= pic->data[2] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
if(emu){
ff_emulated_edge_mc(s->edge_emu_buffer, src_cb, h->mb_uvlinesize, 9, 9/*FIXME*/, (mx>>3), (my>>3), pic_width>>1, pic_height>>1);
src_cb= s->edge_emu_buffer;
}
chroma_op(dest_cb, src_cb, h->mb_uvlinesize, chroma_height, mx&7, my&7);
if(emu){
ff_emulated_edge_mc(s->edge_emu_buffer, src_cr, h->mb_uvlinesize, 9, 9/*FIXME*/, (mx>>3), (my>>3), pic_width>>1, pic_height>>1);
src_cr= s->edge_emu_buffer;
}
chroma_op(dest_cr, src_cr, h->mb_uvlinesize, chroma_height, mx&7, my&7);
}
static inline void mc_part_std(H264Context *h, int n, int square, int chroma_height, int delta,
uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
int x_offset, int y_offset,
qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
int list0, int list1){
MpegEncContext * const s = &h->s;
qpel_mc_func *qpix_op= qpix_put;
h264_chroma_mc_func chroma_op= chroma_put;
dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
dest_cb += x_offset + y_offset*h->mb_uvlinesize;
dest_cr += x_offset + y_offset*h->mb_uvlinesize;
x_offset += 8*s->mb_x;
y_offset += 8*(s->mb_y >> MB_FIELD);
if(list0){
Picture *ref= &h->ref_list[0][ h->ref_cache[0][ scan8[n] ] ];
mc_dir_part(h, ref, n, square, chroma_height, delta, 0,
dest_y, dest_cb, dest_cr, x_offset, y_offset,
qpix_op, chroma_op);
qpix_op= qpix_avg;
chroma_op= chroma_avg;
}
if(list1){
Picture *ref= &h->ref_list[1][ h->ref_cache[1][ scan8[n] ] ];
mc_dir_part(h, ref, n, square, chroma_height, delta, 1,
dest_y, dest_cb, dest_cr, x_offset, y_offset,
qpix_op, chroma_op);
}
}
static inline void mc_part_weighted(H264Context *h, int n, int square, int chroma_height, int delta,
uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
int x_offset, int y_offset,
qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
h264_weight_func luma_weight_op, h264_weight_func chroma_weight_op,
h264_biweight_func luma_weight_avg, h264_biweight_func chroma_weight_avg,
int list0, int list1){
MpegEncContext * const s = &h->s;
dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
dest_cb += x_offset + y_offset*h->mb_uvlinesize;
dest_cr += x_offset + y_offset*h->mb_uvlinesize;
x_offset += 8*s->mb_x;
y_offset += 8*(s->mb_y >> MB_FIELD);
if(list0 && list1){
/* don't optimize for luma-only case, since B-frames usually
* use implicit weights => chroma too. */
uint8_t *tmp_cb = s->obmc_scratchpad;
uint8_t *tmp_cr = s->obmc_scratchpad + 8;
uint8_t *tmp_y = s->obmc_scratchpad + 8*h->mb_uvlinesize;
int refn0 = h->ref_cache[0][ scan8[n] ];
int refn1 = h->ref_cache[1][ scan8[n] ];
mc_dir_part(h, &h->ref_list[0][refn0], n, square, chroma_height, delta, 0,
dest_y, dest_cb, dest_cr,
x_offset, y_offset, qpix_put, chroma_put);
mc_dir_part(h, &h->ref_list[1][refn1], n, square, chroma_height, delta, 1,
tmp_y, tmp_cb, tmp_cr,
x_offset, y_offset, qpix_put, chroma_put);
if(h->use_weight == 2){
int weight0 = h->implicit_weight[refn0][refn1][s->mb_y&1];
int weight1 = 64 - weight0;
luma_weight_avg( dest_y, tmp_y, h-> mb_linesize, 5, weight0, weight1, 0);
chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, 5, weight0, weight1, 0);
chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, 5, weight0, weight1, 0);
}else{
luma_weight_avg(dest_y, tmp_y, h->mb_linesize, h->luma_log2_weight_denom,
h->luma_weight[refn0][0][0] , h->luma_weight[refn1][1][0],
h->luma_weight[refn0][0][1] + h->luma_weight[refn1][1][1]);
chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
h->chroma_weight[refn0][0][0][0] , h->chroma_weight[refn1][1][0][0],
h->chroma_weight[refn0][0][0][1] + h->chroma_weight[refn1][1][0][1]);
chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
h->chroma_weight[refn0][0][1][0] , h->chroma_weight[refn1][1][1][0],
h->chroma_weight[refn0][0][1][1] + h->chroma_weight[refn1][1][1][1]);
}
}else{
int list = list1 ? 1 : 0;
int refn = h->ref_cache[list][ scan8[n] ];
Picture *ref= &h->ref_list[list][refn];
mc_dir_part(h, ref, n, square, chroma_height, delta, list,
dest_y, dest_cb, dest_cr, x_offset, y_offset,
qpix_put, chroma_put);
luma_weight_op(dest_y, h->mb_linesize, h->luma_log2_weight_denom,
h->luma_weight[refn][list][0], h->luma_weight[refn][list][1]);
if(h->use_weight_chroma){
chroma_weight_op(dest_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
h->chroma_weight[refn][list][0][0], h->chroma_weight[refn][list][0][1]);
chroma_weight_op(dest_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
h->chroma_weight[refn][list][1][0], h->chroma_weight[refn][list][1][1]);
}
}
}
static inline void mc_part(H264Context *h, int n, int square, int chroma_height, int delta,
uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
int x_offset, int y_offset,
qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
h264_weight_func *weight_op, h264_biweight_func *weight_avg,
int list0, int list1){
if((h->use_weight==2 && list0 && list1
&& (h->implicit_weight[ h->ref_cache[0][scan8[n]] ][ h->ref_cache[1][scan8[n]] ][h->s.mb_y&1] != 32))
|| h->use_weight==1)
mc_part_weighted(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
x_offset, y_offset, qpix_put, chroma_put,
weight_op[0], weight_op[3], weight_avg[0], weight_avg[3], list0, list1);
else
mc_part_std(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
x_offset, y_offset, qpix_put, chroma_put, qpix_avg, chroma_avg, list0, list1);
}
static inline void prefetch_motion(H264Context *h, int list){
/* fetch pixels for estimated mv 4 macroblocks ahead
* optimized for 64byte cache lines */
MpegEncContext * const s = &h->s;
const int refn = h->ref_cache[list][scan8[0]];
if(refn >= 0){
const int mx= (h->mv_cache[list][scan8[0]][0]>>2) + 16*s->mb_x + 8;
const int my= (h->mv_cache[list][scan8[0]][1]>>2) + 16*s->mb_y;
uint8_t **src= h->ref_list[list][refn].data;
int off= mx + (my + (s->mb_x&3)*4)*h->mb_linesize + 64;
s->dsp.prefetch(src[0]+off, s->linesize, 4);
off= (mx>>1) + ((my>>1) + (s->mb_x&7))*s->uvlinesize + 64;
s->dsp.prefetch(src[1]+off, src[2]-src[1], 2);
}
}
static void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),
qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg),
h264_weight_func *weight_op, h264_biweight_func *weight_avg){
MpegEncContext * const s = &h->s;
const int mb_xy= h->mb_xy;
const int mb_type= s->current_picture.mb_type[mb_xy];
assert(IS_INTER(mb_type));
prefetch_motion(h, 0);
if(IS_16X16(mb_type)){
mc_part(h, 0, 1, 8, 0, dest_y, dest_cb, dest_cr, 0, 0,
qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0],
weight_op, weight_avg,
IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
}else if(IS_16X8(mb_type)){
mc_part(h, 0, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 0,
qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
&weight_op[1], &weight_avg[1],
IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
mc_part(h, 8, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 4,
qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
&weight_op[1], &weight_avg[1],
IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
}else if(IS_8X16(mb_type)){
mc_part(h, 0, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 0, 0,
qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
&weight_op[2], &weight_avg[2],
IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
mc_part(h, 4, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 4, 0,
qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
&weight_op[2], &weight_avg[2],
IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
}else{
int i;
assert(IS_8X8(mb_type));
for(i=0; i<4; i++){
const int sub_mb_type= h->sub_mb_type[i];
const int n= 4*i;
int x_offset= (i&1)<<2;
int y_offset= (i&2)<<1;
if(IS_SUB_8X8(sub_mb_type)){
mc_part(h, n, 1, 4, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset,
qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
&weight_op[3], &weight_avg[3],
IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
}else if(IS_SUB_8X4(sub_mb_type)){
mc_part(h, n , 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset,
qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
&weight_op[4], &weight_avg[4],
IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
mc_part(h, n+2, 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset+2,
qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
&weight_op[4], &weight_avg[4],
IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
}else if(IS_SUB_4X8(sub_mb_type)){
mc_part(h, n , 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset,
qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
&weight_op[5], &weight_avg[5],
IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
mc_part(h, n+1, 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset+2, y_offset,
qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
&weight_op[5], &weight_avg[5],
IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
}else{
int j;
assert(IS_SUB_4X4(sub_mb_type));
for(j=0; j<4; j++){
int sub_x_offset= x_offset + 2*(j&1);
int sub_y_offset= y_offset + (j&2);
mc_part(h, n+j, 1, 2, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,
qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
&weight_op[6], &weight_avg[6],
IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
}
}
}
}
prefetch_motion(h, 1);
}
static void free_tables(H264Context *h){
int i;
H264Context *hx;
av_freep(&h->intra4x4_pred_mode);
av_freep(&h->chroma_pred_mode_table);
av_freep(&h->cbp_table);
av_freep(&h->mvd_table[0]);
av_freep(&h->mvd_table[1]);
av_freep(&h->direct_table);
av_freep(&h->non_zero_count);
av_freep(&h->slice_table_base);
h->slice_table= NULL;
av_freep(&h->list_counts);
av_freep(&h->mb2b_xy);
av_freep(&h->mb2br_xy);
for(i = 0; i < MAX_THREADS; i++) {
hx = h->thread_context[i];
if(!hx) continue;
av_freep(&hx->top_borders[1]);
av_freep(&hx->top_borders[0]);
av_freep(&hx->s.obmc_scratchpad);
av_freep(&hx->rbsp_buffer[1]);
av_freep(&hx->rbsp_buffer[0]);
hx->rbsp_buffer_size[0] = 0;
hx->rbsp_buffer_size[1] = 0;
if (i) av_freep(&h->thread_context[i]);
}
}
static void init_dequant8_coeff_table(H264Context *h){
int i,q,x;
const int transpose = (h->h264dsp.h264_idct8_add != ff_h264_idct8_add_c); //FIXME ugly
h->dequant8_coeff[0] = h->dequant8_buffer[0];
h->dequant8_coeff[1] = h->dequant8_buffer[1];
for(i=0; i<2; i++ ){
if(i && !memcmp(h->pps.scaling_matrix8[0], h->pps.scaling_matrix8[1], 64*sizeof(uint8_t))){
h->dequant8_coeff[1] = h->dequant8_buffer[0];
break;
}
for(q=0; q<52; q++){
int shift = div6[q];
int idx = rem6[q];
for(x=0; x<64; x++)
h->dequant8_coeff[i][q][transpose ? (x>>3)|((x&7)<<3) : x] =
((uint32_t)dequant8_coeff_init[idx][ dequant8_coeff_init_scan[((x>>1)&12) | (x&3)] ] *
h->pps.scaling_matrix8[i][x]) << shift;
}
}
}
static void init_dequant4_coeff_table(H264Context *h){
int i,j,q,x;
const int transpose = (h->h264dsp.h264_idct_add != ff_h264_idct_add_c); //FIXME ugly
for(i=0; i<6; i++ ){
h->dequant4_coeff[i] = h->dequant4_buffer[i];
for(j=0; j<i; j++){
if(!memcmp(h->pps.scaling_matrix4[j], h->pps.scaling_matrix4[i], 16*sizeof(uint8_t))){
h->dequant4_coeff[i] = h->dequant4_buffer[j];
break;
}
}
if(j<i)
continue;
for(q=0; q<52; q++){
int shift = div6[q] + 2;
int idx = rem6[q];
for(x=0; x<16; x++)
h->dequant4_coeff[i][q][transpose ? (x>>2)|((x<<2)&0xF) : x] =
((uint32_t)dequant4_coeff_init[idx][(x&1) + ((x>>2)&1)] *
h->pps.scaling_matrix4[i][x]) << shift;
}
}
}
static void init_dequant_tables(H264Context *h){
int i,x;
init_dequant4_coeff_table(h);
if(h->pps.transform_8x8_mode)
init_dequant8_coeff_table(h);
if(h->sps.transform_bypass){
for(i=0; i<6; i++)
for(x=0; x<16; x++)
h->dequant4_coeff[i][0][x] = 1<<6;
if(h->pps.transform_8x8_mode)
for(i=0; i<2; i++)
for(x=0; x<64; x++)
h->dequant8_coeff[i][0][x] = 1<<6;
}
}
int ff_h264_alloc_tables(H264Context *h){
MpegEncContext * const s = &h->s;
const int big_mb_num= s->mb_stride * (s->mb_height+1);
const int row_mb_num= 2*s->mb_stride*s->avctx->thread_count;
int x,y;
FF_ALLOCZ_OR_GOTO(h->s.avctx, h->intra4x4_pred_mode, row_mb_num * 8 * sizeof(uint8_t), fail)
FF_ALLOCZ_OR_GOTO(h->s.avctx, h->non_zero_count , big_mb_num * 32 * sizeof(uint8_t), fail)
FF_ALLOCZ_OR_GOTO(h->s.avctx, h->slice_table_base , (big_mb_num+s->mb_stride) * sizeof(*h->slice_table_base), fail)
FF_ALLOCZ_OR_GOTO(h->s.avctx, h->cbp_table, big_mb_num * sizeof(uint16_t), fail)
FF_ALLOCZ_OR_GOTO(h->s.avctx, h->chroma_pred_mode_table, big_mb_num * sizeof(uint8_t), fail)
FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mvd_table[0], 16*row_mb_num * sizeof(uint8_t), fail);
FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mvd_table[1], 16*row_mb_num * sizeof(uint8_t), fail);
FF_ALLOCZ_OR_GOTO(h->s.avctx, h->direct_table, 4*big_mb_num * sizeof(uint8_t) , fail);
FF_ALLOCZ_OR_GOTO(h->s.avctx, h->list_counts, big_mb_num * sizeof(uint8_t), fail)
memset(h->slice_table_base, -1, (big_mb_num+s->mb_stride) * sizeof(*h->slice_table_base));
h->slice_table= h->slice_table_base + s->mb_stride*2 + 1;
FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mb2b_xy , big_mb_num * sizeof(uint32_t), fail);
FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mb2br_xy , big_mb_num * sizeof(uint32_t), fail);
for(y=0; y<s->mb_height; y++){
for(x=0; x<s->mb_width; x++){
const int mb_xy= x + y*s->mb_stride;
const int b_xy = 4*x + 4*y*h->b_stride;
h->mb2b_xy [mb_xy]= b_xy;
h->mb2br_xy[mb_xy]= 8*(FMO ? mb_xy : (mb_xy % (2*s->mb_stride)));
}
}
s->obmc_scratchpad = NULL;
if(!h->dequant4_coeff[0])
init_dequant_tables(h);
return 0;
fail:
free_tables(h);
return -1;
}
/**
* Mimic alloc_tables(), but for every context thread.
*/
static void clone_tables(H264Context *dst, H264Context *src, int i){
MpegEncContext * const s = &src->s;
dst->intra4x4_pred_mode = src->intra4x4_pred_mode + i*8*2*s->mb_stride;
dst->non_zero_count = src->non_zero_count;
dst->slice_table = src->slice_table;
dst->cbp_table = src->cbp_table;
dst->mb2b_xy = src->mb2b_xy;
dst->mb2br_xy = src->mb2br_xy;
dst->chroma_pred_mode_table = src->chroma_pred_mode_table;
dst->mvd_table[0] = src->mvd_table[0] + i*8*2*s->mb_stride;
dst->mvd_table[1] = src->mvd_table[1] + i*8*2*s->mb_stride;
dst->direct_table = src->direct_table;
dst->list_counts = src->list_counts;
dst->s.obmc_scratchpad = NULL;
ff_h264_pred_init(&dst->hpc, src->s.codec_id);
}
/**
* Init context
* Allocate buffers which are not shared amongst multiple threads.
*/
static int context_init(H264Context *h){
FF_ALLOCZ_OR_GOTO(h->s.avctx, h->top_borders[0], h->s.mb_width * (16+8+8) * sizeof(uint8_t), fail)
FF_ALLOCZ_OR_GOTO(h->s.avctx, h->top_borders[1], h->s.mb_width * (16+8+8) * sizeof(uint8_t), fail)
h->ref_cache[0][scan8[5 ]+1] = h->ref_cache[0][scan8[7 ]+1] = h->ref_cache[0][scan8[13]+1] =
h->ref_cache[1][scan8[5 ]+1] = h->ref_cache[1][scan8[7 ]+1] = h->ref_cache[1][scan8[13]+1] = PART_NOT_AVAILABLE;
return 0;
fail:
return -1; // free_tables will clean up for us
}
static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size);
static av_cold void common_init(H264Context *h){
MpegEncContext * const s = &h->s;
s->width = s->avctx->width;
s->height = s->avctx->height;
s->codec_id= s->avctx->codec->id;
ff_h264dsp_init(&h->h264dsp);
ff_h264_pred_init(&h->hpc, s->codec_id);
h->dequant_coeff_pps= -1;
s->unrestricted_mv=1;
s->decode=1; //FIXME
dsputil_init(&s->dsp, s->avctx); // needed so that idct permutation is known early
memset(h->pps.scaling_matrix4, 16, 6*16*sizeof(uint8_t));
memset(h->pps.scaling_matrix8, 16, 2*64*sizeof(uint8_t));
}
av_cold int ff_h264_decode_init(AVCodecContext *avctx){
H264Context *h= avctx->priv_data;
MpegEncContext * const s = &h->s;
MPV_decode_defaults(s);
s->avctx = avctx;
common_init(h);
s->out_format = FMT_H264;
s->workaround_bugs= avctx->workaround_bugs;
// set defaults
// s->decode_mb= ff_h263_decode_mb;
s->quarter_sample = 1;
if(!avctx->has_b_frames)
s->low_delay= 1;
avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
ff_h264_decode_init_vlc();
h->thread_context[0] = h;
h->outputed_poc = INT_MIN;
h->prev_poc_msb= 1<<16;
h->x264_build = -1;
ff_h264_reset_sei(h);
if(avctx->codec_id == CODEC_ID_H264){
if(avctx->ticks_per_frame == 1){
s->avctx->time_base.den *=2;
}
avctx->ticks_per_frame = 2;
}
if(avctx->extradata_size > 0 && avctx->extradata && *(char *)avctx->extradata == 1){
int i, cnt, nalsize;
unsigned char *p = avctx->extradata;
h->is_avc = 1;
if(avctx->extradata_size < 7) {
av_log(avctx, AV_LOG_ERROR, "avcC too short\n");
return -1;
}
/* sps and pps in the avcC always have length coded with 2 bytes,
so put a fake nal_length_size = 2 while parsing them */
h->nal_length_size = 2;
// Decode sps from avcC
cnt = *(p+5) & 0x1f; // Number of sps
p += 6;
for (i = 0; i < cnt; i++) {
nalsize = AV_RB16(p) + 2;
if(decode_nal_units(h, p, nalsize) < 0) {
av_log(avctx, AV_LOG_ERROR, "Decoding sps %d from avcC failed\n", i);
return -1;
}
p += nalsize;
}
// Decode pps from avcC
cnt = *(p++); // Number of pps
for (i = 0; i < cnt; i++) {
nalsize = AV_RB16(p) + 2;
if(decode_nal_units(h, p, nalsize) != nalsize) {
av_log(avctx, AV_LOG_ERROR, "Decoding pps %d from avcC failed\n", i);
return -1;
}
p += nalsize;
}
// Now store right nal length size, that will be use to parse all other nals
h->nal_length_size = ((*(((char*)(avctx->extradata))+4))&0x03)+1;
} else {
h->is_avc = 0;
if(decode_nal_units(h, s->avctx->extradata, s->avctx->extradata_size) < 0)
return -1;
}
if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames < h->sps.num_reorder_frames){
s->avctx->has_b_frames = h->sps.num_reorder_frames;
s->low_delay = 0;
}
return 0;
}
int ff_h264_frame_start(H264Context *h){
MpegEncContext * const s = &h->s;
int i;
if(MPV_frame_start(s, s->avctx) < 0)
return -1;
ff_er_frame_start(s);
/*
* MPV_frame_start uses pict_type to derive key_frame.
* This is incorrect for H.264; IDR markings must be used.
* Zero here; IDR markings per slice in frame or fields are ORed in later.
* See decode_nal_units().
*/
s->current_picture_ptr->key_frame= 0;
s->current_picture_ptr->mmco_reset= 0;
assert(s->linesize && s->uvlinesize);
for(i=0; i<16; i++){
h->block_offset[i]= 4*((scan8[i] - scan8[0])&7) + 4*s->linesize*((scan8[i] - scan8[0])>>3);
h->block_offset[24+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->linesize*((scan8[i] - scan8[0])>>3);
}
for(i=0; i<4; i++){
h->block_offset[16+i]=
h->block_offset[20+i]= 4*((scan8[i] - scan8[0])&7) + 4*s->uvlinesize*((scan8[i] - scan8[0])>>3);
h->block_offset[24+16+i]=
h->block_offset[24+20+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->uvlinesize*((scan8[i] - scan8[0])>>3);
}
/* can't be in alloc_tables because linesize isn't known there.
* FIXME: redo bipred weight to not require extra buffer? */
for(i = 0; i < s->avctx->thread_count; i++)
if(!h->thread_context[i]->s.obmc_scratchpad)
h->thread_context[i]->s.obmc_scratchpad = av_malloc(16*2*s->linesize + 8*2*s->uvlinesize);
/* some macroblocks can be accessed before they're available in case of lost slices, mbaff or threading*/
memset(h->slice_table, -1, (s->mb_height*s->mb_stride-1) * sizeof(*h->slice_table));
// s->decode= (s->flags&CODEC_FLAG_PSNR) || !s->encoding || s->current_picture.reference /*|| h->contains_intra*/ || 1;
// We mark the current picture as non-reference after allocating it, so
// that if we break out due to an error it can be released automatically
// in the next MPV_frame_start().
// SVQ3 as well as most other codecs have only last/next/current and thus
// get released even with set reference, besides SVQ3 and others do not
// mark frames as reference later "naturally".
if(s->codec_id != CODEC_ID_SVQ3)
s->current_picture_ptr->reference= 0;
s->current_picture_ptr->field_poc[0]=
s->current_picture_ptr->field_poc[1]= INT_MAX;
assert(s->current_picture_ptr->long_ref==0);
return 0;
}
static inline void backup_mb_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize, int simple){
MpegEncContext * const s = &h->s;
uint8_t *top_border;
int top_idx = 1;
src_y -= linesize;
src_cb -= uvlinesize;
src_cr -= uvlinesize;
if(!simple && FRAME_MBAFF){
if(s->mb_y&1){
if(!MB_MBAFF){
top_border = h->top_borders[0][s->mb_x];
AV_COPY128(top_border, src_y + 15*linesize);
if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
AV_COPY64(top_border+16, src_cb+7*uvlinesize);
AV_COPY64(top_border+24, src_cr+7*uvlinesize);
}
}
}else if(MB_MBAFF){
top_idx = 0;
}else
return;
}
top_border = h->top_borders[top_idx][s->mb_x];
// There are two lines saved, the line above the the top macroblock of a pair,
// and the line above the bottom macroblock
AV_COPY128(top_border, src_y + 16*linesize);
if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
AV_COPY64(top_border+16, src_cb+8*uvlinesize);
AV_COPY64(top_border+24, src_cr+8*uvlinesize);
}
}
static inline void xchg_mb_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize, int xchg, int simple){
MpegEncContext * const s = &h->s;
int deblock_left;
int deblock_top;
int top_idx = 1;
uint8_t *top_border_m1;
uint8_t *top_border;
if(!simple && FRAME_MBAFF){
if(s->mb_y&1){
if(!MB_MBAFF)
return;
}else{
top_idx = MB_MBAFF ? 0 : 1;
}
}
if(h->deblocking_filter == 2) {
deblock_left = h->left_type[0];
deblock_top = h->top_type;
} else {
deblock_left = (s->mb_x > 0);
deblock_top = (s->mb_y > !!MB_FIELD);
}
src_y -= linesize + 1;
src_cb -= uvlinesize + 1;
src_cr -= uvlinesize + 1;
top_border_m1 = h->top_borders[top_idx][s->mb_x-1];
top_border = h->top_borders[top_idx][s->mb_x];
#define XCHG(a,b,xchg)\
if (xchg) AV_SWAP64(b,a);\
else AV_COPY64(b,a);
if(deblock_top){
if(deblock_left){
XCHG(top_border_m1+8, src_y -7, 1);
}
XCHG(top_border+0, src_y +1, xchg);
XCHG(top_border+8, src_y +9, 1);
if(s->mb_x+1 < s->mb_width){
XCHG(h->top_borders[top_idx][s->mb_x+1], src_y +17, 1);
}
}
if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
if(deblock_top){
if(deblock_left){
XCHG(top_border_m1+16, src_cb -7, 1);
XCHG(top_border_m1+24, src_cr -7, 1);
}
XCHG(top_border+16, src_cb+1, 1);
XCHG(top_border+24, src_cr+1, 1);
}
}
}
static av_always_inline void hl_decode_mb_internal(H264Context *h, int simple){
MpegEncContext * const s = &h->s;
const int mb_x= s->mb_x;
const int mb_y= s->mb_y;
const int mb_xy= h->mb_xy;
const int mb_type= s->current_picture.mb_type[mb_xy];
uint8_t *dest_y, *dest_cb, *dest_cr;
int linesize, uvlinesize /*dct_offset*/;
int i;
int *block_offset = &h->block_offset[0];
const int transform_bypass = !simple && (s->qscale == 0 && h->sps.transform_bypass);
/* is_h264 should always be true if SVQ3 is disabled. */
const int is_h264 = !CONFIG_SVQ3_DECODER || simple || s->codec_id == CODEC_ID_H264;
void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
void (*idct_dc_add)(uint8_t *dst, DCTELEM *block, int stride);
dest_y = s->current_picture.data[0] + (mb_x + mb_y * s->linesize ) * 16;
dest_cb = s->current_picture.data[1] + (mb_x + mb_y * s->uvlinesize) * 8;
dest_cr = s->current_picture.data[2] + (mb_x + mb_y * s->uvlinesize) * 8;
s->dsp.prefetch(dest_y + (s->mb_x&3)*4*s->linesize + 64, s->linesize, 4);
s->dsp.prefetch(dest_cb + (s->mb_x&7)*s->uvlinesize + 64, dest_cr - dest_cb, 2);
h->list_counts[mb_xy]= h->list_count;
if (!simple && MB_FIELD) {
linesize = h->mb_linesize = s->linesize * 2;
uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
block_offset = &h->block_offset[24];
if(mb_y&1){ //FIXME move out of this function?
dest_y -= s->linesize*15;
dest_cb-= s->uvlinesize*7;
dest_cr-= s->uvlinesize*7;
}
if(FRAME_MBAFF) {
int list;
for(list=0; list<h->list_count; list++){
if(!USES_LIST(mb_type, list))
continue;
if(IS_16X16(mb_type)){
int8_t *ref = &h->ref_cache[list][scan8[0]];
fill_rectangle(ref, 4, 4, 8, (16+*ref)^(s->mb_y&1), 1);
}else{
for(i=0; i<16; i+=4){
int ref = h->ref_cache[list][scan8[i]];
if(ref >= 0)
fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2, 8, (16+ref)^(s->mb_y&1), 1);
}
}
}
}
} else {
linesize = h->mb_linesize = s->linesize;
uvlinesize = h->mb_uvlinesize = s->uvlinesize;
// dct_offset = s->linesize * 16;
}
if (!simple && IS_INTRA_PCM(mb_type)) {
for (i=0; i<16; i++) {
memcpy(dest_y + i* linesize, h->mb + i*8, 16);
}
for (i=0; i<8; i++) {
memcpy(dest_cb+ i*uvlinesize, h->mb + 128 + i*4, 8);
memcpy(dest_cr+ i*uvlinesize, h->mb + 160 + i*4, 8);
}
} else {
if(IS_INTRA(mb_type)){
if(h->deblocking_filter)
xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 1, simple);
if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
h->hpc.pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize);
h->hpc.pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize);
}
if(IS_INTRA4x4(mb_type)){
if(simple || !s->encoding){
if(IS_8x8DCT(mb_type)){
if(transform_bypass){
idct_dc_add =
idct_add = s->dsp.add_pixels8;
}else{
idct_dc_add = h->h264dsp.h264_idct8_dc_add;
idct_add = h->h264dsp.h264_idct8_add;
}
for(i=0; i<16; i+=4){
uint8_t * const ptr= dest_y + block_offset[i];
const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
if(transform_bypass && h->sps.profile_idc==244 && dir<=1){
h->hpc.pred8x8l_add[dir](ptr, h->mb + i*16, linesize);
}else{
const int nnz = h->non_zero_count_cache[ scan8[i] ];
h->hpc.pred8x8l[ dir ](ptr, (h->topleft_samples_available<<i)&0x8000,
(h->topright_samples_available<<i)&0x4000, linesize);
if(nnz){
if(nnz == 1 && h->mb[i*16])
idct_dc_add(ptr, h->mb + i*16, linesize);
else
idct_add (ptr, h->mb + i*16, linesize);
}
}
}
}else{
if(transform_bypass){
idct_dc_add =
idct_add = s->dsp.add_pixels4;
}else{
idct_dc_add = h->h264dsp.h264_idct_dc_add;
idct_add = h->h264dsp.h264_idct_add;
}
for(i=0; i<16; i++){
uint8_t * const ptr= dest_y + block_offset[i];
const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
if(transform_bypass && h->sps.profile_idc==244 && dir<=1){
h->hpc.pred4x4_add[dir](ptr, h->mb + i*16, linesize);
}else{
uint8_t *topright;
int nnz, tr;
if(dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED){
const int topright_avail= (h->topright_samples_available<<i)&0x8000;
assert(mb_y || linesize <= block_offset[i]);
if(!topright_avail){
tr= ptr[3 - linesize]*0x01010101;
topright= (uint8_t*) &tr;
}else
topright= ptr + 4 - linesize;
}else
topright= NULL;
h->hpc.pred4x4[ dir ](ptr, topright, linesize);
nnz = h->non_zero_count_cache[ scan8[i] ];
if(nnz){
if(is_h264){
if(nnz == 1 && h->mb[i*16])
idct_dc_add(ptr, h->mb + i*16, linesize);
else
idct_add (ptr, h->mb + i*16, linesize);
}else
ff_svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, 0);
}
}
}
}
}
}else{
h->hpc.pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize);
if(is_h264){
if(!transform_bypass)
h264_luma_dc_dequant_idct_c(h->mb, s->qscale, h->dequant4_coeff[0][s->qscale][0]);
}else
ff_svq3_luma_dc_dequant_idct_c(h->mb, s->qscale);
}
if(h->deblocking_filter)
xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0, simple);
}else if(is_h264){
hl_motion(h, dest_y, dest_cb, dest_cr,
s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
h->h264dsp.weight_h264_pixels_tab, h->h264dsp.biweight_h264_pixels_tab);
}
if(!IS_INTRA4x4(mb_type)){
if(is_h264){
if(IS_INTRA16x16(mb_type)){
if(transform_bypass){
if(h->sps.profile_idc==244 && (h->intra16x16_pred_mode==VERT_PRED8x8 || h->intra16x16_pred_mode==HOR_PRED8x8)){
h->hpc.pred16x16_add[h->intra16x16_pred_mode](dest_y, block_offset, h->mb, linesize);
}else{
for(i=0; i<16; i++){
if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16])
s->dsp.add_pixels4(dest_y + block_offset[i], h->mb + i*16, linesize);
}
}
}else{
h->h264dsp.h264_idct_add16intra(dest_y, block_offset, h->mb, linesize, h->non_zero_count_cache);
}
}else if(h->cbp&15){
if(transform_bypass){
const int di = IS_8x8DCT(mb_type) ? 4 : 1;
idct_add= IS_8x8DCT(mb_type) ? s->dsp.add_pixels8 : s->dsp.add_pixels4;
for(i=0; i<16; i+=di){
if(h->non_zero_count_cache[ scan8[i] ]){
idct_add(dest_y + block_offset[i], h->mb + i*16, linesize);
}
}
}else{
if(IS_8x8DCT(mb_type)){
h->h264dsp.h264_idct8_add4(dest_y, block_offset, h->mb, linesize, h->non_zero_count_cache);
}else{
h->h264dsp.h264_idct_add16(dest_y, block_offset, h->mb, linesize, h->non_zero_count_cache);
}
}
}
}else{
for(i=0; i<16; i++){
if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below
uint8_t * const ptr= dest_y + block_offset[i];
ff_svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0);
}
}
}
}
if((simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)) && (h->cbp&0x30)){
uint8_t *dest[2] = {dest_cb, dest_cr};
if(transform_bypass){
if(IS_INTRA(mb_type) && h->sps.profile_idc==244 && (h->chroma_pred_mode==VERT_PRED8x8 || h->chroma_pred_mode==HOR_PRED8x8)){
h->hpc.pred8x8_add[h->chroma_pred_mode](dest[0], block_offset + 16, h->mb + 16*16, uvlinesize);
h->hpc.pred8x8_add[h->chroma_pred_mode](dest[1], block_offset + 20, h->mb + 20*16, uvlinesize);
}else{
idct_add = s->dsp.add_pixels4;
for(i=16; i<16+8; i++){
if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16])
idct_add (dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
}
}
}else{
chroma_dc_dequant_idct_c(h->mb + 16*16, h->chroma_qp[0], h->dequant4_coeff[IS_INTRA(mb_type) ? 1:4][h->chroma_qp[0]][0]);
chroma_dc_dequant_idct_c(h->mb + 16*16+4*16, h->chroma_qp[1], h->dequant4_coeff[IS_INTRA(mb_type) ? 2:5][h->chroma_qp[1]][0]);
if(is_h264){
idct_add = h->h264dsp.h264_idct_add;
idct_dc_add = h->h264dsp.h264_idct_dc_add;
for(i=16; i<16+8; i++){
if(h->non_zero_count_cache[ scan8[i] ])
idct_add (dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
else if(h->mb[i*16])
idct_dc_add(dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
}
}else{
for(i=16; i<16+8; i++){
if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
uint8_t * const ptr= dest[(i&4)>>2] + block_offset[i];
ff_svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, ff_h264_chroma_qp[s->qscale + 12] - 12, 2);
}
}
}
}
}
}
if(h->cbp || IS_INTRA(mb_type))
s->dsp.clear_blocks(h->mb);
}
/**
* Process a macroblock; this case avoids checks for expensive uncommon cases.
*/
static void hl_decode_mb_simple(H264Context *h){
hl_decode_mb_internal(h, 1);
}
/**
* Process a macroblock; this handles edge cases, such as interlacing.
*/
static void av_noinline hl_decode_mb_complex(H264Context *h){
hl_decode_mb_internal(h, 0);
}
void ff_h264_hl_decode_mb(H264Context *h){
MpegEncContext * const s = &h->s;
const int mb_xy= h->mb_xy;
const int mb_type= s->current_picture.mb_type[mb_xy];
int is_complex = CONFIG_SMALL || h->is_complex || IS_INTRA_PCM(mb_type) || s->qscale == 0;
if (is_complex)
hl_decode_mb_complex(h);
else hl_decode_mb_simple(h);
}
static int pred_weight_table(H264Context *h){
MpegEncContext * const s = &h->s;
int list, i;
int luma_def, chroma_def;
h->use_weight= 0;
h->use_weight_chroma= 0;
h->luma_log2_weight_denom= get_ue_golomb(&s->gb);
h->chroma_log2_weight_denom= get_ue_golomb(&s->gb);
luma_def = 1<<h->luma_log2_weight_denom;
chroma_def = 1<<h->chroma_log2_weight_denom;
for(list=0; list<2; list++){
h->luma_weight_flag[list] = 0;
h->chroma_weight_flag[list] = 0;
for(i=0; i<h->ref_count[list]; i++){
int luma_weight_flag, chroma_weight_flag;
luma_weight_flag= get_bits1(&s->gb);
if(luma_weight_flag){
h->luma_weight[i][list][0]= get_se_golomb(&s->gb);
h->luma_weight[i][list][1]= get_se_golomb(&s->gb);
if( h->luma_weight[i][list][0] != luma_def
|| h->luma_weight[i][list][1] != 0) {
h->use_weight= 1;
h->luma_weight_flag[list]= 1;
}
}else{
h->luma_weight[i][list][0]= luma_def;
h->luma_weight[i][list][1]= 0;
}
if(CHROMA){
chroma_weight_flag= get_bits1(&s->gb);
if(chroma_weight_flag){
int j;
for(j=0; j<2; j++){
h->chroma_weight[i][list][j][0]= get_se_golomb(&s->gb);
h->chroma_weight[i][list][j][1]= get_se_golomb(&s->gb);
if( h->chroma_weight[i][list][j][0] != chroma_def
|| h->chroma_weight[i][list][j][1] != 0) {
h->use_weight_chroma= 1;
h->chroma_weight_flag[list]= 1;
}
}
}else{
int j;
for(j=0; j<2; j++){
h->chroma_weight[i][list][j][0]= chroma_def;
h->chroma_weight[i][list][j][1]= 0;
}
}
}
}
if(h->slice_type_nos != FF_B_TYPE) break;
}
h->use_weight= h->use_weight || h->use_weight_chroma;
return 0;
}
/**
* Initialize implicit_weight table.
* @param field, 0/1 initialize the weight for interlaced MBAFF
* -1 initializes the rest
*/
static void implicit_weight_table(H264Context *h, int field){
MpegEncContext * const s = &h->s;
int ref0, ref1, i, cur_poc, ref_start, ref_count0, ref_count1;
for (i = 0; i < 2; i++) {
h->luma_weight_flag[i] = 0;
h->chroma_weight_flag[i] = 0;
}
if(field < 0){
cur_poc = s->current_picture_ptr->poc;
if( h->ref_count[0] == 1 && h->ref_count[1] == 1 && !FRAME_MBAFF
&& h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2*cur_poc){
h->use_weight= 0;
h->use_weight_chroma= 0;
return;
}
ref_start= 0;
ref_count0= h->ref_count[0];
ref_count1= h->ref_count[1];
}else{
cur_poc = s->current_picture_ptr->field_poc[field];
ref_start= 16;
ref_count0= 16+2*h->ref_count[0];
ref_count1= 16+2*h->ref_count[1];
}
h->use_weight= 2;
h->use_weight_chroma= 2;
h->luma_log2_weight_denom= 5;
h->chroma_log2_weight_denom= 5;
for(ref0=ref_start; ref0 < ref_count0; ref0++){
int poc0 = h->ref_list[0][ref0].poc;
for(ref1=ref_start; ref1 < ref_count1; ref1++){
int poc1 = h->ref_list[1][ref1].poc;
int td = av_clip(poc1 - poc0, -128, 127);
int w= 32;
if(td){
int tb = av_clip(cur_poc - poc0, -128, 127);
int tx = (16384 + (FFABS(td) >> 1)) / td;
int dist_scale_factor = (tb*tx + 32) >> 8;
if(dist_scale_factor >= -64 && dist_scale_factor <= 128)
w = 64 - dist_scale_factor;
}
if(field<0){
h->implicit_weight[ref0][ref1][0]=
h->implicit_weight[ref0][ref1][1]= w;
}else{
h->implicit_weight[ref0][ref1][field]=w;
}
}
}
}
/**
* instantaneous decoder refresh.
*/
static void idr(H264Context *h){
ff_h264_remove_all_refs(h);
h->prev_frame_num= 0;
h->prev_frame_num_offset= 0;
h->prev_poc_msb=
h->prev_poc_lsb= 0;
}
/* forget old pics after a seek */
static void flush_dpb(AVCodecContext *avctx){
H264Context *h= avctx->priv_data;
int i;
for(i=0; i<MAX_DELAYED_PIC_COUNT; i++) {
if(h->delayed_pic[i])
h->delayed_pic[i]->reference= 0;
h->delayed_pic[i]= NULL;
}
h->outputed_poc= INT_MIN;
h->prev_interlaced_frame = 1;
idr(h);
if(h->s.current_picture_ptr)
h->s.current_picture_ptr->reference= 0;
h->s.first_field= 0;
ff_h264_reset_sei(h);
ff_mpeg_flush(avctx);
}
static int init_poc(H264Context *h){
MpegEncContext * const s = &h->s;
const int max_frame_num= 1<<h->sps.log2_max_frame_num;
int field_poc[2];
Picture *cur = s->current_picture_ptr;
h->frame_num_offset= h->prev_frame_num_offset;
if(h->frame_num < h->prev_frame_num)
h->frame_num_offset += max_frame_num;
if(h->sps.poc_type==0){
const int max_poc_lsb= 1<<h->sps.log2_max_poc_lsb;
if (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb/2)
h->poc_msb = h->prev_poc_msb + max_poc_lsb;
else if(h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb/2)
h->poc_msb = h->prev_poc_msb - max_poc_lsb;
else
h->poc_msb = h->prev_poc_msb;
//printf("poc: %d %d\n", h->poc_msb, h->poc_lsb);
field_poc[0] =
field_poc[1] = h->poc_msb + h->poc_lsb;
if(s->picture_structure == PICT_FRAME)
field_poc[1] += h->delta_poc_bottom;
}else if(h->sps.poc_type==1){
int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
int i;
if(h->sps.poc_cycle_length != 0)
abs_frame_num = h->frame_num_offset + h->frame_num;
else
abs_frame_num = 0;
if(h->nal_ref_idc==0 && abs_frame_num > 0)
abs_frame_num--;
expected_delta_per_poc_cycle = 0;
for(i=0; i < h->sps.poc_cycle_length; i++)
expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[ i ]; //FIXME integrate during sps parse
if(abs_frame_num > 0){
int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
for(i = 0; i <= frame_num_in_poc_cycle; i++)
expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[ i ];
} else
expectedpoc = 0;
if(h->nal_ref_idc == 0)
expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
field_poc[0] = expectedpoc + h->delta_poc[0];
field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
if(s->picture_structure == PICT_FRAME)
field_poc[1] += h->delta_poc[1];
}else{
int poc= 2*(h->frame_num_offset + h->frame_num);
if(!h->nal_ref_idc)
poc--;
field_poc[0]= poc;
field_poc[1]= poc;
}
if(s->picture_structure != PICT_BOTTOM_FIELD)
s->current_picture_ptr->field_poc[0]= field_poc[0];
if(s->picture_structure != PICT_TOP_FIELD)
s->current_picture_ptr->field_poc[1]= field_poc[1];
cur->poc= FFMIN(cur->field_poc[0], cur->field_poc[1]);
return 0;
}
/**
* initialize scan tables
*/
static void init_scan_tables(H264Context *h){
int i;
if(h->h264dsp.h264_idct_add == ff_h264_idct_add_c){ //FIXME little ugly
memcpy(h->zigzag_scan, zigzag_scan, 16*sizeof(uint8_t));
memcpy(h-> field_scan, field_scan, 16*sizeof(uint8_t));
}else{
for(i=0; i<16; i++){
#define T(x) (x>>2) | ((x<<2) & 0xF)
h->zigzag_scan[i] = T(zigzag_scan[i]);
h-> field_scan[i] = T( field_scan[i]);
#undef T
}
}
if(h->h264dsp.h264_idct8_add == ff_h264_idct8_add_c){
memcpy(h->zigzag_scan8x8, ff_zigzag_direct, 64*sizeof(uint8_t));
memcpy(h->zigzag_scan8x8_cavlc, zigzag_scan8x8_cavlc, 64*sizeof(uint8_t));
memcpy(h->field_scan8x8, field_scan8x8, 64*sizeof(uint8_t));
memcpy(h->field_scan8x8_cavlc, field_scan8x8_cavlc, 64*sizeof(uint8_t));
}else{
for(i=0; i<64; i++){
#define T(x) (x>>3) | ((x&7)<<3)
h->zigzag_scan8x8[i] = T(ff_zigzag_direct[i]);
h->zigzag_scan8x8_cavlc[i] = T(zigzag_scan8x8_cavlc[i]);
h->field_scan8x8[i] = T(field_scan8x8[i]);
h->field_scan8x8_cavlc[i] = T(field_scan8x8_cavlc[i]);
#undef T
}
}
if(h->sps.transform_bypass){ //FIXME same ugly
h->zigzag_scan_q0 = zigzag_scan;
h->zigzag_scan8x8_q0 = ff_zigzag_direct;
h->zigzag_scan8x8_cavlc_q0 = zigzag_scan8x8_cavlc;
h->field_scan_q0 = field_scan;
h->field_scan8x8_q0 = field_scan8x8;
h->field_scan8x8_cavlc_q0 = field_scan8x8_cavlc;
}else{
h->zigzag_scan_q0 = h->zigzag_scan;
h->zigzag_scan8x8_q0 = h->zigzag_scan8x8;
h->zigzag_scan8x8_cavlc_q0 = h->zigzag_scan8x8_cavlc;
h->field_scan_q0 = h->field_scan;
h->field_scan8x8_q0 = h->field_scan8x8;
h->field_scan8x8_cavlc_q0 = h->field_scan8x8_cavlc;
}
}
static void field_end(H264Context *h){
MpegEncContext * const s = &h->s;
AVCodecContext * const avctx= s->avctx;
s->mb_y= 0;
s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_H264;
s->current_picture_ptr->pict_type= s->pict_type;
if (CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
ff_vdpau_h264_set_reference_frames(s);
if(!s->dropable) {
ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
h->prev_poc_msb= h->poc_msb;
h->prev_poc_lsb= h->poc_lsb;
}
h->prev_frame_num_offset= h->frame_num_offset;
h->prev_frame_num= h->frame_num;
if (avctx->hwaccel) {
if (avctx->hwaccel->end_frame(avctx) < 0)
av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode picture\n");
}
if (CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
ff_vdpau_h264_picture_complete(s);
/*
* FIXME: Error handling code does not seem to support interlaced
* when slices span multiple rows
* The ff_er_add_slice calls don't work right for bottom
* fields; they cause massive erroneous error concealing
* Error marking covers both fields (top and bottom).
* This causes a mismatched s->error_count
* and a bad error table. Further, the error count goes to
* INT_MAX when called for bottom field, because mb_y is
* past end by one (callers fault) and resync_mb_y != 0
* causes problems for the first MB line, too.
*/
if (!FIELD_PICTURE)
ff_er_frame_end(s);
MPV_frame_end(s);
h->current_slice=0;
}
/**
* Replicates H264 "master" context to thread contexts.
*/
static void clone_slice(H264Context *dst, H264Context *src)
{
memcpy(dst->block_offset, src->block_offset, sizeof(dst->block_offset));
dst->s.current_picture_ptr = src->s.current_picture_ptr;
dst->s.current_picture = src->s.current_picture;
dst->s.linesize = src->s.linesize;
dst->s.uvlinesize = src->s.uvlinesize;
dst->s.first_field = src->s.first_field;
dst->prev_poc_msb = src->prev_poc_msb;
dst->prev_poc_lsb = src->prev_poc_lsb;
dst->prev_frame_num_offset = src->prev_frame_num_offset;
dst->prev_frame_num = src->prev_frame_num;
dst->short_ref_count = src->short_ref_count;
memcpy(dst->short_ref, src->short_ref, sizeof(dst->short_ref));
memcpy(dst->long_ref, src->long_ref, sizeof(dst->long_ref));
memcpy(dst->default_ref_list, src->default_ref_list, sizeof(dst->default_ref_list));
memcpy(dst->ref_list, src->ref_list, sizeof(dst->ref_list));
memcpy(dst->dequant4_coeff, src->dequant4_coeff, sizeof(src->dequant4_coeff));
memcpy(dst->dequant8_coeff, src->dequant8_coeff, sizeof(src->dequant8_coeff));
}
/**
* decodes a slice header.
* This will also call MPV_common_init() and frame_start() as needed.
*
* @param h h264context
* @param h0 h264 master context (differs from 'h' when doing sliced based parallel decoding)
*
* @return 0 if okay, <0 if an error occurred, 1 if decoding must not be multithreaded
*/
static int decode_slice_header(H264Context *h, H264Context *h0){
MpegEncContext * const s = &h->s;
MpegEncContext * const s0 = &h0->s;
unsigned int first_mb_in_slice;
unsigned int pps_id;
int num_ref_idx_active_override_flag;
unsigned int slice_type, tmp, i, j;
int default_ref_list_done = 0;
int last_pic_structure;
s->dropable= h->nal_ref_idc == 0;
if((s->avctx->flags2 & CODEC_FLAG2_FAST) && !h->nal_ref_idc){
s->me.qpel_put= s->dsp.put_2tap_qpel_pixels_tab;
s->me.qpel_avg= s->dsp.avg_2tap_qpel_pixels_tab;
}else{
s->me.qpel_put= s->dsp.put_h264_qpel_pixels_tab;
s->me.qpel_avg= s->dsp.avg_h264_qpel_pixels_tab;
}
first_mb_in_slice= get_ue_golomb(&s->gb);
if(first_mb_in_slice == 0){ //FIXME better field boundary detection
if(h0->current_slice && FIELD_PICTURE){
field_end(h);
}
h0->current_slice = 0;
if (!s0->first_field)
s->current_picture_ptr= NULL;
}
slice_type= get_ue_golomb_31(&s->gb);
if(slice_type > 9){
av_log(h->s.avctx, AV_LOG_ERROR, "slice type too large (%d) at %d %d\n", h->slice_type, s->mb_x, s->mb_y);
return -1;
}
if(slice_type > 4){
slice_type -= 5;
h->slice_type_fixed=1;
}else
h->slice_type_fixed=0;
slice_type= golomb_to_pict_type[ slice_type ];
if (slice_type == FF_I_TYPE
|| (h0->current_slice != 0 && slice_type == h0->last_slice_type) ) {
default_ref_list_done = 1;
}
h->slice_type= slice_type;
h->slice_type_nos= slice_type & 3;
s->pict_type= h->slice_type; // to make a few old functions happy, it's wrong though
pps_id= get_ue_golomb(&s->gb);
if(pps_id>=MAX_PPS_COUNT){
av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
return -1;
}
if(!h0->pps_buffers[pps_id]) {
av_log(h->s.avctx, AV_LOG_ERROR, "non-existing PPS %u referenced\n", pps_id);
return -1;
}
h->pps= *h0->pps_buffers[pps_id];
if(!h0->sps_buffers[h->pps.sps_id]) {
av_log(h->s.avctx, AV_LOG_ERROR, "non-existing SPS %u referenced\n", h->pps.sps_id);
return -1;
}
h->sps = *h0->sps_buffers[h->pps.sps_id];
s->avctx->profile = h->sps.profile_idc;
s->avctx->level = h->sps.level_idc;
s->avctx->refs = h->sps.ref_frame_count;
if(h == h0 && h->dequant_coeff_pps != pps_id){
h->dequant_coeff_pps = pps_id;
init_dequant_tables(h);
}
s->mb_width= h->sps.mb_width;
s->mb_height= h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
h->b_stride= s->mb_width*4;
s->width = 16*s->mb_width - 2*FFMIN(h->sps.crop_right, 7);
if(h->sps.frame_mbs_only_flag)
s->height= 16*s->mb_height - 2*FFMIN(h->sps.crop_bottom, 7);
else
s->height= 16*s->mb_height - 4*FFMIN(h->sps.crop_bottom, 3);
if (s->context_initialized
&& ( s->width != s->avctx->width || s->height != s->avctx->height
|| av_cmp_q(h->sps.sar, s->avctx->sample_aspect_ratio))) {
if(h != h0)
return -1; // width / height changed during parallelized decoding
free_tables(h);
flush_dpb(s->avctx);
MPV_common_end(s);
}
if (!s->context_initialized) {
if(h != h0)
return -1; // we cant (re-)initialize context during parallel decoding
avcodec_set_dimensions(s->avctx, s->width, s->height);
s->avctx->sample_aspect_ratio= h->sps.sar;
if(!s->avctx->sample_aspect_ratio.den)
s->avctx->sample_aspect_ratio.den = 1;
if(h->sps.video_signal_type_present_flag){
s->avctx->color_range = h->sps.full_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
if(h->sps.colour_description_present_flag){
s->avctx->color_primaries = h->sps.color_primaries;
s->avctx->color_trc = h->sps.color_trc;
s->avctx->colorspace = h->sps.colorspace;
}
}
if(h->sps.timing_info_present_flag){
int64_t den= h->sps.time_scale;
if(h->x264_build < 44U)
den *= 2;
av_reduce(&s->avctx->time_base.num, &s->avctx->time_base.den,
h->sps.num_units_in_tick, den, 1<<30);
}
s->avctx->pix_fmt = s->avctx->get_format(s->avctx, s->avctx->codec->pix_fmts);
s->avctx->hwaccel = ff_find_hwaccel(s->avctx->codec->id, s->avctx->pix_fmt);
if (MPV_common_init(s) < 0)
return -1;
s->first_field = 0;
h->prev_interlaced_frame = 1;
init_scan_tables(h);
ff_h264_alloc_tables(h);
for(i = 1; i < s->avctx->thread_count; i++) {
H264Context *c;
c = h->thread_context[i] = av_malloc(sizeof(H264Context));
memcpy(c, h->s.thread_context[i], sizeof(MpegEncContext));
memset(&c->s + 1, 0, sizeof(H264Context) - sizeof(MpegEncContext));
c->h264dsp = h->h264dsp;
c->sps = h->sps;
c->pps = h->pps;
init_scan_tables(c);
clone_tables(c, h, i);
}
for(i = 0; i < s->avctx->thread_count; i++)
if(context_init(h->thread_context[i]) < 0)
return -1;
}
h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num);
h->mb_mbaff = 0;
h->mb_aff_frame = 0;
last_pic_structure = s0->picture_structure;
if(h->sps.frame_mbs_only_flag){
s->picture_structure= PICT_FRAME;
}else{
if(get_bits1(&s->gb)) { //field_pic_flag
s->picture_structure= PICT_TOP_FIELD + get_bits1(&s->gb); //bottom_field_flag
} else {
s->picture_structure= PICT_FRAME;
h->mb_aff_frame = h->sps.mb_aff;
}
}
h->mb_field_decoding_flag= s->picture_structure != PICT_FRAME;
if(h0->current_slice == 0){
while(h->frame_num != h->prev_frame_num &&
h->frame_num != (h->prev_frame_num+1)%(1<<h->sps.log2_max_frame_num)){
av_log(NULL, AV_LOG_DEBUG, "Frame num gap %d %d\n", h->frame_num, h->prev_frame_num);
if (ff_h264_frame_start(h) < 0)
return -1;
h->prev_frame_num++;
h->prev_frame_num %= 1<<h->sps.log2_max_frame_num;
s->current_picture_ptr->frame_num= h->prev_frame_num;
ff_h264_execute_ref_pic_marking(h, NULL, 0);
}
/* See if we have a decoded first field looking for a pair... */
if (s0->first_field) {
assert(s0->current_picture_ptr);
assert(s0->current_picture_ptr->data[0]);
assert(s0->current_picture_ptr->reference != DELAYED_PIC_REF);
/* figure out if we have a complementary field pair */
if (!FIELD_PICTURE || s->picture_structure == last_pic_structure) {
/*
* Previous field is unmatched. Don't display it, but let it
* remain for reference if marked as such.
*/
s0->current_picture_ptr = NULL;
s0->first_field = FIELD_PICTURE;
} else {
if (h->nal_ref_idc &&
s0->current_picture_ptr->reference &&
s0->current_picture_ptr->frame_num != h->frame_num) {
/*
* This and previous field were reference, but had
* different frame_nums. Consider this field first in
* pair. Throw away previous field except for reference
* purposes.
*/
s0->first_field = 1;
s0->current_picture_ptr = NULL;
} else {
/* Second field in complementary pair */
s0->first_field = 0;
}
}
} else {
/* Frame or first field in a potentially complementary pair */
assert(!s0->current_picture_ptr);
s0->first_field = FIELD_PICTURE;
}
if((!FIELD_PICTURE || s0->first_field) && ff_h264_frame_start(h) < 0) {
s0->first_field = 0;
return -1;
}
}
if(h != h0)
clone_slice(h, h0);
s->current_picture_ptr->frame_num= h->frame_num; //FIXME frame_num cleanup
assert(s->mb_num == s->mb_width * s->mb_height);
if(first_mb_in_slice << FIELD_OR_MBAFF_PICTURE >= s->mb_num ||
first_mb_in_slice >= s->mb_num){
av_log(h->s.avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
return -1;
}
s->resync_mb_x = s->mb_x = first_mb_in_slice % s->mb_width;
s->resync_mb_y = s->mb_y = (first_mb_in_slice / s->mb_width) << FIELD_OR_MBAFF_PICTURE;
if (s->picture_structure == PICT_BOTTOM_FIELD)
s->resync_mb_y = s->mb_y = s->mb_y + 1;
assert(s->mb_y < s->mb_height);
if(s->picture_structure==PICT_FRAME){
h->curr_pic_num= h->frame_num;
h->max_pic_num= 1<< h->sps.log2_max_frame_num;
}else{
h->curr_pic_num= 2*h->frame_num + 1;
h->max_pic_num= 1<<(h->sps.log2_max_frame_num + 1);
}
if(h->nal_unit_type == NAL_IDR_SLICE){
get_ue_golomb(&s->gb); /* idr_pic_id */
}
if(h->sps.poc_type==0){
h->poc_lsb= get_bits(&s->gb, h->sps.log2_max_poc_lsb);
if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME){
h->delta_poc_bottom= get_se_golomb(&s->gb);
}
}
if(h->sps.poc_type==1 && !h->sps.delta_pic_order_always_zero_flag){
h->delta_poc[0]= get_se_golomb(&s->gb);
if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME)
h->delta_poc[1]= get_se_golomb(&s->gb);
}
init_poc(h);
if(h->pps.redundant_pic_cnt_present){
h->redundant_pic_count= get_ue_golomb(&s->gb);
}
//set defaults, might be overridden a few lines later
h->ref_count[0]= h->pps.ref_count[0];
h->ref_count[1]= h->pps.ref_count[1];
if(h->slice_type_nos != FF_I_TYPE){
if(h->slice_type_nos == FF_B_TYPE){
h->direct_spatial_mv_pred= get_bits1(&s->gb);
}
num_ref_idx_active_override_flag= get_bits1(&s->gb);
if(num_ref_idx_active_override_flag){
h->ref_count[0]= get_ue_golomb(&s->gb) + 1;
if(h->slice_type_nos==FF_B_TYPE)
h->ref_count[1]= get_ue_golomb(&s->gb) + 1;
if(h->ref_count[0]-1 > 32-1 || h->ref_count[1]-1 > 32-1){
av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow\n");
h->ref_count[0]= h->ref_count[1]= 1;
return -1;
}
}
if(h->slice_type_nos == FF_B_TYPE)
h->list_count= 2;
else
h->list_count= 1;
}else
h->list_count= 0;
if(!default_ref_list_done){
ff_h264_fill_default_ref_list(h);
}
if(h->slice_type_nos!=FF_I_TYPE && ff_h264_decode_ref_pic_list_reordering(h) < 0)
return -1;
if(h->slice_type_nos!=FF_I_TYPE){
s->last_picture_ptr= &h->ref_list[0][0];
ff_copy_picture(&s->last_picture, s->last_picture_ptr);
}
if(h->slice_type_nos==FF_B_TYPE){
s->next_picture_ptr= &h->ref_list[1][0];
ff_copy_picture(&s->next_picture, s->next_picture_ptr);
}
if( (h->pps.weighted_pred && h->slice_type_nos == FF_P_TYPE )
|| (h->pps.weighted_bipred_idc==1 && h->slice_type_nos== FF_B_TYPE ) )
pred_weight_table(h);
else if(h->pps.weighted_bipred_idc==2 && h->slice_type_nos== FF_B_TYPE){
implicit_weight_table(h, -1);
}else {
h->use_weight = 0;
for (i = 0; i < 2; i++) {
h->luma_weight_flag[i] = 0;
h->chroma_weight_flag[i] = 0;
}
}
if(h->nal_ref_idc)
ff_h264_decode_ref_pic_marking(h0, &s->gb);
if(FRAME_MBAFF){
ff_h264_fill_mbaff_ref_list(h);
if(h->pps.weighted_bipred_idc==2 && h->slice_type_nos== FF_B_TYPE){
implicit_weight_table(h, 0);
implicit_weight_table(h, 1);
}
}
if(h->slice_type_nos==FF_B_TYPE && !h->direct_spatial_mv_pred)
ff_h264_direct_dist_scale_factor(h);
ff_h264_direct_ref_list_init(h);
if( h->slice_type_nos != FF_I_TYPE && h->pps.cabac ){
tmp = get_ue_golomb_31(&s->gb);
if(tmp > 2){
av_log(s->avctx, AV_LOG_ERROR, "cabac_init_idc overflow\n");
return -1;
}
h->cabac_init_idc= tmp;
}
h->last_qscale_diff = 0;
tmp = h->pps.init_qp + get_se_golomb(&s->gb);
if(tmp>51){
av_log(s->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
return -1;
}
s->qscale= tmp;
h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
//FIXME qscale / qp ... stuff
if(h->slice_type == FF_SP_TYPE){
get_bits1(&s->gb); /* sp_for_switch_flag */
}
if(h->slice_type==FF_SP_TYPE || h->slice_type == FF_SI_TYPE){
get_se_golomb(&s->gb); /* slice_qs_delta */
}
h->deblocking_filter = 1;
h->slice_alpha_c0_offset = 52;
h->slice_beta_offset = 52;
if( h->pps.deblocking_filter_parameters_present ) {
tmp= get_ue_golomb_31(&s->gb);
if(tmp > 2){
av_log(s->avctx, AV_LOG_ERROR, "deblocking_filter_idc %u out of range\n", tmp);
return -1;
}
h->deblocking_filter= tmp;
if(h->deblocking_filter < 2)
h->deblocking_filter^= 1; // 1<->0
if( h->deblocking_filter ) {
h->slice_alpha_c0_offset += get_se_golomb(&s->gb) << 1;
h->slice_beta_offset += get_se_golomb(&s->gb) << 1;
if( h->slice_alpha_c0_offset > 104U
|| h->slice_beta_offset > 104U){
av_log(s->avctx, AV_LOG_ERROR, "deblocking filter parameters %d %d out of range\n", h->slice_alpha_c0_offset, h->slice_beta_offset);
return -1;
}
}
}
if( s->avctx->skip_loop_filter >= AVDISCARD_ALL
||(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY && h->slice_type_nos != FF_I_TYPE)
||(s->avctx->skip_loop_filter >= AVDISCARD_BIDIR && h->slice_type_nos == FF_B_TYPE)
||(s->avctx->skip_loop_filter >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
h->deblocking_filter= 0;
if(h->deblocking_filter == 1 && h0->max_contexts > 1) {
if(s->avctx->flags2 & CODEC_FLAG2_FAST) {
/* Cheat slightly for speed:
Do not bother to deblock across slices. */
h->deblocking_filter = 2;
} else {
h0->max_contexts = 1;
if(!h0->single_decode_warning) {
av_log(s->avctx, AV_LOG_INFO, "Cannot parallelize deblocking type 1, decoding such frames in sequential order\n");
h0->single_decode_warning = 1;
}
if(h != h0)
return 1; // deblocking switched inside frame
}
}
h->qp_thresh= 15 + 52 - FFMIN(h->slice_alpha_c0_offset, h->slice_beta_offset) - FFMAX3(0, h->pps.chroma_qp_index_offset[0], h->pps.chroma_qp_index_offset[1]);
#if 0 //FMO
if( h->pps.num_slice_groups > 1 && h->pps.mb_slice_group_map_type >= 3 && h->pps.mb_slice_group_map_type <= 5)
slice_group_change_cycle= get_bits(&s->gb, ?);
#endif
h0->last_slice_type = slice_type;
h->slice_num = ++h0->current_slice;
if(h->slice_num >= MAX_SLICES){
av_log(s->avctx, AV_LOG_ERROR, "Too many slices, increase MAX_SLICES and recompile\n");
}
for(j=0; j<2; j++){
int id_list[16];
int *ref2frm= h->ref2frm[h->slice_num&(MAX_SLICES-1)][j];
for(i=0; i<16; i++){
id_list[i]= 60;
if(h->ref_list[j][i].data[0]){
int k;
uint8_t *base= h->ref_list[j][i].base[0];
for(k=0; k<h->short_ref_count; k++)
if(h->short_ref[k]->base[0] == base){
id_list[i]= k;
break;
}
for(k=0; k<h->long_ref_count; k++)
if(h->long_ref[k] && h->long_ref[k]->base[0] == base){
id_list[i]= h->short_ref_count + k;
break;
}
}
}
ref2frm[0]=
ref2frm[1]= -1;
for(i=0; i<16; i++)
ref2frm[i+2]= 4*id_list[i]
+(h->ref_list[j][i].reference&3);
ref2frm[18+0]=
ref2frm[18+1]= -1;
for(i=16; i<48; i++)
ref2frm[i+4]= 4*id_list[(i-16)>>1]
+(h->ref_list[j][i].reference&3);
}
h->emu_edge_width= (s->flags&CODEC_FLAG_EMU_EDGE) ? 0 : 16;
h->emu_edge_height= (FRAME_MBAFF || FIELD_PICTURE) ? 0 : h->emu_edge_width;
if(s->avctx->debug&FF_DEBUG_PICT_INFO){
av_log(h->s.avctx, AV_LOG_DEBUG, "slice:%d %s mb:%d %c%s%s pps:%u frame:%d poc:%d/%d ref:%d/%d qp:%d loop:%d:%d:%d weight:%d%s %s\n",
h->slice_num,
(s->picture_structure==PICT_FRAME ? "F" : s->picture_structure==PICT_TOP_FIELD ? "T" : "B"),
first_mb_in_slice,
av_get_pict_type_char(h->slice_type), h->slice_type_fixed ? " fix" : "", h->nal_unit_type == NAL_IDR_SLICE ? " IDR" : "",
pps_id, h->frame_num,
s->current_picture_ptr->field_poc[0], s->current_picture_ptr->field_poc[1],
h->ref_count[0], h->ref_count[1],
s->qscale,
h->deblocking_filter, h->slice_alpha_c0_offset/2-26, h->slice_beta_offset/2-26,
h->use_weight,
h->use_weight==1 && h->use_weight_chroma ? "c" : "",
h->slice_type == FF_B_TYPE ? (h->direct_spatial_mv_pred ? "SPAT" : "TEMP") : ""
);
}
return 0;
}
int ff_h264_get_slice_type(const H264Context *h)
{
switch (h->slice_type) {
case FF_P_TYPE: return 0;
case FF_B_TYPE: return 1;
case FF_I_TYPE: return 2;
case FF_SP_TYPE: return 3;
case FF_SI_TYPE: return 4;
default: return -1;
}
}
/**
*
* @return non zero if the loop filter can be skiped
*/
static int fill_filter_caches(H264Context *h, int mb_type){
MpegEncContext * const s = &h->s;
const int mb_xy= h->mb_xy;
int top_xy, left_xy[2];
int top_type, left_type[2];
top_xy = mb_xy - (s->mb_stride << MB_FIELD);
//FIXME deblocking could skip the intra and nnz parts.
/* Wow, what a mess, why didn't they simplify the interlacing & intra
* stuff, I can't imagine that these complex rules are worth it. */
left_xy[1] = left_xy[0] = mb_xy-1;
if(FRAME_MBAFF){
const int left_mb_field_flag = IS_INTERLACED(s->current_picture.mb_type[mb_xy-1]);
const int curr_mb_field_flag = IS_INTERLACED(mb_type);
if(s->mb_y&1){
if (left_mb_field_flag != curr_mb_field_flag) {
left_xy[0] -= s->mb_stride;
}
}else{
if(curr_mb_field_flag){
top_xy += s->mb_stride & (((s->current_picture.mb_type[top_xy ]>>7)&1)-1);
}
if (left_mb_field_flag != curr_mb_field_flag) {
left_xy[1] += s->mb_stride;
}
}
}
h->top_mb_xy = top_xy;
h->left_mb_xy[0] = left_xy[0];
h->left_mb_xy[1] = left_xy[1];
{
//for sufficiently low qp, filtering wouldn't do anything
//this is a conservative estimate: could also check beta_offset and more accurate chroma_qp
int qp_thresh = h->qp_thresh; //FIXME strictly we should store qp_thresh for each mb of a slice
int qp = s->current_picture.qscale_table[mb_xy];
if(qp <= qp_thresh
&& (left_xy[0]<0 || ((qp + s->current_picture.qscale_table[left_xy[0]] + 1)>>1) <= qp_thresh)
&& (top_xy < 0 || ((qp + s->current_picture.qscale_table[top_xy ] + 1)>>1) <= qp_thresh)){
if(!FRAME_MBAFF)
return 1;
if( (left_xy[0]< 0 || ((qp + s->current_picture.qscale_table[left_xy[1] ] + 1)>>1) <= qp_thresh)
&& (top_xy < s->mb_stride || ((qp + s->current_picture.qscale_table[top_xy -s->mb_stride] + 1)>>1) <= qp_thresh))
return 1;
}
}
top_type = s->current_picture.mb_type[top_xy] ;
left_type[0] = s->current_picture.mb_type[left_xy[0]];
left_type[1] = s->current_picture.mb_type[left_xy[1]];
if(h->deblocking_filter == 2){
if(h->slice_table[top_xy ] != h->slice_num) top_type= 0;
if(h->slice_table[left_xy[0] ] != h->slice_num) left_type[0]= left_type[1]= 0;
}else{
if(h->slice_table[top_xy ] == 0xFFFF) top_type= 0;
if(h->slice_table[left_xy[0] ] == 0xFFFF) left_type[0]= left_type[1] =0;
}
h->top_type = top_type ;
h->left_type[0]= left_type[0];
h->left_type[1]= left_type[1];
if(IS_INTRA(mb_type))
return 0;
AV_COPY64(&h->non_zero_count_cache[0+8*1], &h->non_zero_count[mb_xy][ 0]);
AV_COPY64(&h->non_zero_count_cache[0+8*2], &h->non_zero_count[mb_xy][ 8]);
AV_COPY32(&h->non_zero_count_cache[0+8*5], &h->non_zero_count[mb_xy][16]);
AV_COPY32(&h->non_zero_count_cache[4+8*3], &h->non_zero_count[mb_xy][20]);
AV_COPY64(&h->non_zero_count_cache[0+8*4], &h->non_zero_count[mb_xy][24]);
h->cbp= h->cbp_table[mb_xy];
{
int list;
for(list=0; list<h->list_count; list++){
int8_t *ref;
int y, b_stride;
int16_t (*mv_dst)[2];
int16_t (*mv_src)[2];
if(!USES_LIST(mb_type, list)){
fill_rectangle( h->mv_cache[list][scan8[0]], 4, 4, 8, pack16to32(0,0), 4);
AV_WN32A(&h->ref_cache[list][scan8[ 0]], ((LIST_NOT_USED)&0xFF)*0x01010101u);
AV_WN32A(&h->ref_cache[list][scan8[ 2]], ((LIST_NOT_USED)&0xFF)*0x01010101u);
AV_WN32A(&h->ref_cache[list][scan8[ 8]], ((LIST_NOT_USED)&0xFF)*0x01010101u);
AV_WN32A(&h->ref_cache[list][scan8[10]], ((LIST_NOT_USED)&0xFF)*0x01010101u);
continue;
}
ref = &s->current_picture.ref_index[list][4*mb_xy];
{
int (*ref2frm)[64] = h->ref2frm[ h->slice_num&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
AV_WN32A(&h->ref_cache[list][scan8[ 0]], (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101);
AV_WN32A(&h->ref_cache[list][scan8[ 2]], (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101);
ref += 2;
AV_WN32A(&h->ref_cache[list][scan8[ 8]], (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101);
AV_WN32A(&h->ref_cache[list][scan8[10]], (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101);
}
b_stride = h->b_stride;
mv_dst = &h->mv_cache[list][scan8[0]];
mv_src = &s->current_picture.motion_val[list][4*s->mb_x + 4*s->mb_y*b_stride];
for(y=0; y<4; y++){
AV_COPY128(mv_dst + 8*y, mv_src + y*b_stride);
}
}
}
/*
0 . T T. T T T T
1 L . .L . . . .
2 L . .L . . . .
3 . T TL . . . .
4 L . .L . . . .
5 L . .. . . . .
*/
//FIXME constraint_intra_pred & partitioning & nnz (let us hope this is just a typo in the spec)
if(top_type){
AV_COPY32(&h->non_zero_count_cache[4+8*0], &h->non_zero_count[top_xy][4+3*8]);
}
if(left_type[0]){
h->non_zero_count_cache[3+8*1]= h->non_zero_count[left_xy[0]][7+0*8];
h->non_zero_count_cache[3+8*2]= h->non_zero_count[left_xy[0]][7+1*8];
h->non_zero_count_cache[3+8*3]= h->non_zero_count[left_xy[0]][7+2*8];
h->non_zero_count_cache[3+8*4]= h->non_zero_count[left_xy[0]][7+3*8];
}
// CAVLC 8x8dct requires NNZ values for residual decoding that differ from what the loop filter needs
if(!CABAC && h->pps.transform_8x8_mode){
if(IS_8x8DCT(top_type)){
h->non_zero_count_cache[4+8*0]=
h->non_zero_count_cache[5+8*0]= h->cbp_table[top_xy] & 4;
h->non_zero_count_cache[6+8*0]=
h->non_zero_count_cache[7+8*0]= h->cbp_table[top_xy] & 8;
}
if(IS_8x8DCT(left_type[0])){
h->non_zero_count_cache[3+8*1]=
h->non_zero_count_cache[3+8*2]= h->cbp_table[left_xy[0]]&2; //FIXME check MBAFF
}
if(IS_8x8DCT(left_type[1])){
h->non_zero_count_cache[3+8*3]=
h->non_zero_count_cache[3+8*4]= h->cbp_table[left_xy[1]]&8; //FIXME check MBAFF
}
if(IS_8x8DCT(mb_type)){
h->non_zero_count_cache[scan8[0 ]]= h->non_zero_count_cache[scan8[1 ]]=
h->non_zero_count_cache[scan8[2 ]]= h->non_zero_count_cache[scan8[3 ]]= h->cbp & 1;
h->non_zero_count_cache[scan8[0+ 4]]= h->non_zero_count_cache[scan8[1+ 4]]=
h->non_zero_count_cache[scan8[2+ 4]]= h->non_zero_count_cache[scan8[3+ 4]]= h->cbp & 2;
h->non_zero_count_cache[scan8[0+ 8]]= h->non_zero_count_cache[scan8[1+ 8]]=
h->non_zero_count_cache[scan8[2+ 8]]= h->non_zero_count_cache[scan8[3+ 8]]= h->cbp & 4;
h->non_zero_count_cache[scan8[0+12]]= h->non_zero_count_cache[scan8[1+12]]=
h->non_zero_count_cache[scan8[2+12]]= h->non_zero_count_cache[scan8[3+12]]= h->cbp & 8;
}
}
if(IS_INTER(mb_type) || IS_DIRECT(mb_type)){
int list;
for(list=0; list<h->list_count; list++){
if(USES_LIST(top_type, list)){
const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
const int b8_xy= 4*top_xy + 2;
int (*ref2frm)[64] = h->ref2frm[ h->slice_table[top_xy]&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
AV_COPY128(h->mv_cache[list][scan8[0] + 0 - 1*8], s->current_picture.motion_val[list][b_xy + 0]);
h->ref_cache[list][scan8[0] + 0 - 1*8]=
h->ref_cache[list][scan8[0] + 1 - 1*8]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 0]];
h->ref_cache[list][scan8[0] + 2 - 1*8]=
h->ref_cache[list][scan8[0] + 3 - 1*8]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 1]];
}else{
AV_ZERO128(h->mv_cache[list][scan8[0] + 0 - 1*8]);
AV_WN32A(&h->ref_cache[list][scan8[0] + 0 - 1*8], ((LIST_NOT_USED)&0xFF)*0x01010101u);
}
if(!IS_INTERLACED(mb_type^left_type[0])){
if(USES_LIST(left_type[0], list)){
const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
const int b8_xy= 4*left_xy[0] + 1;
int (*ref2frm)[64] = h->ref2frm[ h->slice_table[left_xy[0]]&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
AV_COPY32(h->mv_cache[list][scan8[0] - 1 + 0 ], s->current_picture.motion_val[list][b_xy + h->b_stride*0]);
AV_COPY32(h->mv_cache[list][scan8[0] - 1 + 8 ], s->current_picture.motion_val[list][b_xy + h->b_stride*1]);
AV_COPY32(h->mv_cache[list][scan8[0] - 1 +16 ], s->current_picture.motion_val[list][b_xy + h->b_stride*2]);
AV_COPY32(h->mv_cache[list][scan8[0] - 1 +24 ], s->current_picture.motion_val[list][b_xy + h->b_stride*3]);
h->ref_cache[list][scan8[0] - 1 + 0 ]=
h->ref_cache[list][scan8[0] - 1 + 8 ]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 2*0]];
h->ref_cache[list][scan8[0] - 1 +16 ]=
h->ref_cache[list][scan8[0] - 1 +24 ]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 2*1]];
}else{
AV_ZERO32(h->mv_cache [list][scan8[0] - 1 + 0 ]);
AV_ZERO32(h->mv_cache [list][scan8[0] - 1 + 8 ]);
AV_ZERO32(h->mv_cache [list][scan8[0] - 1 +16 ]);
AV_ZERO32(h->mv_cache [list][scan8[0] - 1 +24 ]);
h->ref_cache[list][scan8[0] - 1 + 0 ]=
h->ref_cache[list][scan8[0] - 1 + 8 ]=
h->ref_cache[list][scan8[0] - 1 + 16 ]=
h->ref_cache[list][scan8[0] - 1 + 24 ]= LIST_NOT_USED;
}
}
}
}
return 0;
}
static void loop_filter(H264Context *h){
MpegEncContext * const s = &h->s;
uint8_t *dest_y, *dest_cb, *dest_cr;
int linesize, uvlinesize, mb_x, mb_y;
const int end_mb_y= s->mb_y + FRAME_MBAFF;
const int old_slice_type= h->slice_type;
if(h->deblocking_filter) {
for(mb_x= 0; mb_x<s->mb_width; mb_x++){
for(mb_y=end_mb_y - FRAME_MBAFF; mb_y<= end_mb_y; mb_y++){
int mb_xy, mb_type;
mb_xy = h->mb_xy = mb_x + mb_y*s->mb_stride;
h->slice_num= h->slice_table[mb_xy];
mb_type= s->current_picture.mb_type[mb_xy];
h->list_count= h->list_counts[mb_xy];
if(FRAME_MBAFF)
h->mb_mbaff = h->mb_field_decoding_flag = !!IS_INTERLACED(mb_type);
s->mb_x= mb_x;
s->mb_y= mb_y;
dest_y = s->current_picture.data[0] + (mb_x + mb_y * s->linesize ) * 16;
dest_cb = s->current_picture.data[1] + (mb_x + mb_y * s->uvlinesize) * 8;
dest_cr = s->current_picture.data[2] + (mb_x + mb_y * s->uvlinesize) * 8;
//FIXME simplify above
if (MB_FIELD) {
linesize = h->mb_linesize = s->linesize * 2;
uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
if(mb_y&1){ //FIXME move out of this function?
dest_y -= s->linesize*15;
dest_cb-= s->uvlinesize*7;
dest_cr-= s->uvlinesize*7;
}
} else {
linesize = h->mb_linesize = s->linesize;
uvlinesize = h->mb_uvlinesize = s->uvlinesize;
}
backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0);
if(fill_filter_caches(h, mb_type))
continue;
h->chroma_qp[0] = get_chroma_qp(h, 0, s->current_picture.qscale_table[mb_xy]);
h->chroma_qp[1] = get_chroma_qp(h, 1, s->current_picture.qscale_table[mb_xy]);
if (FRAME_MBAFF) {
ff_h264_filter_mb (h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
} else {
ff_h264_filter_mb_fast(h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
}
}
}
}
h->slice_type= old_slice_type;
s->mb_x= 0;
s->mb_y= end_mb_y - FRAME_MBAFF;
h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
}
static void predict_field_decoding_flag(H264Context *h){
MpegEncContext * const s = &h->s;
const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
int mb_type = (h->slice_table[mb_xy-1] == h->slice_num)
? s->current_picture.mb_type[mb_xy-1]
: (h->slice_table[mb_xy-s->mb_stride] == h->slice_num)
? s->current_picture.mb_type[mb_xy-s->mb_stride]
: 0;
h->mb_mbaff = h->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
}
static int decode_slice(struct AVCodecContext *avctx, void *arg){
H264Context *h = *(void**)arg;
MpegEncContext * const s = &h->s;
const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
s->mb_skip_run= -1;
h->is_complex = FRAME_MBAFF || s->picture_structure != PICT_FRAME || s->codec_id != CODEC_ID_H264 ||
(CONFIG_GRAY && (s->flags&CODEC_FLAG_GRAY));
if( h->pps.cabac ) {
/* realign */
align_get_bits( &s->gb );
/* init cabac */
ff_init_cabac_states( &h->cabac);
ff_init_cabac_decoder( &h->cabac,
s->gb.buffer + get_bits_count(&s->gb)/8,
(get_bits_left(&s->gb) + 7)/8);
ff_h264_init_cabac_states(h);
for(;;){
//START_TIMER
int ret = ff_h264_decode_mb_cabac(h);
int eos;
//STOP_TIMER("decode_mb_cabac")
if(ret>=0) ff_h264_hl_decode_mb(h);
if( ret >= 0 && FRAME_MBAFF ) { //FIXME optimal? or let mb_decode decode 16x32 ?
s->mb_y++;
ret = ff_h264_decode_mb_cabac(h);
if(ret>=0) ff_h264_hl_decode_mb(h);
s->mb_y--;
}
eos = get_cabac_terminate( &h->cabac );
if((s->workaround_bugs & FF_BUG_TRUNCATED) && h->cabac.bytestream > h->cabac.bytestream_end + 2){
ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
return 0;
}
if( ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 2) {
av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d, bytestream (%td)\n", s->mb_x, s->mb_y, h->cabac.bytestream_end - h->cabac.bytestream);
ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
return -1;
}
if( ++s->mb_x >= s->mb_width ) {
s->mb_x = 0;
loop_filter(h);
ff_draw_horiz_band(s, 16*s->mb_y, 16);
++s->mb_y;
if(FIELD_OR_MBAFF_PICTURE) {
++s->mb_y;
if(FRAME_MBAFF && s->mb_y < s->mb_height)
predict_field_decoding_flag(h);
}
}
if( eos || s->mb_y >= s->mb_height ) {
tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
return 0;
}
}
} else {
for(;;){
int ret = ff_h264_decode_mb_cavlc(h);
if(ret>=0) ff_h264_hl_decode_mb(h);
if(ret>=0 && FRAME_MBAFF){ //FIXME optimal? or let mb_decode decode 16x32 ?
s->mb_y++;
ret = ff_h264_decode_mb_cavlc(h);
if(ret>=0) ff_h264_hl_decode_mb(h);
s->mb_y--;
}
if(ret<0){
av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
return -1;
}
if(++s->mb_x >= s->mb_width){
s->mb_x=0;
loop_filter(h);
ff_draw_horiz_band(s, 16*s->mb_y, 16);
++s->mb_y;
if(FIELD_OR_MBAFF_PICTURE) {
++s->mb_y;
if(FRAME_MBAFF && s->mb_y < s->mb_height)
predict_field_decoding_flag(h);
}
if(s->mb_y >= s->mb_height){
tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
if(get_bits_count(&s->gb) == s->gb.size_in_bits ) {
ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
return 0;
}else{
ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
return -1;
}
}
}
if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->mb_skip_run<=0){
tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
if(get_bits_count(&s->gb) == s->gb.size_in_bits ){
ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
return 0;
}else{
ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
return -1;
}
}
}
}
#if 0
for(;s->mb_y < s->mb_height; s->mb_y++){
for(;s->mb_x < s->mb_width; s->mb_x++){
int ret= decode_mb(h);
ff_h264_hl_decode_mb(h);
if(ret<0){
av_log(s->avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
return -1;
}
if(++s->mb_x >= s->mb_width){
s->mb_x=0;
if(++s->mb_y >= s->mb_height){
if(get_bits_count(s->gb) == s->gb.size_in_bits){
ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
return 0;
}else{
ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
return -1;
}
}
}
if(get_bits_count(s->?gb) >= s->gb?.size_in_bits){
if(get_bits_count(s->gb) == s->gb.size_in_bits){
ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
return 0;
}else{
ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
return -1;
}
}
}
s->mb_x=0;
ff_draw_horiz_band(s, 16*s->mb_y, 16);
}
#endif
return -1; //not reached
}
/**
* Call decode_slice() for each context.
*
* @param h h264 master context
* @param context_count number of contexts to execute
*/
static void execute_decode_slices(H264Context *h, int context_count){
MpegEncContext * const s = &h->s;
AVCodecContext * const avctx= s->avctx;
H264Context *hx;
int i;
if (s->avctx->hwaccel)
return;
if(s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
return;
if(context_count == 1) {
decode_slice(avctx, &h);
} else {
for(i = 1; i < context_count; i++) {
hx = h->thread_context[i];
hx->s.error_recognition = avctx->error_recognition;
hx->s.error_count = 0;
}
avctx->execute(avctx, (void *)decode_slice,
h->thread_context, NULL, context_count, sizeof(void*));
/* pull back stuff from slices to master context */
hx = h->thread_context[context_count - 1];
s->mb_x = hx->s.mb_x;
s->mb_y = hx->s.mb_y;
s->dropable = hx->s.dropable;
s->picture_structure = hx->s.picture_structure;
for(i = 1; i < context_count; i++)
h->s.error_count += h->thread_context[i]->s.error_count;
}
}
static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size){
MpegEncContext * const s = &h->s;
AVCodecContext * const avctx= s->avctx;
int buf_index=0;
H264Context *hx; ///< thread context
int context_count = 0;
int next_avc= h->is_avc ? 0 : buf_size;
h->max_contexts = avctx->thread_count;
#if 0
int i;
for(i=0; i<50; i++){
av_log(NULL, AV_LOG_ERROR,"%02X ", buf[i]);
}
#endif
if(!(s->flags2 & CODEC_FLAG2_CHUNKS)){
h->current_slice = 0;
if (!s->first_field)
s->current_picture_ptr= NULL;
ff_h264_reset_sei(h);
}
for(;;){
int consumed;
int dst_length;
int bit_length;
const uint8_t *ptr;
int i, nalsize = 0;
int err;
if(buf_index >= next_avc) {
if(buf_index >= buf_size) break;
nalsize = 0;
for(i = 0; i < h->nal_length_size; i++)
nalsize = (nalsize << 8) | buf[buf_index++];
if(nalsize <= 1 || nalsize > buf_size - buf_index){
if(nalsize == 1){
buf_index++;
continue;
}else{
av_log(h->s.avctx, AV_LOG_ERROR, "AVC: nal size %d\n", nalsize);
break;
}
}
next_avc= buf_index + nalsize;
} else {
// start code prefix search
for(; buf_index + 3 < next_avc; buf_index++){
// This should always succeed in the first iteration.
if(buf[buf_index] == 0 && buf[buf_index+1] == 0 && buf[buf_index+2] == 1)
break;
}
if(buf_index+3 >= buf_size) break;
buf_index+=3;
if(buf_index >= next_avc) continue;
}
hx = h->thread_context[context_count];
ptr= ff_h264_decode_nal(hx, buf + buf_index, &dst_length, &consumed, next_avc - buf_index);
if (ptr==NULL || dst_length < 0){
return -1;
}
i= buf_index + consumed;
if((s->workaround_bugs & FF_BUG_AUTODETECT) && i+3<next_avc &&
buf[i]==0x00 && buf[i+1]==0x00 && buf[i+2]==0x01 && buf[i+3]==0xE0)
s->workaround_bugs |= FF_BUG_TRUNCATED;
if(!(s->workaround_bugs & FF_BUG_TRUNCATED)){
while(ptr[dst_length - 1] == 0 && dst_length > 0)
dst_length--;
}
bit_length= !dst_length ? 0 : (8*dst_length - ff_h264_decode_rbsp_trailing(h, ptr + dst_length - 1));
if(s->avctx->debug&FF_DEBUG_STARTCODE){
av_log(h->s.avctx, AV_LOG_DEBUG, "NAL %d at %d/%d length %d\n", hx->nal_unit_type, buf_index, buf_size, dst_length);
}
if (h->is_avc && (nalsize != consumed) && nalsize){
av_log(h->s.avctx, AV_LOG_DEBUG, "AVC: Consumed only %d bytes instead of %d\n", consumed, nalsize);
}
buf_index += consumed;
if( (s->hurry_up == 1 && h->nal_ref_idc == 0) //FIXME do not discard SEI id
||(avctx->skip_frame >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
continue;
again:
err = 0;
switch(hx->nal_unit_type){
case NAL_IDR_SLICE:
if (h->nal_unit_type != NAL_IDR_SLICE) {
av_log(h->s.avctx, AV_LOG_ERROR, "Invalid mix of idr and non-idr slices");
return -1;
}
idr(h); //FIXME ensure we don't loose some frames if there is reordering
case NAL_SLICE:
init_get_bits(&hx->s.gb, ptr, bit_length);
hx->intra_gb_ptr=
hx->inter_gb_ptr= &hx->s.gb;
hx->s.data_partitioning = 0;
if((err = decode_slice_header(hx, h)))
break;
if (h->current_slice == 1) {
if (s->avctx->hwaccel && s->avctx->hwaccel->start_frame(s->avctx, NULL, 0) < 0)
return -1;
if(CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
ff_vdpau_h264_picture_start(s);
}
s->current_picture_ptr->key_frame |=
(hx->nal_unit_type == NAL_IDR_SLICE) ||
(h->sei_recovery_frame_cnt >= 0);
if(hx->redundant_pic_count==0 && hx->s.hurry_up < 5
&& (avctx->skip_frame < AVDISCARD_NONREF || hx->nal_ref_idc)
&& (avctx->skip_frame < AVDISCARD_BIDIR || hx->slice_type_nos!=FF_B_TYPE)
&& (avctx->skip_frame < AVDISCARD_NONKEY || hx->slice_type_nos==FF_I_TYPE)
&& avctx->skip_frame < AVDISCARD_ALL){
if(avctx->hwaccel) {
if (avctx->hwaccel->decode_slice(avctx, &buf[buf_index - consumed], consumed) < 0)
return -1;
}else
if(CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU){
static const uint8_t start_code[] = {0x00, 0x00, 0x01};
ff_vdpau_add_data_chunk(s, start_code, sizeof(start_code));
ff_vdpau_add_data_chunk(s, &buf[buf_index - consumed], consumed );
}else
context_count++;
}
break;
case NAL_DPA:
init_get_bits(&hx->s.gb, ptr, bit_length);
hx->intra_gb_ptr=
hx->inter_gb_ptr= NULL;
if ((err = decode_slice_header(hx, h)) < 0)
break;
hx->s.data_partitioning = 1;
break;
case NAL_DPB:
init_get_bits(&hx->intra_gb, ptr, bit_length);
hx->intra_gb_ptr= &hx->intra_gb;
break;
case NAL_DPC:
init_get_bits(&hx->inter_gb, ptr, bit_length);
hx->inter_gb_ptr= &hx->inter_gb;
if(hx->redundant_pic_count==0 && hx->intra_gb_ptr && hx->s.data_partitioning
&& s->context_initialized
&& s->hurry_up < 5
&& (avctx->skip_frame < AVDISCARD_NONREF || hx->nal_ref_idc)
&& (avctx->skip_frame < AVDISCARD_BIDIR || hx->slice_type_nos!=FF_B_TYPE)
&& (avctx->skip_frame < AVDISCARD_NONKEY || hx->slice_type_nos==FF_I_TYPE)
&& avctx->skip_frame < AVDISCARD_ALL)
context_count++;
break;
case NAL_SEI:
init_get_bits(&s->gb, ptr, bit_length);
ff_h264_decode_sei(h);
break;
case NAL_SPS:
init_get_bits(&s->gb, ptr, bit_length);
ff_h264_decode_seq_parameter_set(h);
if(s->flags& CODEC_FLAG_LOW_DELAY)
s->low_delay=1;
if(avctx->has_b_frames < 2)
avctx->has_b_frames= !s->low_delay;
break;
case NAL_PPS:
init_get_bits(&s->gb, ptr, bit_length);
ff_h264_decode_picture_parameter_set(h, bit_length);
break;
case NAL_AUD:
case NAL_END_SEQUENCE:
case NAL_END_STREAM:
case NAL_FILLER_DATA:
case NAL_SPS_EXT:
case NAL_AUXILIARY_SLICE:
break;
default:
av_log(avctx, AV_LOG_DEBUG, "Unknown NAL code: %d (%d bits)\n", hx->nal_unit_type, bit_length);
}
if(context_count == h->max_contexts) {
execute_decode_slices(h, context_count);
context_count = 0;
}
if (err < 0)
av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
else if(err == 1) {
/* Slice could not be decoded in parallel mode, copy down
* NAL unit stuff to context 0 and restart. Note that
* rbsp_buffer is not transferred, but since we no longer
* run in parallel mode this should not be an issue. */
h->nal_unit_type = hx->nal_unit_type;
h->nal_ref_idc = hx->nal_ref_idc;
hx = h;
goto again;
}
}
if(context_count)
execute_decode_slices(h, context_count);
return buf_index;
}
/**
* returns the number of bytes consumed for building the current frame
*/
static int get_consumed_bytes(MpegEncContext *s, int pos, int buf_size){
if(pos==0) pos=1; //avoid infinite loops (i doubt that is needed but ...)
if(pos+10>buf_size) pos=buf_size; // oops ;)
return pos;
}
static int decode_frame(AVCodecContext *avctx,
void *data, int *data_size,
AVPacket *avpkt)
{
const uint8_t *buf = avpkt->data;
int buf_size = avpkt->size;
H264Context *h = avctx->priv_data;
MpegEncContext *s = &h->s;
AVFrame *pict = data;
int buf_index;
s->flags= avctx->flags;
s->flags2= avctx->flags2;
/* end of stream, output what is still in the buffers */
if (buf_size == 0) {
Picture *out;
int i, out_idx;
//FIXME factorize this with the output code below
out = h->delayed_pic[0];
out_idx = 0;
for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame && !h->delayed_pic[i]->mmco_reset; i++)
if(h->delayed_pic[i]->poc < out->poc){
out = h->delayed_pic[i];
out_idx = i;
}
for(i=out_idx; h->delayed_pic[i]; i++)
h->delayed_pic[i] = h->delayed_pic[i+1];
if(out){
*data_size = sizeof(AVFrame);
*pict= *(AVFrame*)out;
}
return 0;
}
buf_index=decode_nal_units(h, buf, buf_size);
if(buf_index < 0)
return -1;
if(!(s->flags2 & CODEC_FLAG2_CHUNKS) && !s->current_picture_ptr){
if (avctx->skip_frame >= AVDISCARD_NONREF || s->hurry_up) return 0;
av_log(avctx, AV_LOG_ERROR, "no frame!\n");
return -1;
}
if(!(s->flags2 & CODEC_FLAG2_CHUNKS) || (s->mb_y >= s->mb_height && s->mb_height)){
Picture *out = s->current_picture_ptr;
Picture *cur = s->current_picture_ptr;
int i, pics, out_of_order, out_idx;
field_end(h);
if (cur->field_poc[0]==INT_MAX || cur->field_poc[1]==INT_MAX) {
/* Wait for second field. */
*data_size = 0;
} else {
cur->interlaced_frame = 0;
cur->repeat_pict = 0;
/* Signal interlacing information externally. */
/* Prioritize picture timing SEI information over used decoding process if it exists. */
if(h->sps.pic_struct_present_flag){
switch (h->sei_pic_struct)
{
case SEI_PIC_STRUCT_FRAME:
break;
case SEI_PIC_STRUCT_TOP_FIELD:
case SEI_PIC_STRUCT_BOTTOM_FIELD:
cur->interlaced_frame = 1;
break;
case SEI_PIC_STRUCT_TOP_BOTTOM:
case SEI_PIC_STRUCT_BOTTOM_TOP:
if (FIELD_OR_MBAFF_PICTURE)
cur->interlaced_frame = 1;
else
// try to flag soft telecine progressive
cur->interlaced_frame = h->prev_interlaced_frame;
break;
case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
// Signal the possibility of telecined film externally (pic_struct 5,6)
// From these hints, let the applications decide if they apply deinterlacing.
cur->repeat_pict = 1;
break;
case SEI_PIC_STRUCT_FRAME_DOUBLING:
// Force progressive here, as doubling interlaced frame is a bad idea.
cur->repeat_pict = 2;
break;
case SEI_PIC_STRUCT_FRAME_TRIPLING:
cur->repeat_pict = 4;
break;
}
if ((h->sei_ct_type & 3) && h->sei_pic_struct <= SEI_PIC_STRUCT_BOTTOM_TOP)
cur->interlaced_frame = (h->sei_ct_type & (1<<1)) != 0;
}else{
/* Derive interlacing flag from used decoding process. */
cur->interlaced_frame = FIELD_OR_MBAFF_PICTURE;
}
h->prev_interlaced_frame = cur->interlaced_frame;
if (cur->field_poc[0] != cur->field_poc[1]){
/* Derive top_field_first from field pocs. */
cur->top_field_first = cur->field_poc[0] < cur->field_poc[1];
}else{
if(cur->interlaced_frame || h->sps.pic_struct_present_flag){
/* Use picture timing SEI information. Even if it is a information of a past frame, better than nothing. */
if(h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM
|| h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM_TOP)
cur->top_field_first = 1;
else
cur->top_field_first = 0;
}else{
/* Most likely progressive */
cur->top_field_first = 0;
}
}
//FIXME do something with unavailable reference frames
/* Sort B-frames into display order */
if(h->sps.bitstream_restriction_flag
&& s->avctx->has_b_frames < h->sps.num_reorder_frames){
s->avctx->has_b_frames = h->sps.num_reorder_frames;
s->low_delay = 0;
}
if( s->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT
&& !h->sps.bitstream_restriction_flag){
s->avctx->has_b_frames= MAX_DELAYED_PIC_COUNT;
s->low_delay= 0;
}
pics = 0;
while(h->delayed_pic[pics]) pics++;
assert(pics <= MAX_DELAYED_PIC_COUNT);
h->delayed_pic[pics++] = cur;
if(cur->reference == 0)
cur->reference = DELAYED_PIC_REF;
out = h->delayed_pic[0];
out_idx = 0;
for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame && !h->delayed_pic[i]->mmco_reset; i++)
if(h->delayed_pic[i]->poc < out->poc){
out = h->delayed_pic[i];
out_idx = i;
}
if(s->avctx->has_b_frames == 0 && (h->delayed_pic[0]->key_frame || h->delayed_pic[0]->mmco_reset))
h->outputed_poc= INT_MIN;
out_of_order = out->poc < h->outputed_poc;
if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames >= h->sps.num_reorder_frames)
{ }
else if((out_of_order && pics-1 == s->avctx->has_b_frames && s->avctx->has_b_frames < MAX_DELAYED_PIC_COUNT)
|| (s->low_delay &&
((h->outputed_poc != INT_MIN && out->poc > h->outputed_poc + 2)
|| cur->pict_type == FF_B_TYPE)))
{
s->low_delay = 0;
s->avctx->has_b_frames++;
}
if(out_of_order || pics > s->avctx->has_b_frames){
out->reference &= ~DELAYED_PIC_REF;
for(i=out_idx; h->delayed_pic[i]; i++)
h->delayed_pic[i] = h->delayed_pic[i+1];
}
if(!out_of_order && pics > s->avctx->has_b_frames){
*data_size = sizeof(AVFrame);
if(out_idx==0 && h->delayed_pic[0] && (h->delayed_pic[0]->key_frame || h->delayed_pic[0]->mmco_reset)) {
h->outputed_poc = INT_MIN;
} else
h->outputed_poc = out->poc;
*pict= *(AVFrame*)out;
}else{
av_log(avctx, AV_LOG_DEBUG, "no picture\n");
}
}
}
assert(pict->data[0] || !*data_size);
ff_print_debug_info(s, pict);
//printf("out %d\n", (int)pict->data[0]);
return get_consumed_bytes(s, buf_index, buf_size);
}
#if 0
static inline void fill_mb_avail(H264Context *h){
MpegEncContext * const s = &h->s;
const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
if(s->mb_y){
h->mb_avail[0]= s->mb_x && h->slice_table[mb_xy - s->mb_stride - 1] == h->slice_num;
h->mb_avail[1]= h->slice_table[mb_xy - s->mb_stride ] == h->slice_num;
h->mb_avail[2]= s->mb_x+1 < s->mb_width && h->slice_table[mb_xy - s->mb_stride + 1] == h->slice_num;
}else{
h->mb_avail[0]=
h->mb_avail[1]=
h->mb_avail[2]= 0;
}
h->mb_avail[3]= s->mb_x && h->slice_table[mb_xy - 1] == h->slice_num;
h->mb_avail[4]= 1; //FIXME move out
h->mb_avail[5]= 0; //FIXME move out
}
#endif
#ifdef TEST
#undef printf
#undef random
#define COUNT 8000
#define SIZE (COUNT*40)
int main(void){
int i;
uint8_t temp[SIZE];
PutBitContext pb;
GetBitContext gb;
// int int_temp[10000];
DSPContext dsp;
AVCodecContext avctx;
dsputil_init(&dsp, &avctx);
init_put_bits(&pb, temp, SIZE);
printf("testing unsigned exp golomb\n");
for(i=0; i<COUNT; i++){
START_TIMER
set_ue_golomb(&pb, i);
STOP_TIMER("set_ue_golomb");
}
flush_put_bits(&pb);
init_get_bits(&gb, temp, 8*SIZE);
for(i=0; i<COUNT; i++){
int j, s;
s= show_bits(&gb, 24);
START_TIMER
j= get_ue_golomb(&gb);
if(j != i){
printf("mismatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
// return -1;
}
STOP_TIMER("get_ue_golomb");
}
init_put_bits(&pb, temp, SIZE);
printf("testing signed exp golomb\n");
for(i=0; i<COUNT; i++){
START_TIMER
set_se_golomb(&pb, i - COUNT/2);
STOP_TIMER("set_se_golomb");
}
flush_put_bits(&pb);
init_get_bits(&gb, temp, 8*SIZE);
for(i=0; i<COUNT; i++){
int j, s;
s= show_bits(&gb, 24);
START_TIMER
j= get_se_golomb(&gb);
if(j != i - COUNT/2){
printf("mismatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
// return -1;
}
STOP_TIMER("get_se_golomb");
}
#if 0
printf("testing 4x4 (I)DCT\n");
DCTELEM block[16];
uint8_t src[16], ref[16];
uint64_t error= 0, max_error=0;
for(i=0; i<COUNT; i++){
int j;
// printf("%d %d %d\n", r1, r2, (r2-r1)*16);
for(j=0; j<16; j++){
ref[j]= random()%255;
src[j]= random()%255;
}
h264_diff_dct_c(block, src, ref, 4);
//normalize
for(j=0; j<16; j++){
// printf("%d ", block[j]);
block[j]= block[j]*4;
if(j&1) block[j]= (block[j]*4 + 2)/5;
if(j&4) block[j]= (block[j]*4 + 2)/5;
}
// printf("\n");
h->h264dsp.h264_idct_add(ref, block, 4);
/* for(j=0; j<16; j++){
printf("%d ", ref[j]);
}
printf("\n");*/
for(j=0; j<16; j++){
int diff= FFABS(src[j] - ref[j]);
error+= diff*diff;
max_error= FFMAX(max_error, diff);
}
}
printf("error=%f max_error=%d\n", ((float)error)/COUNT/16, (int)max_error );
printf("testing quantizer\n");
for(qp=0; qp<52; qp++){
for(i=0; i<16; i++)
src1_block[i]= src2_block[i]= random()%255;
}
printf("Testing NAL layer\n");
uint8_t bitstream[COUNT];
uint8_t nal[COUNT*2];
H264Context h;
memset(&h, 0, sizeof(H264Context));
for(i=0; i<COUNT; i++){
int zeros= i;
int nal_length;
int consumed;
int out_length;
uint8_t *out;
int j;
for(j=0; j<COUNT; j++){
bitstream[j]= (random() % 255) + 1;
}
for(j=0; j<zeros; j++){
int pos= random() % COUNT;
while(bitstream[pos] == 0){
pos++;
pos %= COUNT;
}
bitstream[pos]=0;
}
START_TIMER
nal_length= encode_nal(&h, nal, bitstream, COUNT, COUNT*2);
if(nal_length<0){
printf("encoding failed\n");
return -1;
}
out= ff_h264_decode_nal(&h, nal, &out_length, &consumed, nal_length);
STOP_TIMER("NAL")
if(out_length != COUNT){
printf("incorrect length %d %d\n", out_length, COUNT);
return -1;
}
if(consumed != nal_length){
printf("incorrect consumed length %d %d\n", nal_length, consumed);
return -1;
}
if(memcmp(bitstream, out, COUNT)){
printf("mismatch\n");
return -1;
}
}
#endif
printf("Testing RBSP\n");
return 0;
}
#endif /* TEST */
av_cold void ff_h264_free_context(H264Context *h)
{
int i;
free_tables(h); //FIXME cleanup init stuff perhaps
for(i = 0; i < MAX_SPS_COUNT; i++)
av_freep(h->sps_buffers + i);
for(i = 0; i < MAX_PPS_COUNT; i++)
av_freep(h->pps_buffers + i);
}
av_cold int ff_h264_decode_end(AVCodecContext *avctx)
{
H264Context *h = avctx->priv_data;
MpegEncContext *s = &h->s;
ff_h264_free_context(h);
MPV_common_end(s);
// memset(h, 0, sizeof(H264Context));
return 0;
}
AVCodec h264_decoder = {
"h264",
AVMEDIA_TYPE_VIDEO,
CODEC_ID_H264,
sizeof(H264Context),
ff_h264_decode_init,
NULL,
ff_h264_decode_end,
decode_frame,
/*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_DELAY,
.flush= flush_dpb,
.long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
.pix_fmts= ff_hwaccel_pixfmt_list_420,
};
#if CONFIG_H264_VDPAU_DECODER
AVCodec h264_vdpau_decoder = {
"h264_vdpau",
AVMEDIA_TYPE_VIDEO,
CODEC_ID_H264,
sizeof(H264Context),
ff_h264_decode_init,
NULL,
ff_h264_decode_end,
decode_frame,
CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
.flush= flush_dpb,
.long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (VDPAU acceleration)"),
.pix_fmts = (const enum PixelFormat[]){PIX_FMT_VDPAU_H264, PIX_FMT_NONE},
};
#endif
| 123linslouis-android-video-cutter | jni/libavcodec/h264.c | C | asf20 | 128,608 |
/*
* Wmapro compatible decoder
* Copyright (c) 2007 Baptiste Coudurier, Benjamin Larsson, Ulion
* Copyright (c) 2008 - 2009 Sascha Sommer, Benjamin Larsson
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* @brief wmapro decoder implementation
* Wmapro is an MDCT based codec comparable to wma standard or AAC.
* The decoding therefore consists of the following steps:
* - bitstream decoding
* - reconstruction of per-channel data
* - rescaling and inverse quantization
* - IMDCT
* - windowing and overlapp-add
*
* The compressed wmapro bitstream is split into individual packets.
* Every such packet contains one or more wma frames.
* The compressed frames may have a variable length and frames may
* cross packet boundaries.
* Common to all wmapro frames is the number of samples that are stored in
* a frame.
* The number of samples and a few other decode flags are stored
* as extradata that has to be passed to the decoder.
*
* The wmapro frames themselves are again split into a variable number of
* subframes. Every subframe contains the data for 2^N time domain samples
* where N varies between 7 and 12.
*
* Example wmapro bitstream (in samples):
*
* || packet 0 || packet 1 || packet 2 packets
* ---------------------------------------------------
* || frame 0 || frame 1 || frame 2 || frames
* ---------------------------------------------------
* || | | || | | | || || subframes of channel 0
* ---------------------------------------------------
* || | | || | | | || || subframes of channel 1
* ---------------------------------------------------
*
* The frame layouts for the individual channels of a wma frame does not need
* to be the same.
*
* However, if the offsets and lengths of several subframes of a frame are the
* same, the subframes of the channels can be grouped.
* Every group may then use special coding techniques like M/S stereo coding
* to improve the compression ratio. These channel transformations do not
* need to be applied to a whole subframe. Instead, they can also work on
* individual scale factor bands (see below).
* The coefficients that carry the audio signal in the frequency domain
* are transmitted as huffman-coded vectors with 4, 2 and 1 elements.
* In addition to that, the encoder can switch to a runlevel coding scheme
* by transmitting subframe_length / 128 zero coefficients.
*
* Before the audio signal can be converted to the time domain, the
* coefficients have to be rescaled and inverse quantized.
* A subframe is therefore split into several scale factor bands that get
* scaled individually.
* Scale factors are submitted for every frame but they might be shared
* between the subframes of a channel. Scale factors are initially DPCM-coded.
* Once scale factors are shared, the differences are transmitted as runlevel
* codes.
* Every subframe length and offset combination in the frame layout shares a
* common quantization factor that can be adjusted for every channel by a
* modifier.
* After the inverse quantization, the coefficients get processed by an IMDCT.
* The resulting values are then windowed with a sine window and the first half
* of the values are added to the second half of the output from the previous
* subframe in order to reconstruct the output samples.
*/
#include "avcodec.h"
#include "internal.h"
#include "get_bits.h"
#include "put_bits.h"
#include "wmaprodata.h"
#include "dsputil.h"
#include "wma.h"
/** current decoder limitations */
#define WMAPRO_MAX_CHANNELS 8 ///< max number of handled channels
#define MAX_SUBFRAMES 32 ///< max number of subframes per channel
#define MAX_BANDS 29 ///< max number of scale factor bands
#define MAX_FRAMESIZE 32768 ///< maximum compressed frame size
#define WMAPRO_BLOCK_MAX_BITS 12 ///< log2 of max block size
#define WMAPRO_BLOCK_MAX_SIZE (1 << WMAPRO_BLOCK_MAX_BITS) ///< maximum block size
#define WMAPRO_BLOCK_SIZES (WMAPRO_BLOCK_MAX_BITS - BLOCK_MIN_BITS + 1) ///< possible block sizes
#define VLCBITS 9
#define SCALEVLCBITS 8
#define VEC4MAXDEPTH ((HUFF_VEC4_MAXBITS+VLCBITS-1)/VLCBITS)
#define VEC2MAXDEPTH ((HUFF_VEC2_MAXBITS+VLCBITS-1)/VLCBITS)
#define VEC1MAXDEPTH ((HUFF_VEC1_MAXBITS+VLCBITS-1)/VLCBITS)
#define SCALEMAXDEPTH ((HUFF_SCALE_MAXBITS+SCALEVLCBITS-1)/SCALEVLCBITS)
#define SCALERLMAXDEPTH ((HUFF_SCALE_RL_MAXBITS+VLCBITS-1)/VLCBITS)
static VLC sf_vlc; ///< scale factor DPCM vlc
static VLC sf_rl_vlc; ///< scale factor run length vlc
static VLC vec4_vlc; ///< 4 coefficients per symbol
static VLC vec2_vlc; ///< 2 coefficients per symbol
static VLC vec1_vlc; ///< 1 coefficient per symbol
static VLC coef_vlc[2]; ///< coefficient run length vlc codes
static float sin64[33]; ///< sinus table for decorrelation
/**
* @brief frame specific decoder context for a single channel
*/
typedef struct {
int16_t prev_block_len; ///< length of the previous block
uint8_t transmit_coefs;
uint8_t num_subframes;
uint16_t subframe_len[MAX_SUBFRAMES]; ///< subframe length in samples
uint16_t subframe_offset[MAX_SUBFRAMES]; ///< subframe positions in the current frame
uint8_t cur_subframe; ///< current subframe number
uint16_t decoded_samples; ///< number of already processed samples
uint8_t grouped; ///< channel is part of a group
int quant_step; ///< quantization step for the current subframe
int8_t reuse_sf; ///< share scale factors between subframes
int8_t scale_factor_step; ///< scaling step for the current subframe
int max_scale_factor; ///< maximum scale factor for the current subframe
int saved_scale_factors[2][MAX_BANDS]; ///< resampled and (previously) transmitted scale factor values
int8_t scale_factor_idx; ///< index for the transmitted scale factor values (used for resampling)
int* scale_factors; ///< pointer to the scale factor values used for decoding
uint8_t table_idx; ///< index in sf_offsets for the scale factor reference block
float* coeffs; ///< pointer to the subframe decode buffer
DECLARE_ALIGNED(16, float, out)[WMAPRO_BLOCK_MAX_SIZE + WMAPRO_BLOCK_MAX_SIZE / 2]; ///< output buffer
} WMAProChannelCtx;
/**
* @brief channel group for channel transformations
*/
typedef struct {
uint8_t num_channels; ///< number of channels in the group
int8_t transform; ///< transform on / off
int8_t transform_band[MAX_BANDS]; ///< controls if the transform is enabled for a certain band
float decorrelation_matrix[WMAPRO_MAX_CHANNELS*WMAPRO_MAX_CHANNELS];
float* channel_data[WMAPRO_MAX_CHANNELS]; ///< transformation coefficients
} WMAProChannelGrp;
/**
* @brief main decoder context
*/
typedef struct WMAProDecodeCtx {
/* generic decoder variables */
AVCodecContext* avctx; ///< codec context for av_log
DSPContext dsp; ///< accelerated DSP functions
uint8_t frame_data[MAX_FRAMESIZE +
FF_INPUT_BUFFER_PADDING_SIZE];///< compressed frame data
PutBitContext pb; ///< context for filling the frame_data buffer
FFTContext mdct_ctx[WMAPRO_BLOCK_SIZES]; ///< MDCT context per block size
DECLARE_ALIGNED(16, float, tmp)[WMAPRO_BLOCK_MAX_SIZE]; ///< IMDCT output buffer
float* windows[WMAPRO_BLOCK_SIZES]; ///< windows for the different block sizes
/* frame size dependent frame information (set during initialization) */
uint32_t decode_flags; ///< used compression features
uint8_t len_prefix; ///< frame is prefixed with its length
uint8_t dynamic_range_compression; ///< frame contains DRC data
uint8_t bits_per_sample; ///< integer audio sample size for the unscaled IMDCT output (used to scale to [-1.0, 1.0])
uint16_t samples_per_frame; ///< number of samples to output
uint16_t log2_frame_size;
int8_t num_channels; ///< number of channels in the stream (same as AVCodecContext.num_channels)
int8_t lfe_channel; ///< lfe channel index
uint8_t max_num_subframes;
uint8_t subframe_len_bits; ///< number of bits used for the subframe length
uint8_t max_subframe_len_bit; ///< flag indicating that the subframe is of maximum size when the first subframe length bit is 1
uint16_t min_samples_per_subframe;
int8_t num_sfb[WMAPRO_BLOCK_SIZES]; ///< scale factor bands per block size
int16_t sfb_offsets[WMAPRO_BLOCK_SIZES][MAX_BANDS]; ///< scale factor band offsets (multiples of 4)
int8_t sf_offsets[WMAPRO_BLOCK_SIZES][WMAPRO_BLOCK_SIZES][MAX_BANDS]; ///< scale factor resample matrix
int16_t subwoofer_cutoffs[WMAPRO_BLOCK_SIZES]; ///< subwoofer cutoff values
/* packet decode state */
GetBitContext pgb; ///< bitstream reader context for the packet
uint8_t packet_offset; ///< frame offset in the packet
uint8_t packet_sequence_number; ///< current packet number
int num_saved_bits; ///< saved number of bits
int frame_offset; ///< frame offset in the bit reservoir
int subframe_offset; ///< subframe offset in the bit reservoir
uint8_t packet_loss; ///< set in case of bitstream error
uint8_t packet_done; ///< set when a packet is fully decoded
/* frame decode state */
uint32_t frame_num; ///< current frame number (not used for decoding)
GetBitContext gb; ///< bitstream reader context
int buf_bit_size; ///< buffer size in bits
float* samples; ///< current samplebuffer pointer
float* samples_end; ///< maximum samplebuffer pointer
uint8_t drc_gain; ///< gain for the DRC tool
int8_t skip_frame; ///< skip output step
int8_t parsed_all_subframes; ///< all subframes decoded?
/* subframe/block decode state */
int16_t subframe_len; ///< current subframe length
int8_t channels_for_cur_subframe; ///< number of channels that contain the subframe
int8_t channel_indexes_for_cur_subframe[WMAPRO_MAX_CHANNELS];
int8_t num_bands; ///< number of scale factor bands
int16_t* cur_sfb_offsets; ///< sfb offsets for the current block
uint8_t table_idx; ///< index for the num_sfb, sfb_offsets, sf_offsets and subwoofer_cutoffs tables
int8_t esc_len; ///< length of escaped coefficients
uint8_t num_chgroups; ///< number of channel groups
WMAProChannelGrp chgroup[WMAPRO_MAX_CHANNELS]; ///< channel group information
WMAProChannelCtx channel[WMAPRO_MAX_CHANNELS]; ///< per channel data
} WMAProDecodeCtx;
/**
*@brief helper function to print the most important members of the context
*@param s context
*/
static void av_cold dump_context(WMAProDecodeCtx *s)
{
#define PRINT(a, b) av_log(s->avctx, AV_LOG_DEBUG, " %s = %d\n", a, b);
#define PRINT_HEX(a, b) av_log(s->avctx, AV_LOG_DEBUG, " %s = %x\n", a, b);
PRINT("ed sample bit depth", s->bits_per_sample);
PRINT_HEX("ed decode flags", s->decode_flags);
PRINT("samples per frame", s->samples_per_frame);
PRINT("log2 frame size", s->log2_frame_size);
PRINT("max num subframes", s->max_num_subframes);
PRINT("len prefix", s->len_prefix);
PRINT("num channels", s->num_channels);
}
/**
*@brief Uninitialize the decoder and free all resources.
*@param avctx codec context
*@return 0 on success, < 0 otherwise
*/
static av_cold int decode_end(AVCodecContext *avctx)
{
WMAProDecodeCtx *s = avctx->priv_data;
int i;
for (i = 0; i < WMAPRO_BLOCK_SIZES; i++)
ff_mdct_end(&s->mdct_ctx[i]);
return 0;
}
/**
*@brief Initialize the decoder.
*@param avctx codec context
*@return 0 on success, -1 otherwise
*/
static av_cold int decode_init(AVCodecContext *avctx)
{
WMAProDecodeCtx *s = avctx->priv_data;
uint8_t *edata_ptr = avctx->extradata;
unsigned int channel_mask;
int i;
int log2_max_num_subframes;
int num_possible_block_sizes;
s->avctx = avctx;
dsputil_init(&s->dsp, avctx);
init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
avctx->sample_fmt = SAMPLE_FMT_FLT;
if (avctx->extradata_size >= 18) {
s->decode_flags = AV_RL16(edata_ptr+14);
channel_mask = AV_RL32(edata_ptr+2);
s->bits_per_sample = AV_RL16(edata_ptr);
/** dump the extradata */
for (i = 0; i < avctx->extradata_size; i++)
dprintf(avctx, "[%x] ", avctx->extradata[i]);
dprintf(avctx, "\n");
} else {
av_log_ask_for_sample(avctx, "Unknown extradata size\n");
return AVERROR_INVALIDDATA;
}
/** generic init */
s->log2_frame_size = av_log2(avctx->block_align) + 4;
/** frame info */
s->skip_frame = 1; /** skip first frame */
s->packet_loss = 1;
s->len_prefix = (s->decode_flags & 0x40);
if (!s->len_prefix) {
av_log_ask_for_sample(avctx, "no length prefix\n");
return AVERROR_INVALIDDATA;
}
/** get frame len */
s->samples_per_frame = 1 << ff_wma_get_frame_len_bits(avctx->sample_rate,
3, s->decode_flags);
/** init previous block len */
for (i = 0; i < avctx->channels; i++)
s->channel[i].prev_block_len = s->samples_per_frame;
/** subframe info */
log2_max_num_subframes = ((s->decode_flags & 0x38) >> 3);
s->max_num_subframes = 1 << log2_max_num_subframes;
if (s->max_num_subframes == 16)
s->max_subframe_len_bit = 1;
s->subframe_len_bits = av_log2(log2_max_num_subframes) + 1;
num_possible_block_sizes = log2_max_num_subframes + 1;
s->min_samples_per_subframe = s->samples_per_frame / s->max_num_subframes;
s->dynamic_range_compression = (s->decode_flags & 0x80);
if (s->max_num_subframes > MAX_SUBFRAMES) {
av_log(avctx, AV_LOG_ERROR, "invalid number of subframes %i\n",
s->max_num_subframes);
return AVERROR_INVALIDDATA;
}
s->num_channels = avctx->channels;
/** extract lfe channel position */
s->lfe_channel = -1;
if (channel_mask & 8) {
unsigned int mask;
for (mask = 1; mask < 16; mask <<= 1) {
if (channel_mask & mask)
++s->lfe_channel;
}
}
if (s->num_channels < 0) {
av_log(avctx, AV_LOG_ERROR, "invalid number of channels %d\n", s->num_channels);
return AVERROR_INVALIDDATA;
} else if (s->num_channels > WMAPRO_MAX_CHANNELS) {
av_log_ask_for_sample(avctx, "unsupported number of channels\n");
return AVERROR_PATCHWELCOME;
}
INIT_VLC_STATIC(&sf_vlc, SCALEVLCBITS, HUFF_SCALE_SIZE,
scale_huffbits, 1, 1,
scale_huffcodes, 2, 2, 616);
INIT_VLC_STATIC(&sf_rl_vlc, VLCBITS, HUFF_SCALE_RL_SIZE,
scale_rl_huffbits, 1, 1,
scale_rl_huffcodes, 4, 4, 1406);
INIT_VLC_STATIC(&coef_vlc[0], VLCBITS, HUFF_COEF0_SIZE,
coef0_huffbits, 1, 1,
coef0_huffcodes, 4, 4, 2108);
INIT_VLC_STATIC(&coef_vlc[1], VLCBITS, HUFF_COEF1_SIZE,
coef1_huffbits, 1, 1,
coef1_huffcodes, 4, 4, 3912);
INIT_VLC_STATIC(&vec4_vlc, VLCBITS, HUFF_VEC4_SIZE,
vec4_huffbits, 1, 1,
vec4_huffcodes, 2, 2, 604);
INIT_VLC_STATIC(&vec2_vlc, VLCBITS, HUFF_VEC2_SIZE,
vec2_huffbits, 1, 1,
vec2_huffcodes, 2, 2, 562);
INIT_VLC_STATIC(&vec1_vlc, VLCBITS, HUFF_VEC1_SIZE,
vec1_huffbits, 1, 1,
vec1_huffcodes, 2, 2, 562);
/** calculate number of scale factor bands and their offsets
for every possible block size */
for (i = 0; i < num_possible_block_sizes; i++) {
int subframe_len = s->samples_per_frame >> i;
int x;
int band = 1;
s->sfb_offsets[i][0] = 0;
for (x = 0; x < MAX_BANDS-1 && s->sfb_offsets[i][band - 1] < subframe_len; x++) {
int offset = (subframe_len * 2 * critical_freq[x])
/ s->avctx->sample_rate + 2;
offset &= ~3;
if (offset > s->sfb_offsets[i][band - 1])
s->sfb_offsets[i][band++] = offset;
}
s->sfb_offsets[i][band - 1] = subframe_len;
s->num_sfb[i] = band - 1;
}
/** Scale factors can be shared between blocks of different size
as every block has a different scale factor band layout.
The matrix sf_offsets is needed to find the correct scale factor.
*/
for (i = 0; i < num_possible_block_sizes; i++) {
int b;
for (b = 0; b < s->num_sfb[i]; b++) {
int x;
int offset = ((s->sfb_offsets[i][b]
+ s->sfb_offsets[i][b + 1] - 1) << i) >> 1;
for (x = 0; x < num_possible_block_sizes; x++) {
int v = 0;
while (s->sfb_offsets[x][v + 1] << x < offset)
++v;
s->sf_offsets[i][x][b] = v;
}
}
}
/** init MDCT, FIXME: only init needed sizes */
for (i = 0; i < WMAPRO_BLOCK_SIZES; i++)
ff_mdct_init(&s->mdct_ctx[i], BLOCK_MIN_BITS+1+i, 1,
1.0 / (1 << (BLOCK_MIN_BITS + i - 1))
/ (1 << (s->bits_per_sample - 1)));
/** init MDCT windows: simple sinus window */
for (i = 0; i < WMAPRO_BLOCK_SIZES; i++) {
const int win_idx = WMAPRO_BLOCK_MAX_BITS - i;
ff_init_ff_sine_windows(win_idx);
s->windows[WMAPRO_BLOCK_SIZES - i - 1] = ff_sine_windows[win_idx];
}
/** calculate subwoofer cutoff values */
for (i = 0; i < num_possible_block_sizes; i++) {
int block_size = s->samples_per_frame >> i;
int cutoff = (440*block_size + 3 * (s->avctx->sample_rate >> 1) - 1)
/ s->avctx->sample_rate;
s->subwoofer_cutoffs[i] = av_clip(cutoff, 4, block_size);
}
/** calculate sine values for the decorrelation matrix */
for (i = 0; i < 33; i++)
sin64[i] = sin(i*M_PI / 64.0);
if (avctx->debug & FF_DEBUG_BITSTREAM)
dump_context(s);
avctx->channel_layout = channel_mask;
return 0;
}
/**
*@brief Decode the subframe length.
*@param s context
*@param offset sample offset in the frame
*@return decoded subframe length on success, < 0 in case of an error
*/
static int decode_subframe_length(WMAProDecodeCtx *s, int offset)
{
int frame_len_shift = 0;
int subframe_len;
/** no need to read from the bitstream when only one length is possible */
if (offset == s->samples_per_frame - s->min_samples_per_subframe)
return s->min_samples_per_subframe;
/** 1 bit indicates if the subframe is of maximum length */
if (s->max_subframe_len_bit) {
if (get_bits1(&s->gb))
frame_len_shift = 1 + get_bits(&s->gb, s->subframe_len_bits-1);
} else
frame_len_shift = get_bits(&s->gb, s->subframe_len_bits);
subframe_len = s->samples_per_frame >> frame_len_shift;
/** sanity check the length */
if (subframe_len < s->min_samples_per_subframe ||
subframe_len > s->samples_per_frame) {
av_log(s->avctx, AV_LOG_ERROR, "broken frame: subframe_len %i\n",
subframe_len);
return AVERROR_INVALIDDATA;
}
return subframe_len;
}
/**
*@brief Decode how the data in the frame is split into subframes.
* Every WMA frame contains the encoded data for a fixed number of
* samples per channel. The data for every channel might be split
* into several subframes. This function will reconstruct the list of
* subframes for every channel.
*
* If the subframes are not evenly split, the algorithm estimates the
* channels with the lowest number of total samples.
* Afterwards, for each of these channels a bit is read from the
* bitstream that indicates if the channel contains a subframe with the
* next subframe size that is going to be read from the bitstream or not.
* If a channel contains such a subframe, the subframe size gets added to
* the channel's subframe list.
* The algorithm repeats these steps until the frame is properly divided
* between the individual channels.
*
*@param s context
*@return 0 on success, < 0 in case of an error
*/
static int decode_tilehdr(WMAProDecodeCtx *s)
{
uint16_t num_samples[WMAPRO_MAX_CHANNELS]; /** sum of samples for all currently known subframes of a channel */
uint8_t contains_subframe[WMAPRO_MAX_CHANNELS]; /** flag indicating if a channel contains the current subframe */
int channels_for_cur_subframe = s->num_channels; /** number of channels that contain the current subframe */
int fixed_channel_layout = 0; /** flag indicating that all channels use the same subframe offsets and sizes */
int min_channel_len = 0; /** smallest sum of samples (channels with this length will be processed first) */
int c;
/* Should never consume more than 3073 bits (256 iterations for the
* while loop when always the minimum amount of 128 samples is substracted
* from missing samples in the 8 channel case).
* 1 + BLOCK_MAX_SIZE * MAX_CHANNELS / BLOCK_MIN_SIZE * (MAX_CHANNELS + 4)
*/
/** reset tiling information */
for (c = 0; c < s->num_channels; c++)
s->channel[c].num_subframes = 0;
memset(num_samples, 0, sizeof(num_samples));
if (s->max_num_subframes == 1 || get_bits1(&s->gb))
fixed_channel_layout = 1;
/** loop until the frame data is split between the subframes */
do {
int subframe_len;
/** check which channels contain the subframe */
for (c = 0; c < s->num_channels; c++) {
if (num_samples[c] == min_channel_len) {
if (fixed_channel_layout || channels_for_cur_subframe == 1 ||
(min_channel_len == s->samples_per_frame - s->min_samples_per_subframe))
contains_subframe[c] = 1;
else
contains_subframe[c] = get_bits1(&s->gb);
} else
contains_subframe[c] = 0;
}
/** get subframe length, subframe_len == 0 is not allowed */
if ((subframe_len = decode_subframe_length(s, min_channel_len)) <= 0)
return AVERROR_INVALIDDATA;
/** add subframes to the individual channels and find new min_channel_len */
min_channel_len += subframe_len;
for (c = 0; c < s->num_channels; c++) {
WMAProChannelCtx* chan = &s->channel[c];
if (contains_subframe[c]) {
if (chan->num_subframes >= MAX_SUBFRAMES) {
av_log(s->avctx, AV_LOG_ERROR,
"broken frame: num subframes > 31\n");
return AVERROR_INVALIDDATA;
}
chan->subframe_len[chan->num_subframes] = subframe_len;
num_samples[c] += subframe_len;
++chan->num_subframes;
if (num_samples[c] > s->samples_per_frame) {
av_log(s->avctx, AV_LOG_ERROR, "broken frame: "
"channel len > samples_per_frame\n");
return AVERROR_INVALIDDATA;
}
} else if (num_samples[c] <= min_channel_len) {
if (num_samples[c] < min_channel_len) {
channels_for_cur_subframe = 0;
min_channel_len = num_samples[c];
}
++channels_for_cur_subframe;
}
}
} while (min_channel_len < s->samples_per_frame);
for (c = 0; c < s->num_channels; c++) {
int i;
int offset = 0;
for (i = 0; i < s->channel[c].num_subframes; i++) {
dprintf(s->avctx, "frame[%i] channel[%i] subframe[%i]"
" len %i\n", s->frame_num, c, i,
s->channel[c].subframe_len[i]);
s->channel[c].subframe_offset[i] = offset;
offset += s->channel[c].subframe_len[i];
}
}
return 0;
}
/**
*@brief Calculate a decorrelation matrix from the bitstream parameters.
*@param s codec context
*@param chgroup channel group for which the matrix needs to be calculated
*/
static void decode_decorrelation_matrix(WMAProDecodeCtx *s,
WMAProChannelGrp *chgroup)
{
int i;
int offset = 0;
int8_t rotation_offset[WMAPRO_MAX_CHANNELS * WMAPRO_MAX_CHANNELS];
memset(chgroup->decorrelation_matrix, 0, s->num_channels *
s->num_channels * sizeof(*chgroup->decorrelation_matrix));
for (i = 0; i < chgroup->num_channels * (chgroup->num_channels - 1) >> 1; i++)
rotation_offset[i] = get_bits(&s->gb, 6);
for (i = 0; i < chgroup->num_channels; i++)
chgroup->decorrelation_matrix[chgroup->num_channels * i + i] =
get_bits1(&s->gb) ? 1.0 : -1.0;
for (i = 1; i < chgroup->num_channels; i++) {
int x;
for (x = 0; x < i; x++) {
int y;
for (y = 0; y < i + 1; y++) {
float v1 = chgroup->decorrelation_matrix[x * chgroup->num_channels + y];
float v2 = chgroup->decorrelation_matrix[i * chgroup->num_channels + y];
int n = rotation_offset[offset + x];
float sinv;
float cosv;
if (n < 32) {
sinv = sin64[n];
cosv = sin64[32 - n];
} else {
sinv = sin64[64 - n];
cosv = -sin64[n - 32];
}
chgroup->decorrelation_matrix[y + x * chgroup->num_channels] =
(v1 * sinv) - (v2 * cosv);
chgroup->decorrelation_matrix[y + i * chgroup->num_channels] =
(v1 * cosv) + (v2 * sinv);
}
}
offset += i;
}
}
/**
*@brief Decode channel transformation parameters
*@param s codec context
*@return 0 in case of success, < 0 in case of bitstream errors
*/
static int decode_channel_transform(WMAProDecodeCtx* s)
{
int i;
/* should never consume more than 1921 bits for the 8 channel case
* 1 + MAX_CHANNELS * (MAX_CHANNELS + 2 + 3 * MAX_CHANNELS * MAX_CHANNELS
* + MAX_CHANNELS + MAX_BANDS + 1)
*/
/** in the one channel case channel transforms are pointless */
s->num_chgroups = 0;
if (s->num_channels > 1) {
int remaining_channels = s->channels_for_cur_subframe;
if (get_bits1(&s->gb)) {
av_log_ask_for_sample(s->avctx,
"unsupported channel transform bit\n");
return AVERROR_INVALIDDATA;
}
for (s->num_chgroups = 0; remaining_channels &&
s->num_chgroups < s->channels_for_cur_subframe; s->num_chgroups++) {
WMAProChannelGrp* chgroup = &s->chgroup[s->num_chgroups];
float** channel_data = chgroup->channel_data;
chgroup->num_channels = 0;
chgroup->transform = 0;
/** decode channel mask */
if (remaining_channels > 2) {
for (i = 0; i < s->channels_for_cur_subframe; i++) {
int channel_idx = s->channel_indexes_for_cur_subframe[i];
if (!s->channel[channel_idx].grouped
&& get_bits1(&s->gb)) {
++chgroup->num_channels;
s->channel[channel_idx].grouped = 1;
*channel_data++ = s->channel[channel_idx].coeffs;
}
}
} else {
chgroup->num_channels = remaining_channels;
for (i = 0; i < s->channels_for_cur_subframe; i++) {
int channel_idx = s->channel_indexes_for_cur_subframe[i];
if (!s->channel[channel_idx].grouped)
*channel_data++ = s->channel[channel_idx].coeffs;
s->channel[channel_idx].grouped = 1;
}
}
/** decode transform type */
if (chgroup->num_channels == 2) {
if (get_bits1(&s->gb)) {
if (get_bits1(&s->gb)) {
av_log_ask_for_sample(s->avctx,
"unsupported channel transform type\n");
}
} else {
chgroup->transform = 1;
if (s->num_channels == 2) {
chgroup->decorrelation_matrix[0] = 1.0;
chgroup->decorrelation_matrix[1] = -1.0;
chgroup->decorrelation_matrix[2] = 1.0;
chgroup->decorrelation_matrix[3] = 1.0;
} else {
/** cos(pi/4) */
chgroup->decorrelation_matrix[0] = 0.70703125;
chgroup->decorrelation_matrix[1] = -0.70703125;
chgroup->decorrelation_matrix[2] = 0.70703125;
chgroup->decorrelation_matrix[3] = 0.70703125;
}
}
} else if (chgroup->num_channels > 2) {
if (get_bits1(&s->gb)) {
chgroup->transform = 1;
if (get_bits1(&s->gb)) {
decode_decorrelation_matrix(s, chgroup);
} else {
/** FIXME: more than 6 coupled channels not supported */
if (chgroup->num_channels > 6) {
av_log_ask_for_sample(s->avctx,
"coupled channels > 6\n");
} else {
memcpy(chgroup->decorrelation_matrix,
default_decorrelation[chgroup->num_channels],
chgroup->num_channels * chgroup->num_channels *
sizeof(*chgroup->decorrelation_matrix));
}
}
}
}
/** decode transform on / off */
if (chgroup->transform) {
if (!get_bits1(&s->gb)) {
int i;
/** transform can be enabled for individual bands */
for (i = 0; i < s->num_bands; i++) {
chgroup->transform_band[i] = get_bits1(&s->gb);
}
} else {
memset(chgroup->transform_band, 1, s->num_bands);
}
}
remaining_channels -= chgroup->num_channels;
}
}
return 0;
}
/**
*@brief Extract the coefficients from the bitstream.
*@param s codec context
*@param c current channel number
*@return 0 on success, < 0 in case of bitstream errors
*/
static int decode_coeffs(WMAProDecodeCtx *s, int c)
{
/* Integers 0..15 as single-precision floats. The table saves a
costly int to float conversion, and storing the values as
integers allows fast sign-flipping. */
static const int fval_tab[16] = {
0x00000000, 0x3f800000, 0x40000000, 0x40400000,
0x40800000, 0x40a00000, 0x40c00000, 0x40e00000,
0x41000000, 0x41100000, 0x41200000, 0x41300000,
0x41400000, 0x41500000, 0x41600000, 0x41700000,
};
int vlctable;
VLC* vlc;
WMAProChannelCtx* ci = &s->channel[c];
int rl_mode = 0;
int cur_coeff = 0;
int num_zeros = 0;
const uint16_t* run;
const float* level;
dprintf(s->avctx, "decode coefficients for channel %i\n", c);
vlctable = get_bits1(&s->gb);
vlc = &coef_vlc[vlctable];
if (vlctable) {
run = coef1_run;
level = coef1_level;
} else {
run = coef0_run;
level = coef0_level;
}
/** decode vector coefficients (consumes up to 167 bits per iteration for
4 vector coded large values) */
while (!rl_mode && cur_coeff + 3 < s->subframe_len) {
int vals[4];
int i;
unsigned int idx;
idx = get_vlc2(&s->gb, vec4_vlc.table, VLCBITS, VEC4MAXDEPTH);
if (idx == HUFF_VEC4_SIZE - 1) {
for (i = 0; i < 4; i += 2) {
idx = get_vlc2(&s->gb, vec2_vlc.table, VLCBITS, VEC2MAXDEPTH);
if (idx == HUFF_VEC2_SIZE - 1) {
int v0, v1;
v0 = get_vlc2(&s->gb, vec1_vlc.table, VLCBITS, VEC1MAXDEPTH);
if (v0 == HUFF_VEC1_SIZE - 1)
v0 += ff_wma_get_large_val(&s->gb);
v1 = get_vlc2(&s->gb, vec1_vlc.table, VLCBITS, VEC1MAXDEPTH);
if (v1 == HUFF_VEC1_SIZE - 1)
v1 += ff_wma_get_large_val(&s->gb);
((float*)vals)[i ] = v0;
((float*)vals)[i+1] = v1;
} else {
vals[i] = fval_tab[symbol_to_vec2[idx] >> 4 ];
vals[i+1] = fval_tab[symbol_to_vec2[idx] & 0xF];
}
}
} else {
vals[0] = fval_tab[ symbol_to_vec4[idx] >> 12 ];
vals[1] = fval_tab[(symbol_to_vec4[idx] >> 8) & 0xF];
vals[2] = fval_tab[(symbol_to_vec4[idx] >> 4) & 0xF];
vals[3] = fval_tab[ symbol_to_vec4[idx] & 0xF];
}
/** decode sign */
for (i = 0; i < 4; i++) {
if (vals[i]) {
int sign = get_bits1(&s->gb) - 1;
*(uint32_t*)&ci->coeffs[cur_coeff] = vals[i] ^ sign<<31;
num_zeros = 0;
} else {
ci->coeffs[cur_coeff] = 0;
/** switch to run level mode when subframe_len / 128 zeros
were found in a row */
rl_mode |= (++num_zeros > s->subframe_len >> 8);
}
++cur_coeff;
}
}
/** decode run level coded coefficients */
if (rl_mode) {
memset(&ci->coeffs[cur_coeff], 0,
sizeof(*ci->coeffs) * (s->subframe_len - cur_coeff));
if (ff_wma_run_level_decode(s->avctx, &s->gb, vlc,
level, run, 1, ci->coeffs,
cur_coeff, s->subframe_len,
s->subframe_len, s->esc_len, 0))
return AVERROR_INVALIDDATA;
}
return 0;
}
/**
*@brief Extract scale factors from the bitstream.
*@param s codec context
*@return 0 on success, < 0 in case of bitstream errors
*/
static int decode_scale_factors(WMAProDecodeCtx* s)
{
int i;
/** should never consume more than 5344 bits
* MAX_CHANNELS * (1 + MAX_BANDS * 23)
*/
for (i = 0; i < s->channels_for_cur_subframe; i++) {
int c = s->channel_indexes_for_cur_subframe[i];
int* sf;
int* sf_end;
s->channel[c].scale_factors = s->channel[c].saved_scale_factors[!s->channel[c].scale_factor_idx];
sf_end = s->channel[c].scale_factors + s->num_bands;
/** resample scale factors for the new block size
* as the scale factors might need to be resampled several times
* before some new values are transmitted, a backup of the last
* transmitted scale factors is kept in saved_scale_factors
*/
if (s->channel[c].reuse_sf) {
const int8_t* sf_offsets = s->sf_offsets[s->table_idx][s->channel[c].table_idx];
int b;
for (b = 0; b < s->num_bands; b++)
s->channel[c].scale_factors[b] =
s->channel[c].saved_scale_factors[s->channel[c].scale_factor_idx][*sf_offsets++];
}
if (!s->channel[c].cur_subframe || get_bits1(&s->gb)) {
if (!s->channel[c].reuse_sf) {
int val;
/** decode DPCM coded scale factors */
s->channel[c].scale_factor_step = get_bits(&s->gb, 2) + 1;
val = 45 / s->channel[c].scale_factor_step;
for (sf = s->channel[c].scale_factors; sf < sf_end; sf++) {
val += get_vlc2(&s->gb, sf_vlc.table, SCALEVLCBITS, SCALEMAXDEPTH) - 60;
*sf = val;
}
} else {
int i;
/** run level decode differences to the resampled factors */
for (i = 0; i < s->num_bands; i++) {
int idx;
int skip;
int val;
int sign;
idx = get_vlc2(&s->gb, sf_rl_vlc.table, VLCBITS, SCALERLMAXDEPTH);
if (!idx) {
uint32_t code = get_bits(&s->gb, 14);
val = code >> 6;
sign = (code & 1) - 1;
skip = (code & 0x3f) >> 1;
} else if (idx == 1) {
break;
} else {
skip = scale_rl_run[idx];
val = scale_rl_level[idx];
sign = get_bits1(&s->gb)-1;
}
i += skip;
if (i >= s->num_bands) {
av_log(s->avctx, AV_LOG_ERROR,
"invalid scale factor coding\n");
return AVERROR_INVALIDDATA;
}
s->channel[c].scale_factors[i] += (val ^ sign) - sign;
}
}
/** swap buffers */
s->channel[c].scale_factor_idx = !s->channel[c].scale_factor_idx;
s->channel[c].table_idx = s->table_idx;
s->channel[c].reuse_sf = 1;
}
/** calculate new scale factor maximum */
s->channel[c].max_scale_factor = s->channel[c].scale_factors[0];
for (sf = s->channel[c].scale_factors + 1; sf < sf_end; sf++) {
s->channel[c].max_scale_factor =
FFMAX(s->channel[c].max_scale_factor, *sf);
}
}
return 0;
}
/**
*@brief Reconstruct the individual channel data.
*@param s codec context
*/
static void inverse_channel_transform(WMAProDecodeCtx *s)
{
int i;
for (i = 0; i < s->num_chgroups; i++) {
if (s->chgroup[i].transform) {
float data[WMAPRO_MAX_CHANNELS];
const int num_channels = s->chgroup[i].num_channels;
float** ch_data = s->chgroup[i].channel_data;
float** ch_end = ch_data + num_channels;
const int8_t* tb = s->chgroup[i].transform_band;
int16_t* sfb;
/** multichannel decorrelation */
for (sfb = s->cur_sfb_offsets;
sfb < s->cur_sfb_offsets + s->num_bands; sfb++) {
int y;
if (*tb++ == 1) {
/** multiply values with the decorrelation_matrix */
for (y = sfb[0]; y < FFMIN(sfb[1], s->subframe_len); y++) {
const float* mat = s->chgroup[i].decorrelation_matrix;
const float* data_end = data + num_channels;
float* data_ptr = data;
float** ch;
for (ch = ch_data; ch < ch_end; ch++)
*data_ptr++ = (*ch)[y];
for (ch = ch_data; ch < ch_end; ch++) {
float sum = 0;
data_ptr = data;
while (data_ptr < data_end)
sum += *data_ptr++ * *mat++;
(*ch)[y] = sum;
}
}
} else if (s->num_channels == 2) {
int len = FFMIN(sfb[1], s->subframe_len) - sfb[0];
s->dsp.vector_fmul_scalar(ch_data[0] + sfb[0],
ch_data[0] + sfb[0],
181.0 / 128, len);
s->dsp.vector_fmul_scalar(ch_data[1] + sfb[0],
ch_data[1] + sfb[0],
181.0 / 128, len);
}
}
}
}
}
/**
*@brief Apply sine window and reconstruct the output buffer.
*@param s codec context
*/
static void wmapro_window(WMAProDecodeCtx *s)
{
int i;
for (i = 0; i < s->channels_for_cur_subframe; i++) {
int c = s->channel_indexes_for_cur_subframe[i];
float* window;
int winlen = s->channel[c].prev_block_len;
float* start = s->channel[c].coeffs - (winlen >> 1);
if (s->subframe_len < winlen) {
start += (winlen - s->subframe_len) >> 1;
winlen = s->subframe_len;
}
window = s->windows[av_log2(winlen) - BLOCK_MIN_BITS];
winlen >>= 1;
s->dsp.vector_fmul_window(start, start, start + winlen,
window, 0, winlen);
s->channel[c].prev_block_len = s->subframe_len;
}
}
/**
*@brief Decode a single subframe (block).
*@param s codec context
*@return 0 on success, < 0 when decoding failed
*/
static int decode_subframe(WMAProDecodeCtx *s)
{
int offset = s->samples_per_frame;
int subframe_len = s->samples_per_frame;
int i;
int total_samples = s->samples_per_frame * s->num_channels;
int transmit_coeffs = 0;
int cur_subwoofer_cutoff;
s->subframe_offset = get_bits_count(&s->gb);
/** reset channel context and find the next block offset and size
== the next block of the channel with the smallest number of
decoded samples
*/
for (i = 0; i < s->num_channels; i++) {
s->channel[i].grouped = 0;
if (offset > s->channel[i].decoded_samples) {
offset = s->channel[i].decoded_samples;
subframe_len =
s->channel[i].subframe_len[s->channel[i].cur_subframe];
}
}
dprintf(s->avctx,
"processing subframe with offset %i len %i\n", offset, subframe_len);
/** get a list of all channels that contain the estimated block */
s->channels_for_cur_subframe = 0;
for (i = 0; i < s->num_channels; i++) {
const int cur_subframe = s->channel[i].cur_subframe;
/** substract already processed samples */
total_samples -= s->channel[i].decoded_samples;
/** and count if there are multiple subframes that match our profile */
if (offset == s->channel[i].decoded_samples &&
subframe_len == s->channel[i].subframe_len[cur_subframe]) {
total_samples -= s->channel[i].subframe_len[cur_subframe];
s->channel[i].decoded_samples +=
s->channel[i].subframe_len[cur_subframe];
s->channel_indexes_for_cur_subframe[s->channels_for_cur_subframe] = i;
++s->channels_for_cur_subframe;
}
}
/** check if the frame will be complete after processing the
estimated block */
if (!total_samples)
s->parsed_all_subframes = 1;
dprintf(s->avctx, "subframe is part of %i channels\n",
s->channels_for_cur_subframe);
/** calculate number of scale factor bands and their offsets */
s->table_idx = av_log2(s->samples_per_frame/subframe_len);
s->num_bands = s->num_sfb[s->table_idx];
s->cur_sfb_offsets = s->sfb_offsets[s->table_idx];
cur_subwoofer_cutoff = s->subwoofer_cutoffs[s->table_idx];
/** configure the decoder for the current subframe */
for (i = 0; i < s->channels_for_cur_subframe; i++) {
int c = s->channel_indexes_for_cur_subframe[i];
s->channel[c].coeffs = &s->channel[c].out[(s->samples_per_frame >> 1)
+ offset];
}
s->subframe_len = subframe_len;
s->esc_len = av_log2(s->subframe_len - 1) + 1;
/** skip extended header if any */
if (get_bits1(&s->gb)) {
int num_fill_bits;
if (!(num_fill_bits = get_bits(&s->gb, 2))) {
int len = get_bits(&s->gb, 4);
num_fill_bits = get_bits(&s->gb, len) + 1;
}
if (num_fill_bits >= 0) {
if (get_bits_count(&s->gb) + num_fill_bits > s->num_saved_bits) {
av_log(s->avctx, AV_LOG_ERROR, "invalid number of fill bits\n");
return AVERROR_INVALIDDATA;
}
skip_bits_long(&s->gb, num_fill_bits);
}
}
/** no idea for what the following bit is used */
if (get_bits1(&s->gb)) {
av_log_ask_for_sample(s->avctx, "reserved bit set\n");
return AVERROR_INVALIDDATA;
}
if (decode_channel_transform(s) < 0)
return AVERROR_INVALIDDATA;
for (i = 0; i < s->channels_for_cur_subframe; i++) {
int c = s->channel_indexes_for_cur_subframe[i];
if ((s->channel[c].transmit_coefs = get_bits1(&s->gb)))
transmit_coeffs = 1;
}
if (transmit_coeffs) {
int step;
int quant_step = 90 * s->bits_per_sample >> 4;
if ((get_bits1(&s->gb))) {
/** FIXME: might change run level mode decision */
av_log_ask_for_sample(s->avctx, "unsupported quant step coding\n");
return AVERROR_INVALIDDATA;
}
/** decode quantization step */
step = get_sbits(&s->gb, 6);
quant_step += step;
if (step == -32 || step == 31) {
const int sign = (step == 31) - 1;
int quant = 0;
while (get_bits_count(&s->gb) + 5 < s->num_saved_bits &&
(step = get_bits(&s->gb, 5)) == 31) {
quant += 31;
}
quant_step += ((quant + step) ^ sign) - sign;
}
if (quant_step < 0) {
av_log(s->avctx, AV_LOG_DEBUG, "negative quant step\n");
}
/** decode quantization step modifiers for every channel */
if (s->channels_for_cur_subframe == 1) {
s->channel[s->channel_indexes_for_cur_subframe[0]].quant_step = quant_step;
} else {
int modifier_len = get_bits(&s->gb, 3);
for (i = 0; i < s->channels_for_cur_subframe; i++) {
int c = s->channel_indexes_for_cur_subframe[i];
s->channel[c].quant_step = quant_step;
if (get_bits1(&s->gb)) {
if (modifier_len) {
s->channel[c].quant_step += get_bits(&s->gb, modifier_len) + 1;
} else
++s->channel[c].quant_step;
}
}
}
/** decode scale factors */
if (decode_scale_factors(s) < 0)
return AVERROR_INVALIDDATA;
}
dprintf(s->avctx, "BITSTREAM: subframe header length was %i\n",
get_bits_count(&s->gb) - s->subframe_offset);
/** parse coefficients */
for (i = 0; i < s->channels_for_cur_subframe; i++) {
int c = s->channel_indexes_for_cur_subframe[i];
if (s->channel[c].transmit_coefs &&
get_bits_count(&s->gb) < s->num_saved_bits) {
decode_coeffs(s, c);
} else
memset(s->channel[c].coeffs, 0,
sizeof(*s->channel[c].coeffs) * subframe_len);
}
dprintf(s->avctx, "BITSTREAM: subframe length was %i\n",
get_bits_count(&s->gb) - s->subframe_offset);
if (transmit_coeffs) {
/** reconstruct the per channel data */
inverse_channel_transform(s);
for (i = 0; i < s->channels_for_cur_subframe; i++) {
int c = s->channel_indexes_for_cur_subframe[i];
const int* sf = s->channel[c].scale_factors;
int b;
if (c == s->lfe_channel)
memset(&s->tmp[cur_subwoofer_cutoff], 0, sizeof(*s->tmp) *
(subframe_len - cur_subwoofer_cutoff));
/** inverse quantization and rescaling */
for (b = 0; b < s->num_bands; b++) {
const int end = FFMIN(s->cur_sfb_offsets[b+1], s->subframe_len);
const int exp = s->channel[c].quant_step -
(s->channel[c].max_scale_factor - *sf++) *
s->channel[c].scale_factor_step;
const float quant = pow(10.0, exp / 20.0);
int start = s->cur_sfb_offsets[b];
s->dsp.vector_fmul_scalar(s->tmp + start,
s->channel[c].coeffs + start,
quant, end - start);
}
/** apply imdct (ff_imdct_half == DCTIV with reverse) */
ff_imdct_half(&s->mdct_ctx[av_log2(subframe_len) - BLOCK_MIN_BITS],
s->channel[c].coeffs, s->tmp);
}
}
/** window and overlapp-add */
wmapro_window(s);
/** handled one subframe */
for (i = 0; i < s->channels_for_cur_subframe; i++) {
int c = s->channel_indexes_for_cur_subframe[i];
if (s->channel[c].cur_subframe >= s->channel[c].num_subframes) {
av_log(s->avctx, AV_LOG_ERROR, "broken subframe\n");
return AVERROR_INVALIDDATA;
}
++s->channel[c].cur_subframe;
}
return 0;
}
/**
*@brief Decode one WMA frame.
*@param s codec context
*@return 0 if the trailer bit indicates that this is the last frame,
* 1 if there are additional frames
*/
static int decode_frame(WMAProDecodeCtx *s)
{
GetBitContext* gb = &s->gb;
int more_frames = 0;
int len = 0;
int i;
/** check for potential output buffer overflow */
if (s->num_channels * s->samples_per_frame > s->samples_end - s->samples) {
/** return an error if no frame could be decoded at all */
av_log(s->avctx, AV_LOG_ERROR,
"not enough space for the output samples\n");
s->packet_loss = 1;
return 0;
}
/** get frame length */
if (s->len_prefix)
len = get_bits(gb, s->log2_frame_size);
dprintf(s->avctx, "decoding frame with length %x\n", len);
/** decode tile information */
if (decode_tilehdr(s)) {
s->packet_loss = 1;
return 0;
}
/** read postproc transform */
if (s->num_channels > 1 && get_bits1(gb)) {
av_log_ask_for_sample(s->avctx, "Unsupported postproc transform found\n");
s->packet_loss = 1;
return 0;
}
/** read drc info */
if (s->dynamic_range_compression) {
s->drc_gain = get_bits(gb, 8);
dprintf(s->avctx, "drc_gain %i\n", s->drc_gain);
}
/** no idea what these are for, might be the number of samples
that need to be skipped at the beginning or end of a stream */
if (get_bits1(gb)) {
int skip;
/** usually true for the first frame */
if (get_bits1(gb)) {
skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
dprintf(s->avctx, "start skip: %i\n", skip);
}
/** sometimes true for the last frame */
if (get_bits1(gb)) {
skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
dprintf(s->avctx, "end skip: %i\n", skip);
}
}
dprintf(s->avctx, "BITSTREAM: frame header length was %i\n",
get_bits_count(gb) - s->frame_offset);
/** reset subframe states */
s->parsed_all_subframes = 0;
for (i = 0; i < s->num_channels; i++) {
s->channel[i].decoded_samples = 0;
s->channel[i].cur_subframe = 0;
s->channel[i].reuse_sf = 0;
}
/** decode all subframes */
while (!s->parsed_all_subframes) {
if (decode_subframe(s) < 0) {
s->packet_loss = 1;
return 0;
}
}
/** interleave samples and write them to the output buffer */
for (i = 0; i < s->num_channels; i++) {
float* ptr = s->samples + i;
int incr = s->num_channels;
float* iptr = s->channel[i].out;
float* iend = iptr + s->samples_per_frame;
// FIXME should create/use a DSP function here
while (iptr < iend) {
*ptr = *iptr++;
ptr += incr;
}
/** reuse second half of the IMDCT output for the next frame */
memcpy(&s->channel[i].out[0],
&s->channel[i].out[s->samples_per_frame],
s->samples_per_frame * sizeof(*s->channel[i].out) >> 1);
}
if (s->skip_frame) {
s->skip_frame = 0;
} else
s->samples += s->num_channels * s->samples_per_frame;
if (len != (get_bits_count(gb) - s->frame_offset) + 2) {
/** FIXME: not sure if this is always an error */
av_log(s->avctx, AV_LOG_ERROR, "frame[%i] would have to skip %i bits\n",
s->frame_num, len - (get_bits_count(gb) - s->frame_offset) - 1);
s->packet_loss = 1;
return 0;
}
/** skip the rest of the frame data */
skip_bits_long(gb, len - (get_bits_count(gb) - s->frame_offset) - 1);
/** decode trailer bit */
more_frames = get_bits1(gb);
++s->frame_num;
return more_frames;
}
/**
*@brief Calculate remaining input buffer length.
*@param s codec context
*@param gb bitstream reader context
*@return remaining size in bits
*/
static int remaining_bits(WMAProDecodeCtx *s, GetBitContext *gb)
{
return s->buf_bit_size - get_bits_count(gb);
}
/**
*@brief Fill the bit reservoir with a (partial) frame.
*@param s codec context
*@param gb bitstream reader context
*@param len length of the partial frame
*@param append decides wether to reset the buffer or not
*/
static void save_bits(WMAProDecodeCtx *s, GetBitContext* gb, int len,
int append)
{
int buflen;
/** when the frame data does not need to be concatenated, the input buffer
is resetted and additional bits from the previous frame are copyed
and skipped later so that a fast byte copy is possible */
if (!append) {
s->frame_offset = get_bits_count(gb) & 7;
s->num_saved_bits = s->frame_offset;
init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
}
buflen = (s->num_saved_bits + len + 8) >> 3;
if (len <= 0 || buflen > MAX_FRAMESIZE) {
av_log_ask_for_sample(s->avctx, "input buffer too small\n");
s->packet_loss = 1;
return;
}
s->num_saved_bits += len;
if (!append) {
ff_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3),
s->num_saved_bits);
} else {
int align = 8 - (get_bits_count(gb) & 7);
align = FFMIN(align, len);
put_bits(&s->pb, align, get_bits(gb, align));
len -= align;
ff_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3), len);
}
skip_bits_long(gb, len);
{
PutBitContext tmp = s->pb;
flush_put_bits(&tmp);
}
init_get_bits(&s->gb, s->frame_data, s->num_saved_bits);
skip_bits(&s->gb, s->frame_offset);
}
/**
*@brief Decode a single WMA packet.
*@param avctx codec context
*@param data the output buffer
*@param data_size number of bytes that were written to the output buffer
*@param avpkt input packet
*@return number of bytes that were read from the input buffer
*/
static int decode_packet(AVCodecContext *avctx,
void *data, int *data_size, AVPacket* avpkt)
{
WMAProDecodeCtx *s = avctx->priv_data;
GetBitContext* gb = &s->pgb;
const uint8_t* buf = avpkt->data;
int buf_size = avpkt->size;
int num_bits_prev_frame;
int packet_sequence_number;
s->samples = data;
s->samples_end = (float*)((int8_t*)data + *data_size);
*data_size = 0;
if (s->packet_done || s->packet_loss) {
s->packet_done = 0;
s->buf_bit_size = buf_size << 3;
/** sanity check for the buffer length */
if (buf_size < avctx->block_align)
return 0;
buf_size = avctx->block_align;
/** parse packet header */
init_get_bits(gb, buf, s->buf_bit_size);
packet_sequence_number = get_bits(gb, 4);
skip_bits(gb, 2);
/** get number of bits that need to be added to the previous frame */
num_bits_prev_frame = get_bits(gb, s->log2_frame_size);
dprintf(avctx, "packet[%d]: nbpf %x\n", avctx->frame_number,
num_bits_prev_frame);
/** check for packet loss */
if (!s->packet_loss &&
((s->packet_sequence_number + 1) & 0xF) != packet_sequence_number) {
s->packet_loss = 1;
av_log(avctx, AV_LOG_ERROR, "Packet loss detected! seq %x vs %x\n",
s->packet_sequence_number, packet_sequence_number);
}
s->packet_sequence_number = packet_sequence_number;
if (num_bits_prev_frame > 0) {
/** append the previous frame data to the remaining data from the
previous packet to create a full frame */
save_bits(s, gb, num_bits_prev_frame, 1);
dprintf(avctx, "accumulated %x bits of frame data\n",
s->num_saved_bits - s->frame_offset);
/** decode the cross packet frame if it is valid */
if (!s->packet_loss)
decode_frame(s);
} else if (s->num_saved_bits - s->frame_offset) {
dprintf(avctx, "ignoring %x previously saved bits\n",
s->num_saved_bits - s->frame_offset);
}
s->packet_loss = 0;
} else {
int frame_size;
s->buf_bit_size = avpkt->size << 3;
init_get_bits(gb, avpkt->data, s->buf_bit_size);
skip_bits(gb, s->packet_offset);
if (remaining_bits(s, gb) > s->log2_frame_size &&
(frame_size = show_bits(gb, s->log2_frame_size)) &&
frame_size <= remaining_bits(s, gb)) {
save_bits(s, gb, frame_size, 0);
s->packet_done = !decode_frame(s);
} else
s->packet_done = 1;
}
if (s->packet_done && !s->packet_loss &&
remaining_bits(s, gb) > 0) {
/** save the rest of the data so that it can be decoded
with the next packet */
save_bits(s, gb, remaining_bits(s, gb), 0);
}
*data_size = (int8_t *)s->samples - (int8_t *)data;
s->packet_offset = get_bits_count(gb) & 7;
return (s->packet_loss) ? AVERROR_INVALIDDATA : get_bits_count(gb) >> 3;
}
/**
*@brief Clear decoder buffers (for seeking).
*@param avctx codec context
*/
static void flush(AVCodecContext *avctx)
{
WMAProDecodeCtx *s = avctx->priv_data;
int i;
/** reset output buffer as a part of it is used during the windowing of a
new frame */
for (i = 0; i < s->num_channels; i++)
memset(s->channel[i].out, 0, s->samples_per_frame *
sizeof(*s->channel[i].out));
s->packet_loss = 1;
}
/**
*@brief wmapro decoder
*/
AVCodec wmapro_decoder = {
"wmapro",
AVMEDIA_TYPE_AUDIO,
CODEC_ID_WMAPRO,
sizeof(WMAProDecodeCtx),
decode_init,
NULL,
decode_end,
decode_packet,
.capabilities = CODEC_CAP_SUBFRAMES,
.flush= flush,
.long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 9 Professional"),
};
| 123linslouis-android-video-cutter | jni/libavcodec/wmaprodec.c | C | asf20 | 61,902 |
/*
* Floating point AAN IDCT
* Copyright (c) 2008 Michael Niedermayer <michaelni@gmx.at>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "faanidct.h"
/* To allow switching to double. */
#define FLOAT float
#define B0 1.0000000000000000000000
#define B1 1.3870398453221474618216 // cos(pi*1/16)sqrt(2)
#define B2 1.3065629648763765278566 // cos(pi*2/16)sqrt(2)
#define B3 1.1758756024193587169745 // cos(pi*3/16)sqrt(2)
#define B4 1.0000000000000000000000 // cos(pi*4/16)sqrt(2)
#define B5 0.7856949583871021812779 // cos(pi*5/16)sqrt(2)
#define B6 0.5411961001461969843997 // cos(pi*6/16)sqrt(2)
#define B7 0.2758993792829430123360 // cos(pi*7/16)sqrt(2)
#define A4 0.70710678118654752438 // cos(pi*4/16)
#define A2 0.92387953251128675613 // cos(pi*2/16)
static const FLOAT prescale[64]={
B0*B0/8, B0*B1/8, B0*B2/8, B0*B3/8, B0*B4/8, B0*B5/8, B0*B6/8, B0*B7/8,
B1*B0/8, B1*B1/8, B1*B2/8, B1*B3/8, B1*B4/8, B1*B5/8, B1*B6/8, B1*B7/8,
B2*B0/8, B2*B1/8, B2*B2/8, B2*B3/8, B2*B4/8, B2*B5/8, B2*B6/8, B2*B7/8,
B3*B0/8, B3*B1/8, B3*B2/8, B3*B3/8, B3*B4/8, B3*B5/8, B3*B6/8, B3*B7/8,
B4*B0/8, B4*B1/8, B4*B2/8, B4*B3/8, B4*B4/8, B4*B5/8, B4*B6/8, B4*B7/8,
B5*B0/8, B5*B1/8, B5*B2/8, B5*B3/8, B5*B4/8, B5*B5/8, B5*B6/8, B5*B7/8,
B6*B0/8, B6*B1/8, B6*B2/8, B6*B3/8, B6*B4/8, B6*B5/8, B6*B6/8, B6*B7/8,
B7*B0/8, B7*B1/8, B7*B2/8, B7*B3/8, B7*B4/8, B7*B5/8, B7*B6/8, B7*B7/8,
};
static inline void p8idct(DCTELEM data[64], FLOAT temp[64], uint8_t *dest, int stride, int x, int y, int type){
int i;
FLOAT av_unused tmp0;
FLOAT s04, d04, s17, d17, s26, d26, s53, d53;
FLOAT os07, os16, os25, os34;
FLOAT od07, od16, od25, od34;
for(i=0; i<y*8; i+=y){
s17= temp[1*x + i] + temp[7*x + i];
d17= temp[1*x + i] - temp[7*x + i];
s53= temp[5*x + i] + temp[3*x + i];
d53= temp[5*x + i] - temp[3*x + i];
od07= s17 + s53;
od25= (s17 - s53)*(2*A4);
#if 0 //these 2 are equivalent
tmp0= (d17 + d53)*(2*A2);
od34= d17*( 2*B6) - tmp0;
od16= d53*(-2*B2) + tmp0;
#else
od34= d17*(2*(B6-A2)) - d53*(2*A2);
od16= d53*(2*(A2-B2)) + d17*(2*A2);
#endif
od16 -= od07;
od25 -= od16;
od34 += od25;
s26 = temp[2*x + i] + temp[6*x + i];
d26 = temp[2*x + i] - temp[6*x + i];
d26*= 2*A4;
d26-= s26;
s04= temp[0*x + i] + temp[4*x + i];
d04= temp[0*x + i] - temp[4*x + i];
os07= s04 + s26;
os34= s04 - s26;
os16= d04 + d26;
os25= d04 - d26;
if(type==0){
temp[0*x + i]= os07 + od07;
temp[7*x + i]= os07 - od07;
temp[1*x + i]= os16 + od16;
temp[6*x + i]= os16 - od16;
temp[2*x + i]= os25 + od25;
temp[5*x + i]= os25 - od25;
temp[3*x + i]= os34 - od34;
temp[4*x + i]= os34 + od34;
}else if(type==1){
data[0*x + i]= lrintf(os07 + od07);
data[7*x + i]= lrintf(os07 - od07);
data[1*x + i]= lrintf(os16 + od16);
data[6*x + i]= lrintf(os16 - od16);
data[2*x + i]= lrintf(os25 + od25);
data[5*x + i]= lrintf(os25 - od25);
data[3*x + i]= lrintf(os34 - od34);
data[4*x + i]= lrintf(os34 + od34);
}else if(type==2){
dest[0*stride + i]= av_clip_uint8(((int)dest[0*stride + i]) + lrintf(os07 + od07));
dest[7*stride + i]= av_clip_uint8(((int)dest[7*stride + i]) + lrintf(os07 - od07));
dest[1*stride + i]= av_clip_uint8(((int)dest[1*stride + i]) + lrintf(os16 + od16));
dest[6*stride + i]= av_clip_uint8(((int)dest[6*stride + i]) + lrintf(os16 - od16));
dest[2*stride + i]= av_clip_uint8(((int)dest[2*stride + i]) + lrintf(os25 + od25));
dest[5*stride + i]= av_clip_uint8(((int)dest[5*stride + i]) + lrintf(os25 - od25));
dest[3*stride + i]= av_clip_uint8(((int)dest[3*stride + i]) + lrintf(os34 - od34));
dest[4*stride + i]= av_clip_uint8(((int)dest[4*stride + i]) + lrintf(os34 + od34));
}else{
dest[0*stride + i]= av_clip_uint8(lrintf(os07 + od07));
dest[7*stride + i]= av_clip_uint8(lrintf(os07 - od07));
dest[1*stride + i]= av_clip_uint8(lrintf(os16 + od16));
dest[6*stride + i]= av_clip_uint8(lrintf(os16 - od16));
dest[2*stride + i]= av_clip_uint8(lrintf(os25 + od25));
dest[5*stride + i]= av_clip_uint8(lrintf(os25 - od25));
dest[3*stride + i]= av_clip_uint8(lrintf(os34 - od34));
dest[4*stride + i]= av_clip_uint8(lrintf(os34 + od34));
}
}
}
void ff_faanidct(DCTELEM block[64]){
FLOAT temp[64];
int i;
emms_c();
for(i=0; i<64; i++)
temp[i] = block[i] * prescale[i];
p8idct(block, temp, NULL, 0, 1, 8, 0);
p8idct(block, temp, NULL, 0, 8, 1, 1);
}
void ff_faanidct_add(uint8_t *dest, int line_size, DCTELEM block[64]){
FLOAT temp[64];
int i;
emms_c();
for(i=0; i<64; i++)
temp[i] = block[i] * prescale[i];
p8idct(block, temp, NULL, 0, 1, 8, 0);
p8idct(NULL , temp, dest, line_size, 8, 1, 2);
}
void ff_faanidct_put(uint8_t *dest, int line_size, DCTELEM block[64]){
FLOAT temp[64];
int i;
emms_c();
for(i=0; i<64; i++)
temp[i] = block[i] * prescale[i];
p8idct(block, temp, NULL, 0, 1, 8, 0);
p8idct(NULL , temp, dest, line_size, 8, 1, 3);
}
| 123linslouis-android-video-cutter | jni/libavcodec/faanidct.c | C | asf20 | 6,202 |
/*
* Atrac common functions
* Copyright (c) 2006-2008 Maxim Poliakovski
* Copyright (c) 2006-2008 Benjamin Larsson
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
*/
#include <math.h>
#include <stddef.h>
#include <stdio.h>
#include "avcodec.h"
#include "dsputil.h"
#include "atrac.h"
float sf_table[64];
float qmf_window[48];
static const float qmf_48tap_half[24] = {
-0.00001461907, -0.00009205479,-0.000056157569,0.00030117269,
0.0002422519, -0.00085293897,-0.0005205574, 0.0020340169,
0.00078333891, -0.0042153862, -0.00075614988, 0.0078402944,
-0.000061169922,-0.01344162, 0.0024626821, 0.021736089,
-0.007801671, -0.034090221, 0.01880949, 0.054326009,
-0.043596379, -0.099384367, 0.13207909, 0.46424159
};
/**
* Generate common tables
*/
void atrac_generate_tables(void)
{
int i;
float s;
/* Generate scale factors */
if (!sf_table[63])
for (i=0 ; i<64 ; i++)
sf_table[i] = pow(2.0, (i - 15) / 3.0);
/* Generate the QMF window. */
if (!qmf_window[47])
for (i=0 ; i<24; i++) {
s = qmf_48tap_half[i] * 2.0;
qmf_window[i] = qmf_window[47 - i] = s;
}
}
/**
* Quadrature mirror synthesis filter.
*
* @param inlo lower part of spectrum
* @param inhi higher part of spectrum
* @param nIn size of spectrum buffer
* @param pOut out buffer
* @param delayBuf delayBuf buffer
* @param temp temp buffer
*/
void atrac_iqmf (float *inlo, float *inhi, unsigned int nIn, float *pOut, float *delayBuf, float *temp)
{
int i, j;
float *p1, *p3;
memcpy(temp, delayBuf, 46*sizeof(float));
p3 = temp + 46;
/* loop1 */
for(i=0; i<nIn; i+=2){
p3[2*i+0] = inlo[i ] + inhi[i ];
p3[2*i+1] = inlo[i ] - inhi[i ];
p3[2*i+2] = inlo[i+1] + inhi[i+1];
p3[2*i+3] = inlo[i+1] - inhi[i+1];
}
/* loop2 */
p1 = temp;
for (j = nIn; j != 0; j--) {
float s1 = 0.0;
float s2 = 0.0;
for (i = 0; i < 48; i += 2) {
s1 += p1[i] * qmf_window[i];
s2 += p1[i+1] * qmf_window[i+1];
}
pOut[0] = s2;
pOut[1] = s1;
p1 += 2;
pOut += 2;
}
/* Update the delay buffer. */
memcpy(delayBuf, temp + nIn*2, 46*sizeof(float));
}
| 123linslouis-android-video-cutter | jni/libavcodec/atrac.c | C | asf20 | 3,068 |
/*
* H.263i decoder
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "mpegvideo.h"
#include "h263.h"
/* don't understand why they choose a different header ! */
int ff_intel_h263_decode_picture_header(MpegEncContext *s)
{
int format;
/* picture header */
if (get_bits_long(&s->gb, 22) != 0x20) {
av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
return -1;
}
s->picture_number = get_bits(&s->gb, 8); /* picture timestamp */
if (get_bits1(&s->gb) != 1) {
av_log(s->avctx, AV_LOG_ERROR, "Bad marker\n");
return -1; /* marker */
}
if (get_bits1(&s->gb) != 0) {
av_log(s->avctx, AV_LOG_ERROR, "Bad H263 id\n");
return -1; /* h263 id */
}
skip_bits1(&s->gb); /* split screen off */
skip_bits1(&s->gb); /* camera off */
skip_bits1(&s->gb); /* freeze picture release off */
format = get_bits(&s->gb, 3);
if (format != 7) {
av_log(s->avctx, AV_LOG_ERROR, "Intel H263 free format not supported\n");
return -1;
}
s->h263_plus = 0;
s->pict_type = FF_I_TYPE + get_bits1(&s->gb);
s->unrestricted_mv = get_bits1(&s->gb);
s->h263_long_vectors = s->unrestricted_mv;
if (get_bits1(&s->gb) != 0) {
av_log(s->avctx, AV_LOG_ERROR, "SAC not supported\n");
return -1; /* SAC: off */
}
s->obmc= get_bits1(&s->gb);
s->pb_frame = get_bits1(&s->gb);
if(format == 7){
format = get_bits(&s->gb, 3);
if(format == 0 || format == 7){
av_log(s->avctx, AV_LOG_ERROR, "Wrong Intel H263 format\n");
return -1;
}
if(get_bits(&s->gb, 2))
av_log(s->avctx, AV_LOG_ERROR, "Bad value for reserved field\n");
s->loop_filter = get_bits1(&s->gb);
if(get_bits1(&s->gb))
av_log(s->avctx, AV_LOG_ERROR, "Bad value for reserved field\n");
if(get_bits1(&s->gb))
s->pb_frame = 2;
if(get_bits(&s->gb, 5))
av_log(s->avctx, AV_LOG_ERROR, "Bad value for reserved field\n");
if(get_bits(&s->gb, 5) != 1)
av_log(s->avctx, AV_LOG_ERROR, "Invalid marker\n");
}
if(format == 6){
int ar = get_bits(&s->gb, 4);
skip_bits(&s->gb, 9); // display width
skip_bits1(&s->gb);
skip_bits(&s->gb, 9); // display height
if(ar == 15){
skip_bits(&s->gb, 8); // aspect ratio - width
skip_bits(&s->gb, 8); // aspect ratio - height
}
}
s->chroma_qscale= s->qscale = get_bits(&s->gb, 5);
skip_bits1(&s->gb); /* Continuous Presence Multipoint mode: off */
if(s->pb_frame){
skip_bits(&s->gb, 3); //temporal reference for B-frame
skip_bits(&s->gb, 2); //dbquant
}
/* PEI */
while (get_bits1(&s->gb) != 0) {
skip_bits(&s->gb, 8);
}
s->f_code = 1;
s->y_dc_scale_table=
s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
ff_h263_show_pict_info(s);
return 0;
}
AVCodec h263i_decoder = {
"h263i",
AVMEDIA_TYPE_VIDEO,
CODEC_ID_H263I,
sizeof(MpegEncContext),
ff_h263_decode_init,
NULL,
ff_h263_decode_end,
ff_h263_decode_frame,
CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
.long_name = NULL_IF_CONFIG_SMALL("Intel H.263"),
.pix_fmts= ff_pixfmt_list_420,
};
| 123linslouis-android-video-cutter | jni/libavcodec/intelh263dec.c | C | asf20 | 4,086 |
/*
* Error resilience / concealment
*
* Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* Error resilience / concealment.
*/
#include <limits.h>
#include "avcodec.h"
#include "dsputil.h"
#include "mpegvideo.h"
#include "h264.h"
#include "rectangle.h"
/*
* H264 redefines mb_intra so it is not mistakely used (its uninitialized in h264)
* but error concealment must support both h264 and h263 thus we must undo this
*/
#undef mb_intra
static void decode_mb(MpegEncContext *s, int ref){
s->dest[0] = s->current_picture.data[0] + (s->mb_y * 16* s->linesize ) + s->mb_x * 16;
s->dest[1] = s->current_picture.data[1] + (s->mb_y * (16>>s->chroma_y_shift) * s->uvlinesize) + s->mb_x * (16>>s->chroma_x_shift);
s->dest[2] = s->current_picture.data[2] + (s->mb_y * (16>>s->chroma_y_shift) * s->uvlinesize) + s->mb_x * (16>>s->chroma_x_shift);
if(CONFIG_H264_DECODER && s->codec_id == CODEC_ID_H264){
H264Context *h= (void*)s;
h->mb_xy= s->mb_x + s->mb_y*s->mb_stride;
memset(h->non_zero_count_cache, 0, sizeof(h->non_zero_count_cache));
assert(ref>=0);
if(ref >= h->ref_count[0]) //FIXME it is posible albeit uncommon that slice references differ between slices, we take the easy approuch and ignore it for now. If this turns out to have any relevance in practice then correct remapping should be added
ref=0;
fill_rectangle(&s->current_picture.ref_index[0][4*h->mb_xy], 2, 2, 2, ref, 1);
fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
fill_rectangle(h->mv_cache[0][ scan8[0] ], 4, 4, 8, pack16to32(s->mv[0][0][0],s->mv[0][0][1]), 4);
assert(!FRAME_MBAFF);
ff_h264_hl_decode_mb(h);
}else{
assert(ref==0);
MPV_decode_mb(s, s->block);
}
}
/**
* @param stride the number of MVs to get to the next row
* @param mv_step the number of MVs per row or column in a macroblock
*/
static void set_mv_strides(MpegEncContext *s, int *mv_step, int *stride){
if(s->codec_id == CODEC_ID_H264){
H264Context *h= (void*)s;
assert(s->quarter_sample);
*mv_step= 4;
*stride= h->b_stride;
}else{
*mv_step= 2;
*stride= s->b8_stride;
}
}
/**
* replaces the current MB with a flat dc only version.
*/
static void put_dc(MpegEncContext *s, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, int mb_x, int mb_y)
{
int dc, dcu, dcv, y, i;
for(i=0; i<4; i++){
dc= s->dc_val[0][mb_x*2 + (i&1) + (mb_y*2 + (i>>1))*s->b8_stride];
if(dc<0) dc=0;
else if(dc>2040) dc=2040;
for(y=0; y<8; y++){
int x;
for(x=0; x<8; x++){
dest_y[x + (i&1)*8 + (y + (i>>1)*8)*s->linesize]= dc/8;
}
}
}
dcu = s->dc_val[1][mb_x + mb_y*s->mb_stride];
dcv = s->dc_val[2][mb_x + mb_y*s->mb_stride];
if (dcu<0 ) dcu=0;
else if(dcu>2040) dcu=2040;
if (dcv<0 ) dcv=0;
else if(dcv>2040) dcv=2040;
for(y=0; y<8; y++){
int x;
for(x=0; x<8; x++){
dest_cb[x + y*(s->uvlinesize)]= dcu/8;
dest_cr[x + y*(s->uvlinesize)]= dcv/8;
}
}
}
static void filter181(int16_t *data, int width, int height, int stride){
int x,y;
/* horizontal filter */
for(y=1; y<height-1; y++){
int prev_dc= data[0 + y*stride];
for(x=1; x<width-1; x++){
int dc;
dc= - prev_dc
+ data[x + y*stride]*8
- data[x + 1 + y*stride];
dc= (dc*10923 + 32768)>>16;
prev_dc= data[x + y*stride];
data[x + y*stride]= dc;
}
}
/* vertical filter */
for(x=1; x<width-1; x++){
int prev_dc= data[x];
for(y=1; y<height-1; y++){
int dc;
dc= - prev_dc
+ data[x + y *stride]*8
- data[x + (y+1)*stride];
dc= (dc*10923 + 32768)>>16;
prev_dc= data[x + y*stride];
data[x + y*stride]= dc;
}
}
}
/**
* guess the dc of blocks which do not have an undamaged dc
* @param w width in 8 pixel blocks
* @param h height in 8 pixel blocks
*/
static void guess_dc(MpegEncContext *s, int16_t *dc, int w, int h, int stride, int is_luma){
int b_x, b_y;
for(b_y=0; b_y<h; b_y++){
for(b_x=0; b_x<w; b_x++){
int color[4]={1024,1024,1024,1024};
int distance[4]={9999,9999,9999,9999};
int mb_index, error, j;
int64_t guess, weight_sum;
mb_index= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
error= s->error_status_table[mb_index];
if(IS_INTER(s->current_picture.mb_type[mb_index])) continue; //inter
if(!(error&DC_ERROR)) continue; //dc-ok
/* right block */
for(j=b_x+1; j<w; j++){
int mb_index_j= (j>>is_luma) + (b_y>>is_luma)*s->mb_stride;
int error_j= s->error_status_table[mb_index_j];
int intra_j= IS_INTRA(s->current_picture.mb_type[mb_index_j]);
if(intra_j==0 || !(error_j&DC_ERROR)){
color[0]= dc[j + b_y*stride];
distance[0]= j-b_x;
break;
}
}
/* left block */
for(j=b_x-1; j>=0; j--){
int mb_index_j= (j>>is_luma) + (b_y>>is_luma)*s->mb_stride;
int error_j= s->error_status_table[mb_index_j];
int intra_j= IS_INTRA(s->current_picture.mb_type[mb_index_j]);
if(intra_j==0 || !(error_j&DC_ERROR)){
color[1]= dc[j + b_y*stride];
distance[1]= b_x-j;
break;
}
}
/* bottom block */
for(j=b_y+1; j<h; j++){
int mb_index_j= (b_x>>is_luma) + (j>>is_luma)*s->mb_stride;
int error_j= s->error_status_table[mb_index_j];
int intra_j= IS_INTRA(s->current_picture.mb_type[mb_index_j]);
if(intra_j==0 || !(error_j&DC_ERROR)){
color[2]= dc[b_x + j*stride];
distance[2]= j-b_y;
break;
}
}
/* top block */
for(j=b_y-1; j>=0; j--){
int mb_index_j= (b_x>>is_luma) + (j>>is_luma)*s->mb_stride;
int error_j= s->error_status_table[mb_index_j];
int intra_j= IS_INTRA(s->current_picture.mb_type[mb_index_j]);
if(intra_j==0 || !(error_j&DC_ERROR)){
color[3]= dc[b_x + j*stride];
distance[3]= b_y-j;
break;
}
}
weight_sum=0;
guess=0;
for(j=0; j<4; j++){
int64_t weight= 256*256*256*16/distance[j];
guess+= weight*(int64_t)color[j];
weight_sum+= weight;
}
guess= (guess + weight_sum/2) / weight_sum;
dc[b_x + b_y*stride]= guess;
}
}
}
/**
* simple horizontal deblocking filter used for error resilience
* @param w width in 8 pixel blocks
* @param h height in 8 pixel blocks
*/
static void h_block_filter(MpegEncContext *s, uint8_t *dst, int w, int h, int stride, int is_luma){
int b_x, b_y, mvx_stride, mvy_stride;
uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
set_mv_strides(s, &mvx_stride, &mvy_stride);
mvx_stride >>= is_luma;
mvy_stride *= mvx_stride;
for(b_y=0; b_y<h; b_y++){
for(b_x=0; b_x<w-1; b_x++){
int y;
int left_status = s->error_status_table[( b_x >>is_luma) + (b_y>>is_luma)*s->mb_stride];
int right_status= s->error_status_table[((b_x+1)>>is_luma) + (b_y>>is_luma)*s->mb_stride];
int left_intra= IS_INTRA(s->current_picture.mb_type [( b_x >>is_luma) + (b_y>>is_luma)*s->mb_stride]);
int right_intra= IS_INTRA(s->current_picture.mb_type [((b_x+1)>>is_luma) + (b_y>>is_luma)*s->mb_stride]);
int left_damage = left_status&(DC_ERROR|AC_ERROR|MV_ERROR);
int right_damage= right_status&(DC_ERROR|AC_ERROR|MV_ERROR);
int offset= b_x*8 + b_y*stride*8;
int16_t *left_mv= s->current_picture.motion_val[0][mvy_stride*b_y + mvx_stride* b_x ];
int16_t *right_mv= s->current_picture.motion_val[0][mvy_stride*b_y + mvx_stride*(b_x+1)];
if(!(left_damage||right_damage)) continue; // both undamaged
if( (!left_intra) && (!right_intra)
&& FFABS(left_mv[0]-right_mv[0]) + FFABS(left_mv[1]+right_mv[1]) < 2) continue;
for(y=0; y<8; y++){
int a,b,c,d;
a= dst[offset + 7 + y*stride] - dst[offset + 6 + y*stride];
b= dst[offset + 8 + y*stride] - dst[offset + 7 + y*stride];
c= dst[offset + 9 + y*stride] - dst[offset + 8 + y*stride];
d= FFABS(b) - ((FFABS(a) + FFABS(c) + 1)>>1);
d= FFMAX(d, 0);
if(b<0) d= -d;
if(d==0) continue;
if(!(left_damage && right_damage))
d= d*16/9;
if(left_damage){
dst[offset + 7 + y*stride] = cm[dst[offset + 7 + y*stride] + ((d*7)>>4)];
dst[offset + 6 + y*stride] = cm[dst[offset + 6 + y*stride] + ((d*5)>>4)];
dst[offset + 5 + y*stride] = cm[dst[offset + 5 + y*stride] + ((d*3)>>4)];
dst[offset + 4 + y*stride] = cm[dst[offset + 4 + y*stride] + ((d*1)>>4)];
}
if(right_damage){
dst[offset + 8 + y*stride] = cm[dst[offset + 8 + y*stride] - ((d*7)>>4)];
dst[offset + 9 + y*stride] = cm[dst[offset + 9 + y*stride] - ((d*5)>>4)];
dst[offset + 10+ y*stride] = cm[dst[offset +10 + y*stride] - ((d*3)>>4)];
dst[offset + 11+ y*stride] = cm[dst[offset +11 + y*stride] - ((d*1)>>4)];
}
}
}
}
}
/**
* simple vertical deblocking filter used for error resilience
* @param w width in 8 pixel blocks
* @param h height in 8 pixel blocks
*/
static void v_block_filter(MpegEncContext *s, uint8_t *dst, int w, int h, int stride, int is_luma){
int b_x, b_y, mvx_stride, mvy_stride;
uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
set_mv_strides(s, &mvx_stride, &mvy_stride);
mvx_stride >>= is_luma;
mvy_stride *= mvx_stride;
for(b_y=0; b_y<h-1; b_y++){
for(b_x=0; b_x<w; b_x++){
int x;
int top_status = s->error_status_table[(b_x>>is_luma) + ( b_y >>is_luma)*s->mb_stride];
int bottom_status= s->error_status_table[(b_x>>is_luma) + ((b_y+1)>>is_luma)*s->mb_stride];
int top_intra= IS_INTRA(s->current_picture.mb_type [(b_x>>is_luma) + ( b_y >>is_luma)*s->mb_stride]);
int bottom_intra= IS_INTRA(s->current_picture.mb_type [(b_x>>is_luma) + ((b_y+1)>>is_luma)*s->mb_stride]);
int top_damage = top_status&(DC_ERROR|AC_ERROR|MV_ERROR);
int bottom_damage= bottom_status&(DC_ERROR|AC_ERROR|MV_ERROR);
int offset= b_x*8 + b_y*stride*8;
int16_t *top_mv= s->current_picture.motion_val[0][mvy_stride* b_y + mvx_stride*b_x];
int16_t *bottom_mv= s->current_picture.motion_val[0][mvy_stride*(b_y+1) + mvx_stride*b_x];
if(!(top_damage||bottom_damage)) continue; // both undamaged
if( (!top_intra) && (!bottom_intra)
&& FFABS(top_mv[0]-bottom_mv[0]) + FFABS(top_mv[1]+bottom_mv[1]) < 2) continue;
for(x=0; x<8; x++){
int a,b,c,d;
a= dst[offset + x + 7*stride] - dst[offset + x + 6*stride];
b= dst[offset + x + 8*stride] - dst[offset + x + 7*stride];
c= dst[offset + x + 9*stride] - dst[offset + x + 8*stride];
d= FFABS(b) - ((FFABS(a) + FFABS(c)+1)>>1);
d= FFMAX(d, 0);
if(b<0) d= -d;
if(d==0) continue;
if(!(top_damage && bottom_damage))
d= d*16/9;
if(top_damage){
dst[offset + x + 7*stride] = cm[dst[offset + x + 7*stride] + ((d*7)>>4)];
dst[offset + x + 6*stride] = cm[dst[offset + x + 6*stride] + ((d*5)>>4)];
dst[offset + x + 5*stride] = cm[dst[offset + x + 5*stride] + ((d*3)>>4)];
dst[offset + x + 4*stride] = cm[dst[offset + x + 4*stride] + ((d*1)>>4)];
}
if(bottom_damage){
dst[offset + x + 8*stride] = cm[dst[offset + x + 8*stride] - ((d*7)>>4)];
dst[offset + x + 9*stride] = cm[dst[offset + x + 9*stride] - ((d*5)>>4)];
dst[offset + x + 10*stride] = cm[dst[offset + x + 10*stride] - ((d*3)>>4)];
dst[offset + x + 11*stride] = cm[dst[offset + x + 11*stride] - ((d*1)>>4)];
}
}
}
}
}
static void guess_mv(MpegEncContext *s){
uint8_t fixed[s->mb_stride * s->mb_height];
#define MV_FROZEN 3
#define MV_CHANGED 2
#define MV_UNCHANGED 1
const int mb_stride = s->mb_stride;
const int mb_width = s->mb_width;
const int mb_height= s->mb_height;
int i, depth, num_avail;
int mb_x, mb_y, mot_step, mot_stride;
set_mv_strides(s, &mot_step, &mot_stride);
num_avail=0;
for(i=0; i<s->mb_num; i++){
const int mb_xy= s->mb_index2xy[ i ];
int f=0;
int error= s->error_status_table[mb_xy];
if(IS_INTRA(s->current_picture.mb_type[mb_xy])) f=MV_FROZEN; //intra //FIXME check
if(!(error&MV_ERROR)) f=MV_FROZEN; //inter with undamaged MV
fixed[mb_xy]= f;
if(f==MV_FROZEN)
num_avail++;
}
if((!(s->avctx->error_concealment&FF_EC_GUESS_MVS)) || num_avail <= mb_width/2){
for(mb_y=0; mb_y<s->mb_height; mb_y++){
for(mb_x=0; mb_x<s->mb_width; mb_x++){
const int mb_xy= mb_x + mb_y*s->mb_stride;
if(IS_INTRA(s->current_picture.mb_type[mb_xy])) continue;
if(!(s->error_status_table[mb_xy]&MV_ERROR)) continue;
s->mv_dir = s->last_picture.data[0] ? MV_DIR_FORWARD : MV_DIR_BACKWARD;
s->mb_intra=0;
s->mv_type = MV_TYPE_16X16;
s->mb_skipped=0;
s->dsp.clear_blocks(s->block[0]);
s->mb_x= mb_x;
s->mb_y= mb_y;
s->mv[0][0][0]= 0;
s->mv[0][0][1]= 0;
decode_mb(s, 0);
}
}
return;
}
for(depth=0;; depth++){
int changed, pass, none_left;
none_left=1;
changed=1;
for(pass=0; (changed || pass<2) && pass<10; pass++){
int mb_x, mb_y;
int score_sum=0;
changed=0;
for(mb_y=0; mb_y<s->mb_height; mb_y++){
for(mb_x=0; mb_x<s->mb_width; mb_x++){
const int mb_xy= mb_x + mb_y*s->mb_stride;
int mv_predictor[8][2]={{0}};
int ref[8]={0};
int pred_count=0;
int j;
int best_score=256*256*256*64;
int best_pred=0;
const int mot_index= (mb_x + mb_y*mot_stride) * mot_step;
int prev_x= s->current_picture.motion_val[0][mot_index][0];
int prev_y= s->current_picture.motion_val[0][mot_index][1];
if((mb_x^mb_y^pass)&1) continue;
if(fixed[mb_xy]==MV_FROZEN) continue;
assert(!IS_INTRA(s->current_picture.mb_type[mb_xy]));
assert(s->last_picture_ptr && s->last_picture_ptr->data[0]);
j=0;
if(mb_x>0 && fixed[mb_xy-1 ]==MV_FROZEN) j=1;
if(mb_x+1<mb_width && fixed[mb_xy+1 ]==MV_FROZEN) j=1;
if(mb_y>0 && fixed[mb_xy-mb_stride]==MV_FROZEN) j=1;
if(mb_y+1<mb_height && fixed[mb_xy+mb_stride]==MV_FROZEN) j=1;
if(j==0) continue;
j=0;
if(mb_x>0 && fixed[mb_xy-1 ]==MV_CHANGED) j=1;
if(mb_x+1<mb_width && fixed[mb_xy+1 ]==MV_CHANGED) j=1;
if(mb_y>0 && fixed[mb_xy-mb_stride]==MV_CHANGED) j=1;
if(mb_y+1<mb_height && fixed[mb_xy+mb_stride]==MV_CHANGED) j=1;
if(j==0 && pass>1) continue;
none_left=0;
if(mb_x>0 && fixed[mb_xy-1]){
mv_predictor[pred_count][0]= s->current_picture.motion_val[0][mot_index - mot_step][0];
mv_predictor[pred_count][1]= s->current_picture.motion_val[0][mot_index - mot_step][1];
ref [pred_count] = s->current_picture.ref_index[0][4*(mb_xy-1)];
pred_count++;
}
if(mb_x+1<mb_width && fixed[mb_xy+1]){
mv_predictor[pred_count][0]= s->current_picture.motion_val[0][mot_index + mot_step][0];
mv_predictor[pred_count][1]= s->current_picture.motion_val[0][mot_index + mot_step][1];
ref [pred_count] = s->current_picture.ref_index[0][4*(mb_xy+1)];
pred_count++;
}
if(mb_y>0 && fixed[mb_xy-mb_stride]){
mv_predictor[pred_count][0]= s->current_picture.motion_val[0][mot_index - mot_stride*mot_step][0];
mv_predictor[pred_count][1]= s->current_picture.motion_val[0][mot_index - mot_stride*mot_step][1];
ref [pred_count] = s->current_picture.ref_index[0][4*(mb_xy-s->mb_stride)];
pred_count++;
}
if(mb_y+1<mb_height && fixed[mb_xy+mb_stride]){
mv_predictor[pred_count][0]= s->current_picture.motion_val[0][mot_index + mot_stride*mot_step][0];
mv_predictor[pred_count][1]= s->current_picture.motion_val[0][mot_index + mot_stride*mot_step][1];
ref [pred_count] = s->current_picture.ref_index[0][4*(mb_xy+s->mb_stride)];
pred_count++;
}
if(pred_count==0) continue;
if(pred_count>1){
int sum_x=0, sum_y=0, sum_r=0;
int max_x, max_y, min_x, min_y, max_r, min_r;
for(j=0; j<pred_count; j++){
sum_x+= mv_predictor[j][0];
sum_y+= mv_predictor[j][1];
sum_r+= ref[j];
if(j && ref[j] != ref[j-1])
goto skip_mean_and_median;
}
/* mean */
mv_predictor[pred_count][0] = sum_x/j;
mv_predictor[pred_count][1] = sum_y/j;
ref [pred_count] = sum_r/j;
/* median */
if(pred_count>=3){
min_y= min_x= min_r= 99999;
max_y= max_x= max_r=-99999;
}else{
min_x=min_y=max_x=max_y=min_r=max_r=0;
}
for(j=0; j<pred_count; j++){
max_x= FFMAX(max_x, mv_predictor[j][0]);
max_y= FFMAX(max_y, mv_predictor[j][1]);
max_r= FFMAX(max_r, ref[j]);
min_x= FFMIN(min_x, mv_predictor[j][0]);
min_y= FFMIN(min_y, mv_predictor[j][1]);
min_r= FFMIN(min_r, ref[j]);
}
mv_predictor[pred_count+1][0] = sum_x - max_x - min_x;
mv_predictor[pred_count+1][1] = sum_y - max_y - min_y;
ref [pred_count+1] = sum_r - max_r - min_r;
if(pred_count==4){
mv_predictor[pred_count+1][0] /= 2;
mv_predictor[pred_count+1][1] /= 2;
ref [pred_count+1] /= 2;
}
pred_count+=2;
}
skip_mean_and_median:
/* zero MV */
pred_count++;
/* last MV */
mv_predictor[pred_count][0]= s->current_picture.motion_val[0][mot_index][0];
mv_predictor[pred_count][1]= s->current_picture.motion_val[0][mot_index][1];
ref [pred_count] = s->current_picture.ref_index[0][4*mb_xy];
pred_count++;
s->mv_dir = MV_DIR_FORWARD;
s->mb_intra=0;
s->mv_type = MV_TYPE_16X16;
s->mb_skipped=0;
s->dsp.clear_blocks(s->block[0]);
s->mb_x= mb_x;
s->mb_y= mb_y;
for(j=0; j<pred_count; j++){
int score=0;
uint8_t *src= s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize;
s->current_picture.motion_val[0][mot_index][0]= s->mv[0][0][0]= mv_predictor[j][0];
s->current_picture.motion_val[0][mot_index][1]= s->mv[0][0][1]= mv_predictor[j][1];
if(ref[j]<0) //predictor intra or otherwise not available
continue;
decode_mb(s, ref[j]);
if(mb_x>0 && fixed[mb_xy-1]){
int k;
for(k=0; k<16; k++)
score += FFABS(src[k*s->linesize-1 ]-src[k*s->linesize ]);
}
if(mb_x+1<mb_width && fixed[mb_xy+1]){
int k;
for(k=0; k<16; k++)
score += FFABS(src[k*s->linesize+15]-src[k*s->linesize+16]);
}
if(mb_y>0 && fixed[mb_xy-mb_stride]){
int k;
for(k=0; k<16; k++)
score += FFABS(src[k-s->linesize ]-src[k ]);
}
if(mb_y+1<mb_height && fixed[mb_xy+mb_stride]){
int k;
for(k=0; k<16; k++)
score += FFABS(src[k+s->linesize*15]-src[k+s->linesize*16]);
}
if(score <= best_score){ // <= will favor the last MV
best_score= score;
best_pred= j;
}
}
score_sum+= best_score;
s->mv[0][0][0]= mv_predictor[best_pred][0];
s->mv[0][0][1]= mv_predictor[best_pred][1];
for(i=0; i<mot_step; i++)
for(j=0; j<mot_step; j++){
s->current_picture.motion_val[0][mot_index+i+j*mot_stride][0]= s->mv[0][0][0];
s->current_picture.motion_val[0][mot_index+i+j*mot_stride][1]= s->mv[0][0][1];
}
decode_mb(s, ref[best_pred]);
if(s->mv[0][0][0] != prev_x || s->mv[0][0][1] != prev_y){
fixed[mb_xy]=MV_CHANGED;
changed++;
}else
fixed[mb_xy]=MV_UNCHANGED;
}
}
// printf(".%d/%d", changed, score_sum); fflush(stdout);
}
if(none_left)
return;
for(i=0; i<s->mb_num; i++){
int mb_xy= s->mb_index2xy[i];
if(fixed[mb_xy])
fixed[mb_xy]=MV_FROZEN;
}
// printf(":"); fflush(stdout);
}
}
static int is_intra_more_likely(MpegEncContext *s){
int is_intra_likely, i, j, undamaged_count, skip_amount, mb_x, mb_y;
if(!s->last_picture_ptr || !s->last_picture_ptr->data[0]) return 1; //no previous frame available -> use spatial prediction
undamaged_count=0;
for(i=0; i<s->mb_num; i++){
const int mb_xy= s->mb_index2xy[i];
const int error= s->error_status_table[mb_xy];
if(!((error&DC_ERROR) && (error&MV_ERROR)))
undamaged_count++;
}
if(s->codec_id == CODEC_ID_H264){
H264Context *h= (void*)s;
if(h->ref_count[0] <= 0 || !h->ref_list[0][0].data[0])
return 1;
}
if(undamaged_count < 5) return 0; //almost all MBs damaged -> use temporal prediction
//prevent dsp.sad() check, that requires access to the image
if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration && s->pict_type == FF_I_TYPE)
return 1;
skip_amount= FFMAX(undamaged_count/50, 1); //check only upto 50 MBs
is_intra_likely=0;
j=0;
for(mb_y= 0; mb_y<s->mb_height-1; mb_y++){
for(mb_x= 0; mb_x<s->mb_width; mb_x++){
int error;
const int mb_xy= mb_x + mb_y*s->mb_stride;
error= s->error_status_table[mb_xy];
if((error&DC_ERROR) && (error&MV_ERROR))
continue; //skip damaged
j++;
if((j%skip_amount) != 0) continue; //skip a few to speed things up
if(s->pict_type==FF_I_TYPE){
uint8_t *mb_ptr = s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize;
uint8_t *last_mb_ptr= s->last_picture.data [0] + mb_x*16 + mb_y*16*s->linesize;
is_intra_likely += s->dsp.sad[0](NULL, last_mb_ptr, mb_ptr , s->linesize, 16);
is_intra_likely -= s->dsp.sad[0](NULL, last_mb_ptr, last_mb_ptr+s->linesize*16, s->linesize, 16);
}else{
if(IS_INTRA(s->current_picture.mb_type[mb_xy]))
is_intra_likely++;
else
is_intra_likely--;
}
}
}
//printf("is_intra_likely: %d type:%d\n", is_intra_likely, s->pict_type);
return is_intra_likely > 0;
}
void ff_er_frame_start(MpegEncContext *s){
if(!s->error_recognition) return;
memset(s->error_status_table, MV_ERROR|AC_ERROR|DC_ERROR|VP_START|AC_END|DC_END|MV_END, s->mb_stride*s->mb_height*sizeof(uint8_t));
s->error_count= 3*s->mb_num;
}
/**
* adds a slice.
* @param endx x component of the last macroblock, can be -1 for the last of the previous line
* @param status the status at the end (MV_END, AC_ERROR, ...), it is assumed that no earlier end or
* error of the same type occurred
*/
void ff_er_add_slice(MpegEncContext *s, int startx, int starty, int endx, int endy, int status){
const int start_i= av_clip(startx + starty * s->mb_width , 0, s->mb_num-1);
const int end_i = av_clip(endx + endy * s->mb_width , 0, s->mb_num);
const int start_xy= s->mb_index2xy[start_i];
const int end_xy = s->mb_index2xy[end_i];
int mask= -1;
if(s->avctx->hwaccel)
return;
if(start_i > end_i || start_xy > end_xy){
av_log(s->avctx, AV_LOG_ERROR, "internal error, slice end before start\n");
return;
}
if(!s->error_recognition) return;
mask &= ~VP_START;
if(status & (AC_ERROR|AC_END)){
mask &= ~(AC_ERROR|AC_END);
s->error_count -= end_i - start_i + 1;
}
if(status & (DC_ERROR|DC_END)){
mask &= ~(DC_ERROR|DC_END);
s->error_count -= end_i - start_i + 1;
}
if(status & (MV_ERROR|MV_END)){
mask &= ~(MV_ERROR|MV_END);
s->error_count -= end_i - start_i + 1;
}
if(status & (AC_ERROR|DC_ERROR|MV_ERROR)) s->error_count= INT_MAX;
if(mask == ~0x7F){
memset(&s->error_status_table[start_xy], 0, (end_xy - start_xy) * sizeof(uint8_t));
}else{
int i;
for(i=start_xy; i<end_xy; i++){
s->error_status_table[ i ] &= mask;
}
}
if(end_i == s->mb_num)
s->error_count= INT_MAX;
else{
s->error_status_table[end_xy] &= mask;
s->error_status_table[end_xy] |= status;
}
s->error_status_table[start_xy] |= VP_START;
if(start_xy > 0 && s->avctx->thread_count <= 1 && s->avctx->skip_top*s->mb_width < start_i){
int prev_status= s->error_status_table[ s->mb_index2xy[start_i - 1] ];
prev_status &= ~ VP_START;
if(prev_status != (MV_END|DC_END|AC_END)) s->error_count= INT_MAX;
}
}
void ff_er_frame_end(MpegEncContext *s){
int i, mb_x, mb_y, error, error_type, dc_error, mv_error, ac_error;
int distance;
int threshold_part[4]= {100,100,100};
int threshold= 50;
int is_intra_likely;
int size = s->b8_stride * 2 * s->mb_height;
Picture *pic= s->current_picture_ptr;
if(!s->error_recognition || s->error_count==0 || s->avctx->lowres ||
s->avctx->hwaccel ||
s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU ||
s->picture_structure != PICT_FRAME || // we dont support ER of field pictures yet, though it should not crash if enabled
s->error_count==3*s->mb_width*(s->avctx->skip_top + s->avctx->skip_bottom)) return;
if(s->current_picture.motion_val[0] == NULL){
av_log(s->avctx, AV_LOG_ERROR, "Warning MVs not available\n");
for(i=0; i<2; i++){
pic->ref_index[i]= av_mallocz(s->mb_stride * s->mb_height * 4 * sizeof(uint8_t));
pic->motion_val_base[i]= av_mallocz((size+4) * 2 * sizeof(uint16_t));
pic->motion_val[i]= pic->motion_val_base[i]+4;
}
pic->motion_subsample_log2= 3;
s->current_picture= *s->current_picture_ptr;
}
if(s->avctx->debug&FF_DEBUG_ER){
for(mb_y=0; mb_y<s->mb_height; mb_y++){
for(mb_x=0; mb_x<s->mb_width; mb_x++){
int status= s->error_status_table[mb_x + mb_y*s->mb_stride];
av_log(s->avctx, AV_LOG_DEBUG, "%2X ", status);
}
av_log(s->avctx, AV_LOG_DEBUG, "\n");
}
}
#if 1
/* handle overlapping slices */
for(error_type=1; error_type<=3; error_type++){
int end_ok=0;
for(i=s->mb_num-1; i>=0; i--){
const int mb_xy= s->mb_index2xy[i];
int error= s->error_status_table[mb_xy];
if(error&(1<<error_type))
end_ok=1;
if(error&(8<<error_type))
end_ok=1;
if(!end_ok)
s->error_status_table[mb_xy]|= 1<<error_type;
if(error&VP_START)
end_ok=0;
}
}
#endif
#if 1
/* handle slices with partitions of different length */
if(s->partitioned_frame){
int end_ok=0;
for(i=s->mb_num-1; i>=0; i--){
const int mb_xy= s->mb_index2xy[i];
int error= s->error_status_table[mb_xy];
if(error&AC_END)
end_ok=0;
if((error&MV_END) || (error&DC_END) || (error&AC_ERROR))
end_ok=1;
if(!end_ok)
s->error_status_table[mb_xy]|= AC_ERROR;
if(error&VP_START)
end_ok=0;
}
}
#endif
/* handle missing slices */
if(s->error_recognition>=4){
int end_ok=1;
for(i=s->mb_num-2; i>=s->mb_width+100; i--){ //FIXME +100 hack
const int mb_xy= s->mb_index2xy[i];
int error1= s->error_status_table[mb_xy ];
int error2= s->error_status_table[s->mb_index2xy[i+1]];
if(error1&VP_START)
end_ok=1;
if( error2==(VP_START|DC_ERROR|AC_ERROR|MV_ERROR|AC_END|DC_END|MV_END)
&& error1!=(VP_START|DC_ERROR|AC_ERROR|MV_ERROR|AC_END|DC_END|MV_END)
&& ((error1&AC_END) || (error1&DC_END) || (error1&MV_END))){ //end & uninit
end_ok=0;
}
if(!end_ok)
s->error_status_table[mb_xy]|= DC_ERROR|AC_ERROR|MV_ERROR;
}
}
#if 1
/* backward mark errors */
distance=9999999;
for(error_type=1; error_type<=3; error_type++){
for(i=s->mb_num-1; i>=0; i--){
const int mb_xy= s->mb_index2xy[i];
int error= s->error_status_table[mb_xy];
if(!s->mbskip_table[mb_xy]) //FIXME partition specific
distance++;
if(error&(1<<error_type))
distance= 0;
if(s->partitioned_frame){
if(distance < threshold_part[error_type-1])
s->error_status_table[mb_xy]|= 1<<error_type;
}else{
if(distance < threshold)
s->error_status_table[mb_xy]|= 1<<error_type;
}
if(error&VP_START)
distance= 9999999;
}
}
#endif
/* forward mark errors */
error=0;
for(i=0; i<s->mb_num; i++){
const int mb_xy= s->mb_index2xy[i];
int old_error= s->error_status_table[mb_xy];
if(old_error&VP_START)
error= old_error& (DC_ERROR|AC_ERROR|MV_ERROR);
else{
error|= old_error& (DC_ERROR|AC_ERROR|MV_ERROR);
s->error_status_table[mb_xy]|= error;
}
}
#if 1
/* handle not partitioned case */
if(!s->partitioned_frame){
for(i=0; i<s->mb_num; i++){
const int mb_xy= s->mb_index2xy[i];
error= s->error_status_table[mb_xy];
if(error&(AC_ERROR|DC_ERROR|MV_ERROR))
error|= AC_ERROR|DC_ERROR|MV_ERROR;
s->error_status_table[mb_xy]= error;
}
}
#endif
dc_error= ac_error= mv_error=0;
for(i=0; i<s->mb_num; i++){
const int mb_xy= s->mb_index2xy[i];
error= s->error_status_table[mb_xy];
if(error&DC_ERROR) dc_error ++;
if(error&AC_ERROR) ac_error ++;
if(error&MV_ERROR) mv_error ++;
}
av_log(s->avctx, AV_LOG_INFO, "concealing %d DC, %d AC, %d MV errors\n", dc_error, ac_error, mv_error);
is_intra_likely= is_intra_more_likely(s);
/* set unknown mb-type to most likely */
for(i=0; i<s->mb_num; i++){
const int mb_xy= s->mb_index2xy[i];
error= s->error_status_table[mb_xy];
if(!((error&DC_ERROR) && (error&MV_ERROR)))
continue;
if(is_intra_likely)
s->current_picture.mb_type[mb_xy]= MB_TYPE_INTRA4x4;
else
s->current_picture.mb_type[mb_xy]= MB_TYPE_16x16 | MB_TYPE_L0;
}
// change inter to intra blocks if no reference frames are available
if (!s->last_picture.data[0] && !s->next_picture.data[0])
for(i=0; i<s->mb_num; i++){
const int mb_xy= s->mb_index2xy[i];
if(!IS_INTRA(s->current_picture.mb_type[mb_xy]))
s->current_picture.mb_type[mb_xy]= MB_TYPE_INTRA4x4;
}
/* handle inter blocks with damaged AC */
for(mb_y=0; mb_y<s->mb_height; mb_y++){
for(mb_x=0; mb_x<s->mb_width; mb_x++){
const int mb_xy= mb_x + mb_y * s->mb_stride;
const int mb_type= s->current_picture.mb_type[mb_xy];
int dir = !s->last_picture.data[0];
error= s->error_status_table[mb_xy];
if(IS_INTRA(mb_type)) continue; //intra
if(error&MV_ERROR) continue; //inter with damaged MV
if(!(error&AC_ERROR)) continue; //undamaged inter
s->mv_dir = dir ? MV_DIR_BACKWARD : MV_DIR_FORWARD;
s->mb_intra=0;
s->mb_skipped=0;
if(IS_8X8(mb_type)){
int mb_index= mb_x*2 + mb_y*2*s->b8_stride;
int j;
s->mv_type = MV_TYPE_8X8;
for(j=0; j<4; j++){
s->mv[0][j][0] = s->current_picture.motion_val[dir][ mb_index + (j&1) + (j>>1)*s->b8_stride ][0];
s->mv[0][j][1] = s->current_picture.motion_val[dir][ mb_index + (j&1) + (j>>1)*s->b8_stride ][1];
}
}else{
s->mv_type = MV_TYPE_16X16;
s->mv[0][0][0] = s->current_picture.motion_val[dir][ mb_x*2 + mb_y*2*s->b8_stride ][0];
s->mv[0][0][1] = s->current_picture.motion_val[dir][ mb_x*2 + mb_y*2*s->b8_stride ][1];
}
s->dsp.clear_blocks(s->block[0]);
s->mb_x= mb_x;
s->mb_y= mb_y;
decode_mb(s, 0/*FIXME h264 partitioned slices need this set*/);
}
}
/* guess MVs */
if(s->pict_type==FF_B_TYPE){
for(mb_y=0; mb_y<s->mb_height; mb_y++){
for(mb_x=0; mb_x<s->mb_width; mb_x++){
int xy= mb_x*2 + mb_y*2*s->b8_stride;
const int mb_xy= mb_x + mb_y * s->mb_stride;
const int mb_type= s->current_picture.mb_type[mb_xy];
error= s->error_status_table[mb_xy];
if(IS_INTRA(mb_type)) continue;
if(!(error&MV_ERROR)) continue; //inter with undamaged MV
if(!(error&AC_ERROR)) continue; //undamaged inter
s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD;
if(!s->last_picture.data[0]) s->mv_dir &= ~MV_DIR_FORWARD;
if(!s->next_picture.data[0]) s->mv_dir &= ~MV_DIR_BACKWARD;
s->mb_intra=0;
s->mv_type = MV_TYPE_16X16;
s->mb_skipped=0;
if(s->pp_time){
int time_pp= s->pp_time;
int time_pb= s->pb_time;
s->mv[0][0][0] = s->next_picture.motion_val[0][xy][0]*time_pb/time_pp;
s->mv[0][0][1] = s->next_picture.motion_val[0][xy][1]*time_pb/time_pp;
s->mv[1][0][0] = s->next_picture.motion_val[0][xy][0]*(time_pb - time_pp)/time_pp;
s->mv[1][0][1] = s->next_picture.motion_val[0][xy][1]*(time_pb - time_pp)/time_pp;
}else{
s->mv[0][0][0]= 0;
s->mv[0][0][1]= 0;
s->mv[1][0][0]= 0;
s->mv[1][0][1]= 0;
}
s->dsp.clear_blocks(s->block[0]);
s->mb_x= mb_x;
s->mb_y= mb_y;
decode_mb(s, 0);
}
}
}else
guess_mv(s);
/* the filters below are not XvMC compatible, skip them */
if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
goto ec_clean;
/* fill DC for inter blocks */
for(mb_y=0; mb_y<s->mb_height; mb_y++){
for(mb_x=0; mb_x<s->mb_width; mb_x++){
int dc, dcu, dcv, y, n;
int16_t *dc_ptr;
uint8_t *dest_y, *dest_cb, *dest_cr;
const int mb_xy= mb_x + mb_y * s->mb_stride;
const int mb_type= s->current_picture.mb_type[mb_xy];
error= s->error_status_table[mb_xy];
if(IS_INTRA(mb_type) && s->partitioned_frame) continue;
// if(error&MV_ERROR) continue; //inter data damaged FIXME is this good?
dest_y = s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize;
dest_cb= s->current_picture.data[1] + mb_x*8 + mb_y*8 *s->uvlinesize;
dest_cr= s->current_picture.data[2] + mb_x*8 + mb_y*8 *s->uvlinesize;
dc_ptr= &s->dc_val[0][mb_x*2 + mb_y*2*s->b8_stride];
for(n=0; n<4; n++){
dc=0;
for(y=0; y<8; y++){
int x;
for(x=0; x<8; x++){
dc+= dest_y[x + (n&1)*8 + (y + (n>>1)*8)*s->linesize];
}
}
dc_ptr[(n&1) + (n>>1)*s->b8_stride]= (dc+4)>>3;
}
dcu=dcv=0;
for(y=0; y<8; y++){
int x;
for(x=0; x<8; x++){
dcu+=dest_cb[x + y*(s->uvlinesize)];
dcv+=dest_cr[x + y*(s->uvlinesize)];
}
}
s->dc_val[1][mb_x + mb_y*s->mb_stride]= (dcu+4)>>3;
s->dc_val[2][mb_x + mb_y*s->mb_stride]= (dcv+4)>>3;
}
}
#if 1
/* guess DC for damaged blocks */
guess_dc(s, s->dc_val[0], s->mb_width*2, s->mb_height*2, s->b8_stride, 1);
guess_dc(s, s->dc_val[1], s->mb_width , s->mb_height , s->mb_stride, 0);
guess_dc(s, s->dc_val[2], s->mb_width , s->mb_height , s->mb_stride, 0);
#endif
/* filter luma DC */
filter181(s->dc_val[0], s->mb_width*2, s->mb_height*2, s->b8_stride);
#if 1
/* render DC only intra */
for(mb_y=0; mb_y<s->mb_height; mb_y++){
for(mb_x=0; mb_x<s->mb_width; mb_x++){
uint8_t *dest_y, *dest_cb, *dest_cr;
const int mb_xy= mb_x + mb_y * s->mb_stride;
const int mb_type= s->current_picture.mb_type[mb_xy];
error= s->error_status_table[mb_xy];
if(IS_INTER(mb_type)) continue;
if(!(error&AC_ERROR)) continue; //undamaged
dest_y = s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize;
dest_cb= s->current_picture.data[1] + mb_x*8 + mb_y*8 *s->uvlinesize;
dest_cr= s->current_picture.data[2] + mb_x*8 + mb_y*8 *s->uvlinesize;
put_dc(s, dest_y, dest_cb, dest_cr, mb_x, mb_y);
}
}
#endif
if(s->avctx->error_concealment&FF_EC_DEBLOCK){
/* filter horizontal block boundaries */
h_block_filter(s, s->current_picture.data[0], s->mb_width*2, s->mb_height*2, s->linesize , 1);
h_block_filter(s, s->current_picture.data[1], s->mb_width , s->mb_height , s->uvlinesize, 0);
h_block_filter(s, s->current_picture.data[2], s->mb_width , s->mb_height , s->uvlinesize, 0);
/* filter vertical block boundaries */
v_block_filter(s, s->current_picture.data[0], s->mb_width*2, s->mb_height*2, s->linesize , 1);
v_block_filter(s, s->current_picture.data[1], s->mb_width , s->mb_height , s->uvlinesize, 0);
v_block_filter(s, s->current_picture.data[2], s->mb_width , s->mb_height , s->uvlinesize, 0);
}
ec_clean:
/* clean a few tables */
for(i=0; i<s->mb_num; i++){
const int mb_xy= s->mb_index2xy[i];
int error= s->error_status_table[mb_xy];
if(s->pict_type!=FF_B_TYPE && (error&(DC_ERROR|MV_ERROR|AC_ERROR))){
s->mbskip_table[mb_xy]=0;
}
s->mbintra_table[mb_xy]=1;
}
}
| 123linslouis-android-video-cutter | jni/libavcodec/error_resilience.c | C | asf20 | 43,806 |
/*
* H261 encoder
* Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
* Copyright (c) 2004 Maarten Daniels
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* H.261 encoder.
*/
#include "dsputil.h"
#include "avcodec.h"
#include "mpegvideo.h"
#include "h263.h"
#include "h261.h"
#include "h261data.h"
extern uint8_t ff_h261_rl_table_store[2][2*MAX_RUN + MAX_LEVEL + 3];
static void h261_encode_block(H261Context * h, DCTELEM * block,
int n);
int ff_h261_get_picture_format(int width, int height){
// QCIF
if (width == 176 && height == 144)
return 0;
// CIF
else if (width == 352 && height == 288)
return 1;
// ERROR
else
return -1;
}
void ff_h261_encode_picture_header(MpegEncContext * s, int picture_number){
H261Context * h = (H261Context *) s;
int format, temp_ref;
align_put_bits(&s->pb);
/* Update the pointer to last GOB */
s->ptr_lastgob = put_bits_ptr(&s->pb);
put_bits(&s->pb, 20, 0x10); /* PSC */
temp_ref= s->picture_number * (int64_t)30000 * s->avctx->time_base.num /
(1001 * (int64_t)s->avctx->time_base.den); //FIXME maybe this should use a timestamp
put_sbits(&s->pb, 5, temp_ref); /* TemporalReference */
put_bits(&s->pb, 1, 0); /* split screen off */
put_bits(&s->pb, 1, 0); /* camera off */
put_bits(&s->pb, 1, 0); /* freeze picture release off */
format = ff_h261_get_picture_format(s->width, s->height);
put_bits(&s->pb, 1, format); /* 0 == QCIF, 1 == CIF */
put_bits(&s->pb, 1, 0); /* still image mode */
put_bits(&s->pb, 1, 0); /* reserved */
put_bits(&s->pb, 1, 0); /* no PEI */
if(format == 0)
h->gob_number = -1;
else
h->gob_number = 0;
h->current_mba = 0;
}
/**
* Encodes a group of blocks header.
*/
static void h261_encode_gob_header(MpegEncContext * s, int mb_line){
H261Context * h = (H261Context *)s;
if(ff_h261_get_picture_format(s->width, s->height) == 0){
h->gob_number+=2; // QCIF
}
else{
h->gob_number++; // CIF
}
put_bits(&s->pb, 16, 1); /* GBSC */
put_bits(&s->pb, 4, h->gob_number); /* GN */
put_bits(&s->pb, 5, s->qscale); /* GQUANT */
put_bits(&s->pb, 1, 0); /* no GEI */
h->current_mba = 0;
h->previous_mba = 0;
h->current_mv_x=0;
h->current_mv_y=0;
}
void ff_h261_reorder_mb_index(MpegEncContext* s){
int index= s->mb_x + s->mb_y*s->mb_width;
if(index % 33 == 0)
h261_encode_gob_header(s,0);
/* for CIF the GOB's are fragmented in the middle of a scanline
that's why we need to adjust the x and y index of the macroblocks */
if(ff_h261_get_picture_format(s->width,s->height) == 1){ // CIF
s->mb_x = index % 11 ; index /= 11;
s->mb_y = index % 3 ; index /= 3;
s->mb_x+= 11*(index % 2); index /= 2;
s->mb_y+= 3*index;
ff_init_block_index(s);
ff_update_block_index(s);
}
}
static void h261_encode_motion(H261Context * h, int val){
MpegEncContext * const s = &h->s;
int sign, code;
if(val==0){
code = 0;
put_bits(&s->pb,h261_mv_tab[code][1],h261_mv_tab[code][0]);
}
else{
if(val > 15)
val -=32;
if(val < -16)
val+=32;
sign = val < 0;
code = sign ? -val : val;
put_bits(&s->pb,h261_mv_tab[code][1],h261_mv_tab[code][0]);
put_bits(&s->pb,1,sign);
}
}
static inline int get_cbp(MpegEncContext * s,
DCTELEM block[6][64])
{
int i, cbp;
cbp= 0;
for (i = 0; i < 6; i++) {
if (s->block_last_index[i] >= 0)
cbp |= 1 << (5 - i);
}
return cbp;
}
void ff_h261_encode_mb(MpegEncContext * s,
DCTELEM block[6][64],
int motion_x, int motion_y)
{
H261Context * h = (H261Context *)s;
int mvd, mv_diff_x, mv_diff_y, i, cbp;
cbp = 63; // avoid warning
mvd = 0;
h->current_mba++;
h->mtype = 0;
if (!s->mb_intra){
/* compute cbp */
cbp= get_cbp(s, block);
/* mvd indicates if this block is motion compensated */
mvd = motion_x | motion_y;
if((cbp | mvd | s->dquant ) == 0) {
/* skip macroblock */
s->skip_count++;
h->current_mv_x=0;
h->current_mv_y=0;
return;
}
}
/* MB is not skipped, encode MBA */
put_bits(&s->pb, h261_mba_bits[(h->current_mba-h->previous_mba)-1], h261_mba_code[(h->current_mba-h->previous_mba)-1]);
/* calculate MTYPE */
if(!s->mb_intra){
h->mtype++;
if(mvd || s->loop_filter)
h->mtype+=3;
if(s->loop_filter)
h->mtype+=3;
if(cbp || s->dquant)
h->mtype++;
assert(h->mtype > 1);
}
if(s->dquant)
h->mtype++;
put_bits(&s->pb, h261_mtype_bits[h->mtype], h261_mtype_code[h->mtype]);
h->mtype = h261_mtype_map[h->mtype];
if(IS_QUANT(h->mtype)){
ff_set_qscale(s,s->qscale+s->dquant);
put_bits(&s->pb, 5, s->qscale);
}
if(IS_16X16(h->mtype)){
mv_diff_x = (motion_x >> 1) - h->current_mv_x;
mv_diff_y = (motion_y >> 1) - h->current_mv_y;
h->current_mv_x = (motion_x >> 1);
h->current_mv_y = (motion_y >> 1);
h261_encode_motion(h,mv_diff_x);
h261_encode_motion(h,mv_diff_y);
}
h->previous_mba = h->current_mba;
if(HAS_CBP(h->mtype)){
assert(cbp>0);
put_bits(&s->pb,h261_cbp_tab[cbp-1][1],h261_cbp_tab[cbp-1][0]);
}
for(i=0; i<6; i++) {
/* encode each block */
h261_encode_block(h, block[i], i);
}
if ( ( h->current_mba == 11 ) || ( h->current_mba == 22 ) || ( h->current_mba == 33 ) || ( !IS_16X16 ( h->mtype ) )){
h->current_mv_x=0;
h->current_mv_y=0;
}
}
void ff_h261_encode_init(MpegEncContext *s){
static int done = 0;
if (!done) {
done = 1;
init_rl(&h261_rl_tcoeff, ff_h261_rl_table_store);
}
s->min_qcoeff= -127;
s->max_qcoeff= 127;
s->y_dc_scale_table=
s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
}
/**
* encodes a 8x8 block.
* @param block the 8x8 block
* @param n block index (0-3 are luma, 4-5 are chroma)
*/
static void h261_encode_block(H261Context * h, DCTELEM * block, int n){
MpegEncContext * const s = &h->s;
int level, run, i, j, last_index, last_non_zero, sign, slevel, code;
RLTable *rl;
rl = &h261_rl_tcoeff;
if (s->mb_intra) {
/* DC coef */
level = block[0];
/* 255 cannot be represented, so we clamp */
if (level > 254) {
level = 254;
block[0] = 254;
}
/* 0 cannot be represented also */
else if (level < 1) {
level = 1;
block[0] = 1;
}
if (level == 128)
put_bits(&s->pb, 8, 0xff);
else
put_bits(&s->pb, 8, level);
i = 1;
} else if((block[0]==1 || block[0] == -1) && (s->block_last_index[n] > -1)){
//special case
put_bits(&s->pb,2,block[0]>0 ? 2 : 3 );
i = 1;
} else {
i = 0;
}
/* AC coefs */
last_index = s->block_last_index[n];
last_non_zero = i - 1;
for (; i <= last_index; i++) {
j = s->intra_scantable.permutated[i];
level = block[j];
if (level) {
run = i - last_non_zero - 1;
sign = 0;
slevel = level;
if (level < 0) {
sign = 1;
level = -level;
}
code = get_rl_index(rl, 0 /*no last in H.261, EOB is used*/, run, level);
if(run==0 && level < 16)
code+=1;
put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
if (code == rl->n) {
put_bits(&s->pb, 6, run);
assert(slevel != 0);
assert(level <= 127);
put_sbits(&s->pb, 8, slevel);
} else {
put_bits(&s->pb, 1, sign);
}
last_non_zero = i;
}
}
if(last_index > -1){
put_bits(&s->pb, rl->table_vlc[0][1], rl->table_vlc[0][0]);// END OF BLOCK
}
}
AVCodec h261_encoder = {
"h261",
AVMEDIA_TYPE_VIDEO,
CODEC_ID_H261,
sizeof(H261Context),
MPV_encode_init,
MPV_encode_picture,
MPV_encode_end,
.pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
.long_name= NULL_IF_CONFIG_SMALL("H.261"),
};
| 123linslouis-android-video-cutter | jni/libavcodec/h261enc.c | C | asf20 | 9,318 |
/*
* DVB subtitle encoding for ffmpeg
* Copyright (c) 2005 Fabrice Bellard
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "avcodec.h"
#include "bytestream.h"
#include "colorspace.h"
typedef struct DVBSubtitleContext {
int hide_state;
int object_version;
} DVBSubtitleContext;
#define PUTBITS2(val)\
{\
bitbuf |= (val) << bitcnt;\
bitcnt -= 2;\
if (bitcnt < 0) {\
bitcnt = 6;\
*q++ = bitbuf;\
bitbuf = 0;\
}\
}
static void dvb_encode_rle2(uint8_t **pq,
const uint8_t *bitmap, int linesize,
int w, int h)
{
uint8_t *q;
unsigned int bitbuf;
int bitcnt;
int x, y, len, x1, v, color;
q = *pq;
for(y = 0; y < h; y++) {
*q++ = 0x10;
bitbuf = 0;
bitcnt = 6;
x = 0;
while (x < w) {
x1 = x;
color = bitmap[x1++];
while (x1 < w && bitmap[x1] == color)
x1++;
len = x1 - x;
if (color == 0 && len == 2) {
PUTBITS2(0);
PUTBITS2(0);
PUTBITS2(1);
} else if (len >= 3 && len <= 10) {
v = len - 3;
PUTBITS2(0);
PUTBITS2((v >> 2) | 2);
PUTBITS2(v & 3);
PUTBITS2(color);
} else if (len >= 12 && len <= 27) {
v = len - 12;
PUTBITS2(0);
PUTBITS2(0);
PUTBITS2(2);
PUTBITS2(v >> 2);
PUTBITS2(v & 3);
PUTBITS2(color);
} else if (len >= 29) {
/* length = 29 ... 284 */
if (len > 284)
len = 284;
v = len - 29;
PUTBITS2(0);
PUTBITS2(0);
PUTBITS2(3);
PUTBITS2((v >> 6));
PUTBITS2((v >> 4) & 3);
PUTBITS2((v >> 2) & 3);
PUTBITS2(v & 3);
PUTBITS2(color);
} else {
PUTBITS2(color);
if (color == 0) {
PUTBITS2(1);
}
len = 1;
}
x += len;
}
/* end of line */
PUTBITS2(0);
PUTBITS2(0);
PUTBITS2(0);
if (bitcnt != 6) {
*q++ = bitbuf;
}
*q++ = 0xf0;
bitmap += linesize;
}
*pq = q;
}
#define PUTBITS4(val)\
{\
bitbuf |= (val) << bitcnt;\
bitcnt -= 4;\
if (bitcnt < 0) {\
bitcnt = 4;\
*q++ = bitbuf;\
bitbuf = 0;\
}\
}
/* some DVB decoders only implement 4 bits/pixel */
static void dvb_encode_rle4(uint8_t **pq,
const uint8_t *bitmap, int linesize,
int w, int h)
{
uint8_t *q;
unsigned int bitbuf;
int bitcnt;
int x, y, len, x1, v, color;
q = *pq;
for(y = 0; y < h; y++) {
*q++ = 0x11;
bitbuf = 0;
bitcnt = 4;
x = 0;
while (x < w) {
x1 = x;
color = bitmap[x1++];
while (x1 < w && bitmap[x1] == color)
x1++;
len = x1 - x;
if (color == 0 && len == 2) {
PUTBITS4(0);
PUTBITS4(0xd);
} else if (color == 0 && (len >= 3 && len <= 9)) {
PUTBITS4(0);
PUTBITS4(len - 2);
} else if (len >= 4 && len <= 7) {
PUTBITS4(0);
PUTBITS4(8 + len - 4);
PUTBITS4(color);
} else if (len >= 9 && len <= 24) {
PUTBITS4(0);
PUTBITS4(0xe);
PUTBITS4(len - 9);
PUTBITS4(color);
} else if (len >= 25) {
if (len > 280)
len = 280;
v = len - 25;
PUTBITS4(0);
PUTBITS4(0xf);
PUTBITS4(v >> 4);
PUTBITS4(v & 0xf);
PUTBITS4(color);
} else {
PUTBITS4(color);
if (color == 0) {
PUTBITS4(0xc);
}
len = 1;
}
x += len;
}
/* end of line */
PUTBITS4(0);
PUTBITS4(0);
if (bitcnt != 4) {
*q++ = bitbuf;
}
*q++ = 0xf0;
bitmap += linesize;
}
*pq = q;
}
static int encode_dvb_subtitles(DVBSubtitleContext *s,
uint8_t *outbuf, AVSubtitle *h)
{
uint8_t *q, *pseg_len;
int page_id, region_id, clut_id, object_id, i, bpp_index, page_state;
q = outbuf;
page_id = 1;
if (h->num_rects == 0 || h->rects == NULL)
return -1;
*q++ = 0x00; /* subtitle_stream_id */
/* page composition segment */
*q++ = 0x0f; /* sync_byte */
*q++ = 0x10; /* segment_type */
bytestream_put_be16(&q, page_id);
pseg_len = q;
q += 2; /* segment length */
*q++ = 30; /* page_timeout (seconds) */
if (s->hide_state)
page_state = 0; /* normal case */
else
page_state = 2; /* mode change */
/* page_version = 0 + page_state */
*q++ = s->object_version | (page_state << 2) | 3;
for (region_id = 0; region_id < h->num_rects; region_id++) {
*q++ = region_id;
*q++ = 0xff; /* reserved */
bytestream_put_be16(&q, h->rects[region_id]->x); /* left pos */
bytestream_put_be16(&q, h->rects[region_id]->y); /* top pos */
}
bytestream_put_be16(&pseg_len, q - pseg_len - 2);
if (!s->hide_state) {
for (clut_id = 0; clut_id < h->num_rects; clut_id++) {
/* CLUT segment */
if (h->rects[clut_id]->nb_colors <= 4) {
/* 2 bpp, some decoders do not support it correctly */
bpp_index = 0;
} else if (h->rects[clut_id]->nb_colors <= 16) {
/* 4 bpp, standard encoding */
bpp_index = 1;
} else {
return -1;
}
*q++ = 0x0f; /* sync byte */
*q++ = 0x12; /* CLUT definition segment */
bytestream_put_be16(&q, page_id);
pseg_len = q;
q += 2; /* segment length */
*q++ = clut_id;
*q++ = (0 << 4) | 0xf; /* version = 0 */
for(i = 0; i < h->rects[clut_id]->nb_colors; i++) {
*q++ = i; /* clut_entry_id */
*q++ = (1 << (7 - bpp_index)) | (0xf << 1) | 1; /* 2 bits/pixel full range */
{
int a, r, g, b;
uint32_t x= ((uint32_t*)h->rects[clut_id]->pict.data[1])[i];
a = (x >> 24) & 0xff;
r = (x >> 16) & 0xff;
g = (x >> 8) & 0xff;
b = (x >> 0) & 0xff;
*q++ = RGB_TO_Y_CCIR(r, g, b);
*q++ = RGB_TO_V_CCIR(r, g, b, 0);
*q++ = RGB_TO_U_CCIR(r, g, b, 0);
*q++ = 255 - a;
}
}
bytestream_put_be16(&pseg_len, q - pseg_len - 2);
}
}
for (region_id = 0; region_id < h->num_rects; region_id++) {
/* region composition segment */
if (h->rects[region_id]->nb_colors <= 4) {
/* 2 bpp, some decoders do not support it correctly */
bpp_index = 0;
} else if (h->rects[region_id]->nb_colors <= 16) {
/* 4 bpp, standard encoding */
bpp_index = 1;
} else {
return -1;
}
*q++ = 0x0f; /* sync_byte */
*q++ = 0x11; /* segment_type */
bytestream_put_be16(&q, page_id);
pseg_len = q;
q += 2; /* segment length */
*q++ = region_id;
*q++ = (s->object_version << 4) | (0 << 3) | 0x07; /* version , no fill */
bytestream_put_be16(&q, h->rects[region_id]->w); /* region width */
bytestream_put_be16(&q, h->rects[region_id]->h); /* region height */
*q++ = ((1 + bpp_index) << 5) | ((1 + bpp_index) << 2) | 0x03;
*q++ = region_id; /* clut_id == region_id */
*q++ = 0; /* 8 bit fill colors */
*q++ = 0x03; /* 4 bit and 2 bit fill colors */
if (!s->hide_state) {
bytestream_put_be16(&q, region_id); /* object_id == region_id */
*q++ = (0 << 6) | (0 << 4);
*q++ = 0;
*q++ = 0xf0;
*q++ = 0;
}
bytestream_put_be16(&pseg_len, q - pseg_len - 2);
}
if (!s->hide_state) {
for (object_id = 0; object_id < h->num_rects; object_id++) {
/* Object Data segment */
if (h->rects[object_id]->nb_colors <= 4) {
/* 2 bpp, some decoders do not support it correctly */
bpp_index = 0;
} else if (h->rects[object_id]->nb_colors <= 16) {
/* 4 bpp, standard encoding */
bpp_index = 1;
} else {
return -1;
}
*q++ = 0x0f; /* sync byte */
*q++ = 0x13;
bytestream_put_be16(&q, page_id);
pseg_len = q;
q += 2; /* segment length */
bytestream_put_be16(&q, object_id);
*q++ = (s->object_version << 4) | (0 << 2) | (0 << 1) | 1; /* version = 0,
onject_coding_method,
non_modifying_color_flag */
{
uint8_t *ptop_field_len, *pbottom_field_len, *top_ptr, *bottom_ptr;
void (*dvb_encode_rle)(uint8_t **pq,
const uint8_t *bitmap, int linesize,
int w, int h);
ptop_field_len = q;
q += 2;
pbottom_field_len = q;
q += 2;
if (bpp_index == 0)
dvb_encode_rle = dvb_encode_rle2;
else
dvb_encode_rle = dvb_encode_rle4;
top_ptr = q;
dvb_encode_rle(&q, h->rects[object_id]->pict.data[0], h->rects[object_id]->w * 2,
h->rects[object_id]->w, h->rects[object_id]->h >> 1);
bottom_ptr = q;
dvb_encode_rle(&q, h->rects[object_id]->pict.data[0] + h->rects[object_id]->w,
h->rects[object_id]->w * 2, h->rects[object_id]->w,
h->rects[object_id]->h >> 1);
bytestream_put_be16(&ptop_field_len, bottom_ptr - top_ptr);
bytestream_put_be16(&pbottom_field_len, q - bottom_ptr);
}
bytestream_put_be16(&pseg_len, q - pseg_len - 2);
}
}
/* end of display set segment */
*q++ = 0x0f; /* sync_byte */
*q++ = 0x80; /* segment_type */
bytestream_put_be16(&q, page_id);
pseg_len = q;
q += 2; /* segment length */
bytestream_put_be16(&pseg_len, q - pseg_len - 2);
*q++ = 0xff; /* end of PES data */
s->object_version = (s->object_version + 1) & 0xf;
s->hide_state = !s->hide_state;
return q - outbuf;
}
static int dvbsub_encode(AVCodecContext *avctx,
unsigned char *buf, int buf_size, void *data)
{
DVBSubtitleContext *s = avctx->priv_data;
AVSubtitle *sub = data;
int ret;
ret = encode_dvb_subtitles(s, buf, sub);
return ret;
}
AVCodec dvbsub_encoder = {
"dvbsub",
AVMEDIA_TYPE_SUBTITLE,
CODEC_ID_DVB_SUBTITLE,
sizeof(DVBSubtitleContext),
NULL,
dvbsub_encode,
.long_name = NULL_IF_CONFIG_SMALL("DVB subtitles"),
};
| 123linslouis-android-video-cutter | jni/libavcodec/dvbsub.c | C | asf20 | 12,564 |
/*
* Bink video decoder
* Copyright (c) 2009 Konstantin Shishkov
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "avcodec.h"
#include "dsputil.h"
#include "binkdata.h"
#include "mathops.h"
#define ALT_BITSTREAM_READER_LE
#include "get_bits.h"
#define BINK_FLAG_ALPHA 0x00100000
#define BINK_FLAG_GRAY 0x00020000
static VLC bink_trees[16];
/**
* IDs for different data types used in Bink video codec
*/
enum Sources {
BINK_SRC_BLOCK_TYPES = 0, ///< 8x8 block types
BINK_SRC_SUB_BLOCK_TYPES, ///< 16x16 block types (a subset of 8x8 block types)
BINK_SRC_COLORS, ///< pixel values used for different block types
BINK_SRC_PATTERN, ///< 8-bit values for 2-colour pattern fill
BINK_SRC_X_OFF, ///< X components of motion value
BINK_SRC_Y_OFF, ///< Y components of motion value
BINK_SRC_INTRA_DC, ///< DC values for intrablocks with DCT
BINK_SRC_INTER_DC, ///< DC values for interblocks with DCT
BINK_SRC_RUN, ///< run lengths for special fill block
BINK_NB_SRC
};
/**
* data needed to decode 4-bit Huffman-coded value
*/
typedef struct Tree {
int vlc_num; ///< tree number (in bink_trees[])
uint8_t syms[16]; ///< leaf value to symbol mapping
} Tree;
#define GET_HUFF(gb, tree) (tree).syms[get_vlc2(gb, bink_trees[(tree).vlc_num].table,\
bink_trees[(tree).vlc_num].bits, 1)]
/**
* data structure used for decoding single Bink data type
*/
typedef struct Bundle {
int len; ///< length of number of entries to decode (in bits)
Tree tree; ///< Huffman tree-related data
uint8_t *data; ///< buffer for decoded symbols
uint8_t *data_end; ///< buffer end
uint8_t *cur_dec; ///< pointer to the not yet decoded part of the buffer
uint8_t *cur_ptr; ///< pointer to the data that is not read from buffer yet
} Bundle;
/*
* Decoder context
*/
typedef struct BinkContext {
AVCodecContext *avctx;
DSPContext dsp;
AVFrame pic, last;
int version; ///< internal Bink file version
int has_alpha;
int swap_planes;
ScanTable scantable; ///< permutated scantable for DCT coeffs decoding
Bundle bundle[BINK_NB_SRC]; ///< bundles for decoding all data types
Tree col_high[16]; ///< trees for decoding high nibble in "colours" data type
int col_lastval; ///< value of last decoded high nibble in "colours" data type
} BinkContext;
/**
* Bink video block types
*/
enum BlockTypes {
SKIP_BLOCK = 0, ///< skipped block
SCALED_BLOCK, ///< block has size 16x16
MOTION_BLOCK, ///< block is copied from previous frame with some offset
RUN_BLOCK, ///< block is composed from runs of colours with custom scan order
RESIDUE_BLOCK, ///< motion block with some difference added
INTRA_BLOCK, ///< intra DCT block
FILL_BLOCK, ///< block is filled with single colour
INTER_BLOCK, ///< motion block with DCT applied to the difference
PATTERN_BLOCK, ///< block is filled with two colours following custom pattern
RAW_BLOCK, ///< uncoded 8x8 block
};
/**
* Initializes length length in all bundles.
*
* @param c decoder context
* @param width plane width
* @param bw plane width in 8x8 blocks
*/
static void init_lengths(BinkContext *c, int width, int bw)
{
c->bundle[BINK_SRC_BLOCK_TYPES].len = av_log2((width >> 3) + 511) + 1;
c->bundle[BINK_SRC_SUB_BLOCK_TYPES].len = av_log2((width >> 4) + 511) + 1;
c->bundle[BINK_SRC_COLORS].len = av_log2((width >> 3)*64 + 511) + 1;
c->bundle[BINK_SRC_INTRA_DC].len =
c->bundle[BINK_SRC_INTER_DC].len =
c->bundle[BINK_SRC_X_OFF].len =
c->bundle[BINK_SRC_Y_OFF].len = av_log2((width >> 3) + 511) + 1;
c->bundle[BINK_SRC_PATTERN].len = av_log2((bw << 3) + 511) + 1;
c->bundle[BINK_SRC_RUN].len = av_log2((width >> 3)*48 + 511) + 1;
}
/**
* Allocates memory for bundles.
*
* @param c decoder context
*/
static av_cold void init_bundles(BinkContext *c)
{
int bw, bh, blocks;
int i;
bw = (c->avctx->width + 7) >> 3;
bh = (c->avctx->height + 7) >> 3;
blocks = bw * bh;
for (i = 0; i < BINK_NB_SRC; i++) {
c->bundle[i].data = av_malloc(blocks * 64);
c->bundle[i].data_end = c->bundle[i].data + blocks * 64;
}
}
/**
* Frees memory used by bundles.
*
* @param c decoder context
*/
static av_cold void free_bundles(BinkContext *c)
{
int i;
for (i = 0; i < BINK_NB_SRC; i++)
av_freep(&c->bundle[i].data);
}
/**
* Merges two consequent lists of equal size depending on bits read.
*
* @param gb context for reading bits
* @param dst buffer where merged list will be written to
* @param src pointer to the head of the first list (the second lists starts at src+size)
* @param size input lists size
*/
static void merge(GetBitContext *gb, uint8_t *dst, uint8_t *src, int size)
{
uint8_t *src2 = src + size;
int size2 = size;
do {
if (!get_bits1(gb)) {
*dst++ = *src++;
size--;
} else {
*dst++ = *src2++;
size2--;
}
} while (size && size2);
while (size--)
*dst++ = *src++;
while (size2--)
*dst++ = *src2++;
}
/**
* Reads information about Huffman tree used to decode data.
*
* @param gb context for reading bits
* @param tree pointer for storing tree data
*/
static void read_tree(GetBitContext *gb, Tree *tree)
{
uint8_t tmp1[16], tmp2[16], *in = tmp1, *out = tmp2;
int i, t, len;
tree->vlc_num = get_bits(gb, 4);
if (!tree->vlc_num) {
for (i = 0; i < 16; i++)
tree->syms[i] = i;
return;
}
if (get_bits1(gb)) {
len = get_bits(gb, 3);
memset(tmp1, 0, sizeof(tmp1));
for (i = 0; i <= len; i++) {
tree->syms[i] = get_bits(gb, 4);
tmp1[tree->syms[i]] = 1;
}
for (i = 0; i < 16; i++)
if (!tmp1[i])
tree->syms[++len] = i;
} else {
len = get_bits(gb, 2);
for (i = 0; i < 16; i++)
in[i] = i;
for (i = 0; i <= len; i++) {
int size = 1 << i;
for (t = 0; t < 16; t += size << 1)
merge(gb, out + t, in + t, size);
FFSWAP(uint8_t*, in, out);
}
memcpy(tree->syms, in, 16);
}
}
/**
* Prepares bundle for decoding data.
*
* @param gb context for reading bits
* @param c decoder context
* @param bundle_num number of the bundle to initialize
*/
static void read_bundle(GetBitContext *gb, BinkContext *c, int bundle_num)
{
int i;
if (bundle_num == BINK_SRC_COLORS) {
for (i = 0; i < 16; i++)
read_tree(gb, &c->col_high[i]);
c->col_lastval = 0;
}
if (bundle_num != BINK_SRC_INTRA_DC && bundle_num != BINK_SRC_INTER_DC)
read_tree(gb, &c->bundle[bundle_num].tree);
c->bundle[bundle_num].cur_dec =
c->bundle[bundle_num].cur_ptr = c->bundle[bundle_num].data;
}
/**
* common check before starting decoding bundle data
*
* @param gb context for reading bits
* @param b bundle
* @param t variable where number of elements to decode will be stored
*/
#define CHECK_READ_VAL(gb, b, t) \
if (!b->cur_dec || (b->cur_dec > b->cur_ptr)) \
return 0; \
t = get_bits(gb, b->len); \
if (!t) { \
b->cur_dec = NULL; \
return 0; \
} \
static int read_runs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
{
int t, v;
const uint8_t *dec_end;
CHECK_READ_VAL(gb, b, t);
dec_end = b->cur_dec + t;
if (dec_end > b->data_end) {
av_log(avctx, AV_LOG_ERROR, "Run value went out of bounds\n");
return -1;
}
if (get_bits1(gb)) {
v = get_bits(gb, 4);
memset(b->cur_dec, v, t);
b->cur_dec += t;
} else {
while (b->cur_dec < dec_end)
*b->cur_dec++ = GET_HUFF(gb, b->tree);
}
return 0;
}
static int read_motion_values(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
{
int t, sign, v;
const uint8_t *dec_end;
CHECK_READ_VAL(gb, b, t);
dec_end = b->cur_dec + t;
if (dec_end > b->data_end) {
av_log(avctx, AV_LOG_ERROR, "Too many motion values\n");
return -1;
}
if (get_bits1(gb)) {
v = get_bits(gb, 4);
if (v) {
sign = -get_bits1(gb);
v = (v ^ sign) - sign;
}
memset(b->cur_dec, v, t);
b->cur_dec += t;
} else {
do {
v = GET_HUFF(gb, b->tree);
if (v) {
sign = -get_bits1(gb);
v = (v ^ sign) - sign;
}
*b->cur_dec++ = v;
} while (b->cur_dec < dec_end);
}
return 0;
}
const uint8_t bink_rlelens[4] = { 4, 8, 12, 32 };
static int read_block_types(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
{
int t, v;
int last = 0;
const uint8_t *dec_end;
CHECK_READ_VAL(gb, b, t);
dec_end = b->cur_dec + t;
if (dec_end > b->data_end) {
av_log(avctx, AV_LOG_ERROR, "Too many block type values\n");
return -1;
}
if (get_bits1(gb)) {
v = get_bits(gb, 4);
memset(b->cur_dec, v, t);
b->cur_dec += t;
} else {
do {
v = GET_HUFF(gb, b->tree);
if (v < 12) {
last = v;
*b->cur_dec++ = v;
} else {
int run = bink_rlelens[v - 12];
memset(b->cur_dec, last, run);
b->cur_dec += run;
}
} while (b->cur_dec < dec_end);
}
return 0;
}
static int read_patterns(AVCodecContext *avctx, GetBitContext *gb, Bundle *b)
{
int t, v;
const uint8_t *dec_end;
CHECK_READ_VAL(gb, b, t);
dec_end = b->cur_dec + t;
if (dec_end > b->data_end) {
av_log(avctx, AV_LOG_ERROR, "Too many pattern values\n");
return -1;
}
while (b->cur_dec < dec_end) {
v = GET_HUFF(gb, b->tree);
v |= GET_HUFF(gb, b->tree) << 4;
*b->cur_dec++ = v;
}
return 0;
}
static int read_colors(GetBitContext *gb, Bundle *b, BinkContext *c)
{
int t, sign, v;
const uint8_t *dec_end;
CHECK_READ_VAL(gb, b, t);
dec_end = b->cur_dec + t;
if (dec_end > b->data_end) {
av_log(c->avctx, AV_LOG_ERROR, "Too many color values\n");
return -1;
}
if (get_bits1(gb)) {
c->col_lastval = GET_HUFF(gb, c->col_high[c->col_lastval]);
v = GET_HUFF(gb, b->tree);
v = (c->col_lastval << 4) | v;
if (c->version < 'i') {
sign = ((int8_t) v) >> 7;
v = ((v & 0x7F) ^ sign) - sign;
v += 0x80;
}
memset(b->cur_dec, v, t);
b->cur_dec += t;
} else {
while (b->cur_dec < dec_end) {
c->col_lastval = GET_HUFF(gb, c->col_high[c->col_lastval]);
v = GET_HUFF(gb, b->tree);
v = (c->col_lastval << 4) | v;
if (c->version < 'i') {
sign = ((int8_t) v) >> 7;
v = ((v & 0x7F) ^ sign) - sign;
v += 0x80;
}
*b->cur_dec++ = v;
}
}
return 0;
}
/** number of bits used to store first DC value in bundle */
#define DC_START_BITS 11
static int read_dcs(AVCodecContext *avctx, GetBitContext *gb, Bundle *b,
int start_bits, int has_sign)
{
int i, j, len, len2, bsize, sign, v, v2;
int16_t *dst = (int16_t*)b->cur_dec;
CHECK_READ_VAL(gb, b, len);
v = get_bits(gb, start_bits - has_sign);
if (v && has_sign) {
sign = -get_bits1(gb);
v = (v ^ sign) - sign;
}
*dst++ = v;
len--;
for (i = 0; i < len; i += 8) {
len2 = FFMIN(len - i, 8);
bsize = get_bits(gb, 4);
if (bsize) {
for (j = 0; j < len2; j++) {
v2 = get_bits(gb, bsize);
if (v2) {
sign = -get_bits1(gb);
v2 = (v2 ^ sign) - sign;
}
v += v2;
*dst++ = v;
if (v < -32768 || v > 32767) {
av_log(avctx, AV_LOG_ERROR, "DC value went out of bounds: %d\n", v);
return -1;
}
}
} else {
for (j = 0; j < len2; j++)
*dst++ = v;
}
}
b->cur_dec = (uint8_t*)dst;
return 0;
}
/**
* Retrieves next value from bundle.
*
* @param c decoder context
* @param bundle bundle number
*/
static inline int get_value(BinkContext *c, int bundle)
{
int16_t ret;
if (bundle < BINK_SRC_X_OFF || bundle == BINK_SRC_RUN)
return *c->bundle[bundle].cur_ptr++;
if (bundle == BINK_SRC_X_OFF || bundle == BINK_SRC_Y_OFF)
return (int8_t)*c->bundle[bundle].cur_ptr++;
ret = *(int16_t*)c->bundle[bundle].cur_ptr;
c->bundle[bundle].cur_ptr += 2;
return ret;
}
/**
* Reads 8x8 block of DCT coefficients.
*
* @param gb context for reading bits
* @param block place for storing coefficients
* @param scan scan order table
* @param is_intra tells what set of quantizer matrices to use
* @return 0 for success, negative value in other cases
*/
static int read_dct_coeffs(GetBitContext *gb, DCTELEM block[64], const uint8_t *scan,
int is_intra)
{
int coef_list[128];
int mode_list[128];
int i, t, mask, bits, ccoef, mode, sign;
int list_start = 64, list_end = 64, list_pos;
int coef_count = 0;
int coef_idx[64];
int quant_idx;
const uint32_t *quant;
coef_list[list_end] = 4; mode_list[list_end++] = 0;
coef_list[list_end] = 24; mode_list[list_end++] = 0;
coef_list[list_end] = 44; mode_list[list_end++] = 0;
coef_list[list_end] = 1; mode_list[list_end++] = 3;
coef_list[list_end] = 2; mode_list[list_end++] = 3;
coef_list[list_end] = 3; mode_list[list_end++] = 3;
bits = get_bits(gb, 4) - 1;
for (mask = 1 << bits; bits >= 0; mask >>= 1, bits--) {
list_pos = list_start;
while (list_pos < list_end) {
if (!(mode_list[list_pos] | coef_list[list_pos]) || !get_bits1(gb)) {
list_pos++;
continue;
}
ccoef = coef_list[list_pos];
mode = mode_list[list_pos];
switch (mode) {
case 0:
coef_list[list_pos] = ccoef + 4;
mode_list[list_pos] = 1;
case 2:
if (mode == 2) {
coef_list[list_pos] = 0;
mode_list[list_pos++] = 0;
}
for (i = 0; i < 4; i++, ccoef++) {
if (get_bits1(gb)) {
coef_list[--list_start] = ccoef;
mode_list[ list_start] = 3;
} else {
int t;
if (!bits) {
t = 1 - (get_bits1(gb) << 1);
} else {
t = get_bits(gb, bits) | mask;
sign = -get_bits1(gb);
t = (t ^ sign) - sign;
}
block[scan[ccoef]] = t;
coef_idx[coef_count++] = ccoef;
}
}
break;
case 1:
mode_list[list_pos] = 2;
for (i = 0; i < 3; i++) {
ccoef += 4;
coef_list[list_end] = ccoef;
mode_list[list_end++] = 2;
}
break;
case 3:
if (!bits) {
t = 1 - (get_bits1(gb) << 1);
} else {
t = get_bits(gb, bits) | mask;
sign = -get_bits1(gb);
t = (t ^ sign) - sign;
}
block[scan[ccoef]] = t;
coef_idx[coef_count++] = ccoef;
coef_list[list_pos] = 0;
mode_list[list_pos++] = 0;
break;
}
}
}
quant_idx = get_bits(gb, 4);
quant = is_intra ? bink_intra_quant[quant_idx]
: bink_inter_quant[quant_idx];
block[0] = (block[0] * quant[0]) >> 11;
for (i = 0; i < coef_count; i++) {
int idx = coef_idx[i];
block[scan[idx]] = (block[scan[idx]] * quant[idx]) >> 11;
}
return 0;
}
/**
* Reads 8x8 block with residue after motion compensation.
*
* @param gb context for reading bits
* @param block place to store read data
* @param masks_count number of masks to decode
* @return 0 on success, negative value in other cases
*/
static int read_residue(GetBitContext *gb, DCTELEM block[64], int masks_count)
{
int coef_list[128];
int mode_list[128];
int i, sign, mask, ccoef, mode;
int list_start = 64, list_end = 64, list_pos;
int nz_coeff[64];
int nz_coeff_count = 0;
coef_list[list_end] = 4; mode_list[list_end++] = 0;
coef_list[list_end] = 24; mode_list[list_end++] = 0;
coef_list[list_end] = 44; mode_list[list_end++] = 0;
coef_list[list_end] = 0; mode_list[list_end++] = 2;
for (mask = 1 << get_bits(gb, 3); mask; mask >>= 1) {
for (i = 0; i < nz_coeff_count; i++) {
if (!get_bits1(gb))
continue;
if (block[nz_coeff[i]] < 0)
block[nz_coeff[i]] -= mask;
else
block[nz_coeff[i]] += mask;
masks_count--;
if (masks_count < 0)
return 0;
}
list_pos = list_start;
while (list_pos < list_end) {
if (!(coef_list[list_pos] | mode_list[list_pos]) || !get_bits1(gb)) {
list_pos++;
continue;
}
ccoef = coef_list[list_pos];
mode = mode_list[list_pos];
switch (mode) {
case 0:
coef_list[list_pos] = ccoef + 4;
mode_list[list_pos] = 1;
case 2:
if (mode == 2) {
coef_list[list_pos] = 0;
mode_list[list_pos++] = 0;
}
for (i = 0; i < 4; i++, ccoef++) {
if (get_bits1(gb)) {
coef_list[--list_start] = ccoef;
mode_list[ list_start] = 3;
} else {
nz_coeff[nz_coeff_count++] = bink_scan[ccoef];
sign = -get_bits1(gb);
block[bink_scan[ccoef]] = (mask ^ sign) - sign;
masks_count--;
if (masks_count < 0)
return 0;
}
}
break;
case 1:
mode_list[list_pos] = 2;
for (i = 0; i < 3; i++) {
ccoef += 4;
coef_list[list_end] = ccoef;
mode_list[list_end++] = 2;
}
break;
case 3:
nz_coeff[nz_coeff_count++] = bink_scan[ccoef];
sign = -get_bits1(gb);
block[bink_scan[ccoef]] = (mask ^ sign) - sign;
coef_list[list_pos] = 0;
mode_list[list_pos++] = 0;
masks_count--;
if (masks_count < 0)
return 0;
break;
}
}
}
return 0;
}
static int bink_decode_plane(BinkContext *c, GetBitContext *gb, int plane_idx,
int is_chroma)
{
int blk;
int i, j, bx, by;
uint8_t *dst, *prev, *ref, *ref_start, *ref_end;
int v, col[2];
const uint8_t *scan;
int xoff, yoff;
DECLARE_ALIGNED(16, DCTELEM, block[64]);
DECLARE_ALIGNED(16, uint8_t, ublock[64]);
int coordmap[64];
const int stride = c->pic.linesize[plane_idx];
int bw = is_chroma ? (c->avctx->width + 15) >> 4 : (c->avctx->width + 7) >> 3;
int bh = is_chroma ? (c->avctx->height + 15) >> 4 : (c->avctx->height + 7) >> 3;
int width = c->avctx->width >> is_chroma;
init_lengths(c, FFMAX(width, 8), bw);
for (i = 0; i < BINK_NB_SRC; i++)
read_bundle(gb, c, i);
ref_start = c->last.data[plane_idx];
ref_end = c->last.data[plane_idx]
+ (bw - 1 + c->last.linesize[plane_idx] * (bh - 1)) * 8;
for (i = 0; i < 64; i++)
coordmap[i] = (i & 7) + (i >> 3) * stride;
for (by = 0; by < bh; by++) {
if (read_block_types(c->avctx, gb, &c->bundle[BINK_SRC_BLOCK_TYPES]) < 0)
return -1;
if (read_block_types(c->avctx, gb, &c->bundle[BINK_SRC_SUB_BLOCK_TYPES]) < 0)
return -1;
if (read_colors(gb, &c->bundle[BINK_SRC_COLORS], c) < 0)
return -1;
if (read_patterns(c->avctx, gb, &c->bundle[BINK_SRC_PATTERN]) < 0)
return -1;
if (read_motion_values(c->avctx, gb, &c->bundle[BINK_SRC_X_OFF]) < 0)
return -1;
if (read_motion_values(c->avctx, gb, &c->bundle[BINK_SRC_Y_OFF]) < 0)
return -1;
if (read_dcs(c->avctx, gb, &c->bundle[BINK_SRC_INTRA_DC], DC_START_BITS, 0) < 0)
return -1;
if (read_dcs(c->avctx, gb, &c->bundle[BINK_SRC_INTER_DC], DC_START_BITS, 1) < 0)
return -1;
if (read_runs(c->avctx, gb, &c->bundle[BINK_SRC_RUN]) < 0)
return -1;
if (by == bh)
break;
dst = c->pic.data[plane_idx] + 8*by*stride;
prev = c->last.data[plane_idx] + 8*by*stride;
for (bx = 0; bx < bw; bx++, dst += 8, prev += 8) {
blk = get_value(c, BINK_SRC_BLOCK_TYPES);
// 16x16 block type on odd line means part of the already decoded block, so skip it
if ((by & 1) && blk == SCALED_BLOCK) {
bx++;
dst += 8;
prev += 8;
continue;
}
switch (blk) {
case SKIP_BLOCK:
c->dsp.put_pixels_tab[1][0](dst, prev, stride, 8);
break;
case SCALED_BLOCK:
blk = get_value(c, BINK_SRC_SUB_BLOCK_TYPES);
switch (blk) {
case RUN_BLOCK:
scan = bink_patterns[get_bits(gb, 4)];
i = 0;
do {
int run = get_value(c, BINK_SRC_RUN) + 1;
i += run;
if (i > 64) {
av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
return -1;
}
if (get_bits1(gb)) {
v = get_value(c, BINK_SRC_COLORS);
for (j = 0; j < run; j++)
ublock[*scan++] = v;
} else {
for (j = 0; j < run; j++)
ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
}
} while (i < 63);
if (i == 63)
ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
break;
case INTRA_BLOCK:
c->dsp.clear_block(block);
block[0] = get_value(c, BINK_SRC_INTRA_DC);
read_dct_coeffs(gb, block, c->scantable.permutated, 1);
c->dsp.idct(block);
c->dsp.put_pixels_nonclamped(block, ublock, 8);
break;
case FILL_BLOCK:
v = get_value(c, BINK_SRC_COLORS);
c->dsp.fill_block_tab[0](dst, v, stride, 16);
break;
case PATTERN_BLOCK:
for (i = 0; i < 2; i++)
col[i] = get_value(c, BINK_SRC_COLORS);
for (j = 0; j < 8; j++) {
v = get_value(c, BINK_SRC_PATTERN);
for (i = 0; i < 8; i++, v >>= 1)
ublock[i + j*8] = col[v & 1];
}
break;
case RAW_BLOCK:
for (j = 0; j < 8; j++)
for (i = 0; i < 8; i++)
ublock[i + j*8] = get_value(c, BINK_SRC_COLORS);
break;
default:
av_log(c->avctx, AV_LOG_ERROR, "Incorrect 16x16 block type %d\n", blk);
return -1;
}
if (blk != FILL_BLOCK)
c->dsp.scale_block(ublock, dst, stride);
bx++;
dst += 8;
prev += 8;
break;
case MOTION_BLOCK:
xoff = get_value(c, BINK_SRC_X_OFF);
yoff = get_value(c, BINK_SRC_Y_OFF);
ref = prev + xoff + yoff * stride;
if (ref < ref_start || ref > ref_end) {
av_log(c->avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n",
bx*8 + xoff, by*8 + yoff);
return -1;
}
c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
break;
case RUN_BLOCK:
scan = bink_patterns[get_bits(gb, 4)];
i = 0;
do {
int run = get_value(c, BINK_SRC_RUN) + 1;
i += run;
if (i > 64) {
av_log(c->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
return -1;
}
if (get_bits1(gb)) {
v = get_value(c, BINK_SRC_COLORS);
for (j = 0; j < run; j++)
dst[coordmap[*scan++]] = v;
} else {
for (j = 0; j < run; j++)
dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
}
} while (i < 63);
if (i == 63)
dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
break;
case RESIDUE_BLOCK:
xoff = get_value(c, BINK_SRC_X_OFF);
yoff = get_value(c, BINK_SRC_Y_OFF);
ref = prev + xoff + yoff * stride;
if (ref < ref_start || ref > ref_end) {
av_log(c->avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n",
bx*8 + xoff, by*8 + yoff);
return -1;
}
c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
c->dsp.clear_block(block);
v = get_bits(gb, 7);
read_residue(gb, block, v);
c->dsp.add_pixels8(dst, block, stride);
break;
case INTRA_BLOCK:
c->dsp.clear_block(block);
block[0] = get_value(c, BINK_SRC_INTRA_DC);
read_dct_coeffs(gb, block, c->scantable.permutated, 1);
c->dsp.idct_put(dst, stride, block);
break;
case FILL_BLOCK:
v = get_value(c, BINK_SRC_COLORS);
c->dsp.fill_block_tab[1](dst, v, stride, 8);
break;
case INTER_BLOCK:
xoff = get_value(c, BINK_SRC_X_OFF);
yoff = get_value(c, BINK_SRC_Y_OFF);
ref = prev + xoff + yoff * stride;
c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
c->dsp.clear_block(block);
block[0] = get_value(c, BINK_SRC_INTER_DC);
read_dct_coeffs(gb, block, c->scantable.permutated, 0);
c->dsp.idct_add(dst, stride, block);
break;
case PATTERN_BLOCK:
for (i = 0; i < 2; i++)
col[i] = get_value(c, BINK_SRC_COLORS);
for (i = 0; i < 8; i++) {
v = get_value(c, BINK_SRC_PATTERN);
for (j = 0; j < 8; j++, v >>= 1)
dst[i*stride + j] = col[v & 1];
}
break;
case RAW_BLOCK:
for (i = 0; i < 8; i++)
memcpy(dst + i*stride, c->bundle[BINK_SRC_COLORS].cur_ptr + i*8, 8);
c->bundle[BINK_SRC_COLORS].cur_ptr += 64;
break;
default:
av_log(c->avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk);
return -1;
}
}
}
if (get_bits_count(gb) & 0x1F) //next plane data starts at 32-bit boundary
skip_bits_long(gb, 32 - (get_bits_count(gb) & 0x1F));
return 0;
}
static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *pkt)
{
BinkContext * const c = avctx->priv_data;
GetBitContext gb;
int plane, plane_idx;
int bits_count = pkt->size << 3;
if(c->pic.data[0])
avctx->release_buffer(avctx, &c->pic);
if(avctx->get_buffer(avctx, &c->pic) < 0){
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
return -1;
}
init_get_bits(&gb, pkt->data, bits_count);
if (c->has_alpha) {
if (c->version >= 'i')
skip_bits_long(&gb, 32);
if (bink_decode_plane(c, &gb, 3, 0) < 0)
return -1;
}
if (c->version >= 'i')
skip_bits_long(&gb, 32);
for (plane = 0; plane < 3; plane++) {
plane_idx = (!plane || !c->swap_planes) ? plane : (plane ^ 3);
if (bink_decode_plane(c, &gb, plane_idx, !!plane) < 0)
return -1;
if (get_bits_count(&gb) >= bits_count)
break;
}
emms_c();
*data_size = sizeof(AVFrame);
*(AVFrame*)data = c->pic;
FFSWAP(AVFrame, c->pic, c->last);
/* always report that the buffer was completely consumed */
return pkt->size;
}
static av_cold int decode_init(AVCodecContext *avctx)
{
BinkContext * const c = avctx->priv_data;
static VLC_TYPE table[16 * 128][2];
int i;
int flags;
c->version = avctx->codec_tag >> 24;
if (c->version < 'c') {
av_log(avctx, AV_LOG_ERROR, "Too old version '%c'\n", c->version);
return -1;
}
if (avctx->extradata_size < 4) {
av_log(avctx, AV_LOG_ERROR, "Extradata missing or too short\n");
return -1;
}
flags = AV_RL32(avctx->extradata);
c->has_alpha = flags & BINK_FLAG_ALPHA;
c->swap_planes = c->version >= 'h';
if (!bink_trees[15].table) {
for (i = 0; i < 16; i++) {
const int maxbits = bink_tree_lens[i][15];
bink_trees[i].table = table + i*128;
bink_trees[i].table_allocated = 1 << maxbits;
init_vlc(&bink_trees[i], maxbits, 16,
bink_tree_lens[i], 1, 1,
bink_tree_bits[i], 1, 1, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
}
}
c->avctx = avctx;
c->pic.data[0] = NULL;
if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
return 1;
}
avctx->pix_fmt = c->has_alpha ? PIX_FMT_YUVA420P : PIX_FMT_YUV420P;
avctx->idct_algo = FF_IDCT_BINK;
dsputil_init(&c->dsp, avctx);
ff_init_scantable(c->dsp.idct_permutation, &c->scantable, bink_scan);
init_bundles(c);
return 0;
}
static av_cold int decode_end(AVCodecContext *avctx)
{
BinkContext * const c = avctx->priv_data;
if (c->pic.data[0])
avctx->release_buffer(avctx, &c->pic);
if (c->last.data[0])
avctx->release_buffer(avctx, &c->last);
free_bundles(c);
return 0;
}
AVCodec bink_decoder = {
"binkvideo",
AVMEDIA_TYPE_VIDEO,
CODEC_ID_BINKVIDEO,
sizeof(BinkContext),
decode_init,
NULL,
decode_end,
decode_frame,
.long_name = NULL_IF_CONFIG_SMALL("Bink video"),
};
| 123linslouis-android-video-cutter | jni/libavcodec/bink.c | C | asf20 | 33,029 |
/*
* DVB subtitle decoding for ffmpeg
* Copyright (c) 2005 Ian Caulfield
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "avcodec.h"
#include "dsputil.h"
#include "get_bits.h"
#include "colorspace.h"
//#define DEBUG
//#define DEBUG_PACKET_CONTENTS
//#define DEBUG_SAVE_IMAGES
#define DVBSUB_PAGE_SEGMENT 0x10
#define DVBSUB_REGION_SEGMENT 0x11
#define DVBSUB_CLUT_SEGMENT 0x12
#define DVBSUB_OBJECT_SEGMENT 0x13
#define DVBSUB_DISPLAY_SEGMENT 0x80
#define cm (ff_cropTbl + MAX_NEG_CROP)
#ifdef DEBUG_SAVE_IMAGES
#undef fprintf
#if 0
static void png_save(const char *filename, uint8_t *bitmap, int w, int h,
uint32_t *rgba_palette)
{
int x, y, v;
FILE *f;
char fname[40], fname2[40];
char command[1024];
snprintf(fname, 40, "%s.ppm", filename);
f = fopen(fname, "w");
if (!f) {
perror(fname);
exit(1);
}
fprintf(f, "P6\n"
"%d %d\n"
"%d\n",
w, h, 255);
for(y = 0; y < h; y++) {
for(x = 0; x < w; x++) {
v = rgba_palette[bitmap[y * w + x]];
putc((v >> 16) & 0xff, f);
putc((v >> 8) & 0xff, f);
putc((v >> 0) & 0xff, f);
}
}
fclose(f);
snprintf(fname2, 40, "%s-a.pgm", filename);
f = fopen(fname2, "w");
if (!f) {
perror(fname2);
exit(1);
}
fprintf(f, "P5\n"
"%d %d\n"
"%d\n",
w, h, 255);
for(y = 0; y < h; y++) {
for(x = 0; x < w; x++) {
v = rgba_palette[bitmap[y * w + x]];
putc((v >> 24) & 0xff, f);
}
}
fclose(f);
snprintf(command, 1024, "pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
system(command);
snprintf(command, 1024, "rm %s %s", fname, fname2);
system(command);
}
#endif
static void png_save2(const char *filename, uint32_t *bitmap, int w, int h)
{
int x, y, v;
FILE *f;
char fname[40], fname2[40];
char command[1024];
snprintf(fname, sizeof(fname), "%s.ppm", filename);
f = fopen(fname, "w");
if (!f) {
perror(fname);
exit(1);
}
fprintf(f, "P6\n"
"%d %d\n"
"%d\n",
w, h, 255);
for(y = 0; y < h; y++) {
for(x = 0; x < w; x++) {
v = bitmap[y * w + x];
putc((v >> 16) & 0xff, f);
putc((v >> 8) & 0xff, f);
putc((v >> 0) & 0xff, f);
}
}
fclose(f);
snprintf(fname2, sizeof(fname2), "%s-a.pgm", filename);
f = fopen(fname2, "w");
if (!f) {
perror(fname2);
exit(1);
}
fprintf(f, "P5\n"
"%d %d\n"
"%d\n",
w, h, 255);
for(y = 0; y < h; y++) {
for(x = 0; x < w; x++) {
v = bitmap[y * w + x];
putc((v >> 24) & 0xff, f);
}
}
fclose(f);
snprintf(command, sizeof(command), "pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
system(command);
snprintf(command, sizeof(command), "rm %s %s", fname, fname2);
system(command);
}
#endif
#define RGBA(r,g,b,a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
typedef struct DVBSubCLUT {
int id;
uint32_t clut4[4];
uint32_t clut16[16];
uint32_t clut256[256];
struct DVBSubCLUT *next;
} DVBSubCLUT;
static DVBSubCLUT default_clut;
typedef struct DVBSubObjectDisplay {
int object_id;
int region_id;
int x_pos;
int y_pos;
int fgcolor;
int bgcolor;
struct DVBSubObjectDisplay *region_list_next;
struct DVBSubObjectDisplay *object_list_next;
} DVBSubObjectDisplay;
typedef struct DVBSubObject {
int id;
int type;
DVBSubObjectDisplay *display_list;
struct DVBSubObject *next;
} DVBSubObject;
typedef struct DVBSubRegionDisplay {
int region_id;
int x_pos;
int y_pos;
struct DVBSubRegionDisplay *next;
} DVBSubRegionDisplay;
typedef struct DVBSubRegion {
int id;
int width;
int height;
int depth;
int clut;
int bgcolor;
uint8_t *pbuf;
int buf_size;
DVBSubObjectDisplay *display_list;
struct DVBSubRegion *next;
} DVBSubRegion;
typedef struct DVBSubContext {
int composition_id;
int ancillary_id;
int time_out;
DVBSubRegion *region_list;
DVBSubCLUT *clut_list;
DVBSubObject *object_list;
int display_list_size;
DVBSubRegionDisplay *display_list;
} DVBSubContext;
static DVBSubObject* get_object(DVBSubContext *ctx, int object_id)
{
DVBSubObject *ptr = ctx->object_list;
while (ptr && ptr->id != object_id) {
ptr = ptr->next;
}
return ptr;
}
static DVBSubCLUT* get_clut(DVBSubContext *ctx, int clut_id)
{
DVBSubCLUT *ptr = ctx->clut_list;
while (ptr && ptr->id != clut_id) {
ptr = ptr->next;
}
return ptr;
}
static DVBSubRegion* get_region(DVBSubContext *ctx, int region_id)
{
DVBSubRegion *ptr = ctx->region_list;
while (ptr && ptr->id != region_id) {
ptr = ptr->next;
}
return ptr;
}
static void delete_region_display_list(DVBSubContext *ctx, DVBSubRegion *region)
{
DVBSubObject *object, *obj2, **obj2_ptr;
DVBSubObjectDisplay *display, *obj_disp, **obj_disp_ptr;
while (region->display_list) {
display = region->display_list;
object = get_object(ctx, display->object_id);
if (object) {
obj_disp_ptr = &object->display_list;
obj_disp = *obj_disp_ptr;
while (obj_disp && obj_disp != display) {
obj_disp_ptr = &obj_disp->object_list_next;
obj_disp = *obj_disp_ptr;
}
if (obj_disp) {
*obj_disp_ptr = obj_disp->object_list_next;
if (!object->display_list) {
obj2_ptr = &ctx->object_list;
obj2 = *obj2_ptr;
while (obj2 != object) {
assert(obj2);
obj2_ptr = &obj2->next;
obj2 = *obj2_ptr;
}
*obj2_ptr = obj2->next;
av_free(obj2);
}
}
}
region->display_list = display->region_list_next;
av_free(display);
}
}
static void delete_state(DVBSubContext *ctx)
{
DVBSubRegion *region;
DVBSubCLUT *clut;
while (ctx->region_list) {
region = ctx->region_list;
ctx->region_list = region->next;
delete_region_display_list(ctx, region);
if (region->pbuf)
av_free(region->pbuf);
av_free(region);
}
while (ctx->clut_list) {
clut = ctx->clut_list;
ctx->clut_list = clut->next;
av_free(clut);
}
/* Should already be null */
if (ctx->object_list)
av_log(0, AV_LOG_ERROR, "Memory deallocation error!\n");
}
static av_cold int dvbsub_init_decoder(AVCodecContext *avctx)
{
int i, r, g, b, a = 0;
DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
memset(avctx->priv_data, 0, sizeof(DVBSubContext));
ctx->composition_id = avctx->sub_id & 0xffff;
ctx->ancillary_id = avctx->sub_id >> 16;
default_clut.id = -1;
default_clut.next = NULL;
default_clut.clut4[0] = RGBA( 0, 0, 0, 0);
default_clut.clut4[1] = RGBA(255, 255, 255, 255);
default_clut.clut4[2] = RGBA( 0, 0, 0, 255);
default_clut.clut4[3] = RGBA(127, 127, 127, 255);
default_clut.clut16[0] = RGBA( 0, 0, 0, 0);
for (i = 1; i < 16; i++) {
if (i < 8) {
r = (i & 1) ? 255 : 0;
g = (i & 2) ? 255 : 0;
b = (i & 4) ? 255 : 0;
} else {
r = (i & 1) ? 127 : 0;
g = (i & 2) ? 127 : 0;
b = (i & 4) ? 127 : 0;
}
default_clut.clut16[i] = RGBA(r, g, b, 255);
}
default_clut.clut256[0] = RGBA( 0, 0, 0, 0);
for (i = 1; i < 256; i++) {
if (i < 8) {
r = (i & 1) ? 255 : 0;
g = (i & 2) ? 255 : 0;
b = (i & 4) ? 255 : 0;
a = 63;
} else {
switch (i & 0x88) {
case 0x00:
r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
a = 255;
break;
case 0x08:
r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
a = 127;
break;
case 0x80:
r = 127 + ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
g = 127 + ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
b = 127 + ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
a = 255;
break;
case 0x88:
r = ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
g = ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
b = ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
a = 255;
break;
}
}
default_clut.clut256[i] = RGBA(r, g, b, a);
}
return 0;
}
static av_cold int dvbsub_close_decoder(AVCodecContext *avctx)
{
DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
DVBSubRegionDisplay *display;
delete_state(ctx);
while (ctx->display_list) {
display = ctx->display_list;
ctx->display_list = display->next;
av_free(display);
}
return 0;
}
static int dvbsub_read_2bit_string(uint8_t *destbuf, int dbuf_len,
const uint8_t **srcbuf, int buf_size,
int non_mod, uint8_t *map_table)
{
GetBitContext gb;
int bits;
int run_length;
int pixels_read = 0;
init_get_bits(&gb, *srcbuf, buf_size << 3);
while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
bits = get_bits(&gb, 2);
if (bits) {
if (non_mod != 1 || bits != 1) {
if (map_table)
*destbuf++ = map_table[bits];
else
*destbuf++ = bits;
}
pixels_read++;
} else {
bits = get_bits1(&gb);
if (bits == 1) {
run_length = get_bits(&gb, 3) + 3;
bits = get_bits(&gb, 2);
if (non_mod == 1 && bits == 1)
pixels_read += run_length;
else {
if (map_table)
bits = map_table[bits];
while (run_length-- > 0 && pixels_read < dbuf_len) {
*destbuf++ = bits;
pixels_read++;
}
}
} else {
bits = get_bits1(&gb);
if (bits == 0) {
bits = get_bits(&gb, 2);
if (bits == 2) {
run_length = get_bits(&gb, 4) + 12;
bits = get_bits(&gb, 2);
if (non_mod == 1 && bits == 1)
pixels_read += run_length;
else {
if (map_table)
bits = map_table[bits];
while (run_length-- > 0 && pixels_read < dbuf_len) {
*destbuf++ = bits;
pixels_read++;
}
}
} else if (bits == 3) {
run_length = get_bits(&gb, 8) + 29;
bits = get_bits(&gb, 2);
if (non_mod == 1 && bits == 1)
pixels_read += run_length;
else {
if (map_table)
bits = map_table[bits];
while (run_length-- > 0 && pixels_read < dbuf_len) {
*destbuf++ = bits;
pixels_read++;
}
}
} else if (bits == 1) {
pixels_read += 2;
if (map_table)
bits = map_table[0];
else
bits = 0;
if (pixels_read <= dbuf_len) {
*destbuf++ = bits;
*destbuf++ = bits;
}
} else {
(*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
return pixels_read;
}
} else {
if (map_table)
bits = map_table[0];
else
bits = 0;
*destbuf++ = bits;
pixels_read++;
}
}
}
}
if (get_bits(&gb, 6))
av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
(*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
return pixels_read;
}
static int dvbsub_read_4bit_string(uint8_t *destbuf, int dbuf_len,
const uint8_t **srcbuf, int buf_size,
int non_mod, uint8_t *map_table)
{
GetBitContext gb;
int bits;
int run_length;
int pixels_read = 0;
init_get_bits(&gb, *srcbuf, buf_size << 3);
while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
bits = get_bits(&gb, 4);
if (bits) {
if (non_mod != 1 || bits != 1) {
if (map_table)
*destbuf++ = map_table[bits];
else
*destbuf++ = bits;
}
pixels_read++;
} else {
bits = get_bits1(&gb);
if (bits == 0) {
run_length = get_bits(&gb, 3);
if (run_length == 0) {
(*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
return pixels_read;
}
run_length += 2;
if (map_table)
bits = map_table[0];
else
bits = 0;
while (run_length-- > 0 && pixels_read < dbuf_len) {
*destbuf++ = bits;
pixels_read++;
}
} else {
bits = get_bits1(&gb);
if (bits == 0) {
run_length = get_bits(&gb, 2) + 4;
bits = get_bits(&gb, 4);
if (non_mod == 1 && bits == 1)
pixels_read += run_length;
else {
if (map_table)
bits = map_table[bits];
while (run_length-- > 0 && pixels_read < dbuf_len) {
*destbuf++ = bits;
pixels_read++;
}
}
} else {
bits = get_bits(&gb, 2);
if (bits == 2) {
run_length = get_bits(&gb, 4) + 9;
bits = get_bits(&gb, 4);
if (non_mod == 1 && bits == 1)
pixels_read += run_length;
else {
if (map_table)
bits = map_table[bits];
while (run_length-- > 0 && pixels_read < dbuf_len) {
*destbuf++ = bits;
pixels_read++;
}
}
} else if (bits == 3) {
run_length = get_bits(&gb, 8) + 25;
bits = get_bits(&gb, 4);
if (non_mod == 1 && bits == 1)
pixels_read += run_length;
else {
if (map_table)
bits = map_table[bits];
while (run_length-- > 0 && pixels_read < dbuf_len) {
*destbuf++ = bits;
pixels_read++;
}
}
} else if (bits == 1) {
pixels_read += 2;
if (map_table)
bits = map_table[0];
else
bits = 0;
if (pixels_read <= dbuf_len) {
*destbuf++ = bits;
*destbuf++ = bits;
}
} else {
if (map_table)
bits = map_table[0];
else
bits = 0;
*destbuf++ = bits;
pixels_read ++;
}
}
}
}
}
if (get_bits(&gb, 8))
av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
(*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
return pixels_read;
}
static int dvbsub_read_8bit_string(uint8_t *destbuf, int dbuf_len,
const uint8_t **srcbuf, int buf_size,
int non_mod, uint8_t *map_table)
{
const uint8_t *sbuf_end = (*srcbuf) + buf_size;
int bits;
int run_length;
int pixels_read = 0;
while (*srcbuf < sbuf_end && pixels_read < dbuf_len) {
bits = *(*srcbuf)++;
if (bits) {
if (non_mod != 1 || bits != 1) {
if (map_table)
*destbuf++ = map_table[bits];
else
*destbuf++ = bits;
}
pixels_read++;
} else {
bits = *(*srcbuf)++;
run_length = bits & 0x7f;
if ((bits & 0x80) == 0) {
if (run_length == 0) {
return pixels_read;
}
if (map_table)
bits = map_table[0];
else
bits = 0;
while (run_length-- > 0 && pixels_read < dbuf_len) {
*destbuf++ = bits;
pixels_read++;
}
} else {
bits = *(*srcbuf)++;
if (non_mod == 1 && bits == 1)
pixels_read += run_length;
if (map_table)
bits = map_table[bits];
else while (run_length-- > 0 && pixels_read < dbuf_len) {
*destbuf++ = bits;
pixels_read++;
}
}
}
}
if (*(*srcbuf)++)
av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
return pixels_read;
}
static void dvbsub_parse_pixel_data_block(AVCodecContext *avctx, DVBSubObjectDisplay *display,
const uint8_t *buf, int buf_size, int top_bottom, int non_mod)
{
DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
DVBSubRegion *region = get_region(ctx, display->region_id);
const uint8_t *buf_end = buf + buf_size;
uint8_t *pbuf;
int x_pos, y_pos;
int i;
uint8_t map2to4[] = { 0x0, 0x7, 0x8, 0xf};
uint8_t map2to8[] = {0x00, 0x77, 0x88, 0xff};
uint8_t map4to8[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
uint8_t *map_table;
dprintf(avctx, "DVB pixel block size %d, %s field:\n", buf_size,
top_bottom ? "bottom" : "top");
#ifdef DEBUG_PACKET_CONTENTS
for (i = 0; i < buf_size; i++) {
if (i % 16 == 0)
av_log(avctx, AV_LOG_INFO, "0x%08p: ", buf+i);
av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
if (i % 16 == 15)
av_log(avctx, AV_LOG_INFO, "\n");
}
if (i % 16)
av_log(avctx, AV_LOG_INFO, "\n");
#endif
if (region == 0)
return;
pbuf = region->pbuf;
x_pos = display->x_pos;
y_pos = display->y_pos;
if ((y_pos & 1) != top_bottom)
y_pos++;
while (buf < buf_end) {
if (x_pos > region->width || y_pos > region->height) {
av_log(avctx, AV_LOG_ERROR, "Invalid object location!\n");
return;
}
switch (*buf++) {
case 0x10:
if (region->depth == 8)
map_table = map2to8;
else if (region->depth == 4)
map_table = map2to4;
else
map_table = NULL;
x_pos += dvbsub_read_2bit_string(pbuf + (y_pos * region->width) + x_pos,
region->width - x_pos, &buf, buf_size,
non_mod, map_table);
break;
case 0x11:
if (region->depth < 4) {
av_log(avctx, AV_LOG_ERROR, "4-bit pixel string in %d-bit region!\n", region->depth);
return;
}
if (region->depth == 8)
map_table = map4to8;
else
map_table = NULL;
x_pos += dvbsub_read_4bit_string(pbuf + (y_pos * region->width) + x_pos,
region->width - x_pos, &buf, buf_size,
non_mod, map_table);
break;
case 0x12:
if (region->depth < 8) {
av_log(avctx, AV_LOG_ERROR, "8-bit pixel string in %d-bit region!\n", region->depth);
return;
}
x_pos += dvbsub_read_8bit_string(pbuf + (y_pos * region->width) + x_pos,
region->width - x_pos, &buf, buf_size,
non_mod, NULL);
break;
case 0x20:
map2to4[0] = (*buf) >> 4;
map2to4[1] = (*buf++) & 0xf;
map2to4[2] = (*buf) >> 4;
map2to4[3] = (*buf++) & 0xf;
break;
case 0x21:
for (i = 0; i < 4; i++)
map2to8[i] = *buf++;
break;
case 0x22:
for (i = 0; i < 16; i++)
map4to8[i] = *buf++;
break;
case 0xf0:
x_pos = display->x_pos;
y_pos += 2;
break;
default:
av_log(avctx, AV_LOG_INFO, "Unknown/unsupported pixel block 0x%x\n", *(buf-1));
}
}
}
static void dvbsub_parse_object_segment(AVCodecContext *avctx,
const uint8_t *buf, int buf_size)
{
DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
const uint8_t *buf_end = buf + buf_size;
const uint8_t *block;
int object_id;
DVBSubObject *object;
DVBSubObjectDisplay *display;
int top_field_len, bottom_field_len;
int coding_method, non_modifying_color;
object_id = AV_RB16(buf);
buf += 2;
object = get_object(ctx, object_id);
if (!object)
return;
coding_method = ((*buf) >> 2) & 3;
non_modifying_color = ((*buf++) >> 1) & 1;
if (coding_method == 0) {
top_field_len = AV_RB16(buf);
buf += 2;
bottom_field_len = AV_RB16(buf);
buf += 2;
if (buf + top_field_len + bottom_field_len > buf_end) {
av_log(avctx, AV_LOG_ERROR, "Field data size too large\n");
return;
}
for (display = object->display_list; display; display = display->object_list_next) {
block = buf;
dvbsub_parse_pixel_data_block(avctx, display, block, top_field_len, 0,
non_modifying_color);
if (bottom_field_len > 0)
block = buf + top_field_len;
else
bottom_field_len = top_field_len;
dvbsub_parse_pixel_data_block(avctx, display, block, bottom_field_len, 1,
non_modifying_color);
}
/* } else if (coding_method == 1) {*/
} else {
av_log(avctx, AV_LOG_ERROR, "Unknown object coding %d\n", coding_method);
}
}
static void dvbsub_parse_clut_segment(AVCodecContext *avctx,
const uint8_t *buf, int buf_size)
{
DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
const uint8_t *buf_end = buf + buf_size;
int clut_id;
DVBSubCLUT *clut;
int entry_id, depth , full_range;
int y, cr, cb, alpha;
int r, g, b, r_add, g_add, b_add;
#ifdef DEBUG_PACKET_CONTENTS
int i;
av_log(avctx, AV_LOG_INFO, "DVB clut packet:\n");
for (i=0; i < buf_size; i++) {
av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
if (i % 16 == 15)
av_log(avctx, AV_LOG_INFO, "\n");
}
if (i % 16)
av_log(avctx, AV_LOG_INFO, "\n");
#endif
clut_id = *buf++;
buf += 1;
clut = get_clut(ctx, clut_id);
if (!clut) {
clut = av_malloc(sizeof(DVBSubCLUT));
memcpy(clut, &default_clut, sizeof(DVBSubCLUT));
clut->id = clut_id;
clut->next = ctx->clut_list;
ctx->clut_list = clut;
}
while (buf + 4 < buf_end) {
entry_id = *buf++;
depth = (*buf) & 0xe0;
if (depth == 0) {
av_log(avctx, AV_LOG_ERROR, "Invalid clut depth 0x%x!\n", *buf);
return;
}
full_range = (*buf++) & 1;
if (full_range) {
y = *buf++;
cr = *buf++;
cb = *buf++;
alpha = *buf++;
} else {
y = buf[0] & 0xfc;
cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
cb = (buf[1] << 2) & 0xf0;
alpha = (buf[1] << 6) & 0xc0;
buf += 2;
}
if (y == 0)
alpha = 0xff;
YUV_TO_RGB1_CCIR(cb, cr);
YUV_TO_RGB2_CCIR(r, g, b, y);
dprintf(avctx, "clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha);
if (depth & 0x80)
clut->clut4[entry_id] = RGBA(r,g,b,255 - alpha);
if (depth & 0x40)
clut->clut16[entry_id] = RGBA(r,g,b,255 - alpha);
if (depth & 0x20)
clut->clut256[entry_id] = RGBA(r,g,b,255 - alpha);
}
}
static void dvbsub_parse_region_segment(AVCodecContext *avctx,
const uint8_t *buf, int buf_size)
{
DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
const uint8_t *buf_end = buf + buf_size;
int region_id, object_id;
DVBSubRegion *region;
DVBSubObject *object;
DVBSubObjectDisplay *display;
int fill;
if (buf_size < 10)
return;
region_id = *buf++;
region = get_region(ctx, region_id);
if (!region) {
region = av_mallocz(sizeof(DVBSubRegion));
region->id = region_id;
region->next = ctx->region_list;
ctx->region_list = region;
}
fill = ((*buf++) >> 3) & 1;
region->width = AV_RB16(buf);
buf += 2;
region->height = AV_RB16(buf);
buf += 2;
if (region->width * region->height != region->buf_size) {
if (region->pbuf)
av_free(region->pbuf);
region->buf_size = region->width * region->height;
region->pbuf = av_malloc(region->buf_size);
fill = 1;
}
region->depth = 1 << (((*buf++) >> 2) & 7);
if(region->depth<2 || region->depth>8){
av_log(avctx, AV_LOG_ERROR, "region depth %d is invalid\n", region->depth);
region->depth= 4;
}
region->clut = *buf++;
if (region->depth == 8)
region->bgcolor = *buf++;
else {
buf += 1;
if (region->depth == 4)
region->bgcolor = (((*buf++) >> 4) & 15);
else
region->bgcolor = (((*buf++) >> 2) & 3);
}
dprintf(avctx, "Region %d, (%dx%d)\n", region_id, region->width, region->height);
if (fill) {
memset(region->pbuf, region->bgcolor, region->buf_size);
dprintf(avctx, "Fill region (%d)\n", region->bgcolor);
}
delete_region_display_list(ctx, region);
while (buf + 5 < buf_end) {
object_id = AV_RB16(buf);
buf += 2;
object = get_object(ctx, object_id);
if (!object) {
object = av_mallocz(sizeof(DVBSubObject));
object->id = object_id;
object->next = ctx->object_list;
ctx->object_list = object;
}
object->type = (*buf) >> 6;
display = av_mallocz(sizeof(DVBSubObjectDisplay));
display->object_id = object_id;
display->region_id = region_id;
display->x_pos = AV_RB16(buf) & 0xfff;
buf += 2;
display->y_pos = AV_RB16(buf) & 0xfff;
buf += 2;
if ((object->type == 1 || object->type == 2) && buf+1 < buf_end) {
display->fgcolor = *buf++;
display->bgcolor = *buf++;
}
display->region_list_next = region->display_list;
region->display_list = display;
display->object_list_next = object->display_list;
object->display_list = display;
}
}
static void dvbsub_parse_page_segment(AVCodecContext *avctx,
const uint8_t *buf, int buf_size)
{
DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
DVBSubRegionDisplay *display;
DVBSubRegionDisplay *tmp_display_list, **tmp_ptr;
const uint8_t *buf_end = buf + buf_size;
int region_id;
int page_state;
if (buf_size < 1)
return;
ctx->time_out = *buf++;
page_state = ((*buf++) >> 2) & 3;
dprintf(avctx, "Page time out %ds, state %d\n", ctx->time_out, page_state);
if (page_state == 2) {
delete_state(ctx);
}
tmp_display_list = ctx->display_list;
ctx->display_list = NULL;
ctx->display_list_size = 0;
while (buf + 5 < buf_end) {
region_id = *buf++;
buf += 1;
display = tmp_display_list;
tmp_ptr = &tmp_display_list;
while (display && display->region_id != region_id) {
tmp_ptr = &display->next;
display = display->next;
}
if (!display)
display = av_mallocz(sizeof(DVBSubRegionDisplay));
display->region_id = region_id;
display->x_pos = AV_RB16(buf);
buf += 2;
display->y_pos = AV_RB16(buf);
buf += 2;
*tmp_ptr = display->next;
display->next = ctx->display_list;
ctx->display_list = display;
ctx->display_list_size++;
dprintf(avctx, "Region %d, (%d,%d)\n", region_id, display->x_pos, display->y_pos);
}
while (tmp_display_list) {
display = tmp_display_list;
tmp_display_list = display->next;
av_free(display);
}
}
#ifdef DEBUG_SAVE_IMAGES
static void save_display_set(DVBSubContext *ctx)
{
DVBSubRegion *region;
DVBSubRegionDisplay *display;
DVBSubCLUT *clut;
uint32_t *clut_table;
int x_pos, y_pos, width, height;
int x, y, y_off, x_off;
uint32_t *pbuf;
char filename[32];
static int fileno_index = 0;
x_pos = -1;
y_pos = -1;
width = 0;
height = 0;
for (display = ctx->display_list; display; display = display->next) {
region = get_region(ctx, display->region_id);
if (x_pos == -1) {
x_pos = display->x_pos;
y_pos = display->y_pos;
width = region->width;
height = region->height;
} else {
if (display->x_pos < x_pos) {
width += (x_pos - display->x_pos);
x_pos = display->x_pos;
}
if (display->y_pos < y_pos) {
height += (y_pos - display->y_pos);
y_pos = display->y_pos;
}
if (display->x_pos + region->width > x_pos + width) {
width = display->x_pos + region->width - x_pos;
}
if (display->y_pos + region->height > y_pos + height) {
height = display->y_pos + region->height - y_pos;
}
}
}
if (x_pos >= 0) {
pbuf = av_malloc(width * height * 4);
for (display = ctx->display_list; display; display = display->next) {
region = get_region(ctx, display->region_id);
x_off = display->x_pos - x_pos;
y_off = display->y_pos - y_pos;
clut = get_clut(ctx, region->clut);
if (clut == 0)
clut = &default_clut;
switch (region->depth) {
case 2:
clut_table = clut->clut4;
break;
case 8:
clut_table = clut->clut256;
break;
case 4:
default:
clut_table = clut->clut16;
break;
}
for (y = 0; y < region->height; y++) {
for (x = 0; x < region->width; x++) {
pbuf[((y + y_off) * width) + x_off + x] =
clut_table[region->pbuf[y * region->width + x]];
}
}
}
snprintf(filename, sizeof(filename), "dvbs.%d", fileno_index);
png_save2(filename, pbuf, width, height);
av_free(pbuf);
}
fileno_index++;
}
#endif
static int dvbsub_display_end_segment(AVCodecContext *avctx, const uint8_t *buf,
int buf_size, AVSubtitle *sub)
{
DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
DVBSubRegion *region;
DVBSubRegionDisplay *display;
AVSubtitleRect *rect;
DVBSubCLUT *clut;
uint32_t *clut_table;
int i;
sub->rects = NULL;
sub->start_display_time = 0;
sub->end_display_time = ctx->time_out * 1000;
sub->format = 0;
sub->num_rects = ctx->display_list_size;
if (sub->num_rects > 0){
sub->rects = av_mallocz(sizeof(*sub->rects) * sub->num_rects);
for(i=0; i<sub->num_rects; i++)
sub->rects[i] = av_mallocz(sizeof(*sub->rects[i]));
}
i = 0;
for (display = ctx->display_list; display; display = display->next) {
region = get_region(ctx, display->region_id);
rect = sub->rects[i];
if (!region)
continue;
rect->x = display->x_pos;
rect->y = display->y_pos;
rect->w = region->width;
rect->h = region->height;
rect->nb_colors = 16;
rect->type = SUBTITLE_BITMAP;
rect->pict.linesize[0] = region->width;
clut = get_clut(ctx, region->clut);
if (!clut)
clut = &default_clut;
switch (region->depth) {
case 2:
clut_table = clut->clut4;
break;
case 8:
clut_table = clut->clut256;
break;
case 4:
default:
clut_table = clut->clut16;
break;
}
rect->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
memcpy(rect->pict.data[1], clut_table, (1 << region->depth) * sizeof(uint32_t));
rect->pict.data[0] = av_malloc(region->buf_size);
memcpy(rect->pict.data[0], region->pbuf, region->buf_size);
i++;
}
sub->num_rects = i;
#ifdef DEBUG_SAVE_IMAGES
save_display_set(ctx);
#endif
return 1;
}
static int dvbsub_decode(AVCodecContext *avctx,
void *data, int *data_size,
AVPacket *avpkt)
{
const uint8_t *buf = avpkt->data;
int buf_size = avpkt->size;
DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
AVSubtitle *sub = (AVSubtitle*) data;
const uint8_t *p, *p_end;
int segment_type;
int page_id;
int segment_length;
#ifdef DEBUG_PACKET_CONTENTS
int i;
av_log(avctx, AV_LOG_INFO, "DVB sub packet:\n");
for (i=0; i < buf_size; i++) {
av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
if (i % 16 == 15)
av_log(avctx, AV_LOG_INFO, "\n");
}
if (i % 16)
av_log(avctx, AV_LOG_INFO, "\n");
#endif
if (buf_size <= 2)
return -1;
p = buf;
p_end = buf + buf_size;
while (p < p_end && *p == 0x0f) {
p += 1;
segment_type = *p++;
page_id = AV_RB16(p);
p += 2;
segment_length = AV_RB16(p);
p += 2;
if (page_id == ctx->composition_id || page_id == ctx->ancillary_id) {
switch (segment_type) {
case DVBSUB_PAGE_SEGMENT:
dvbsub_parse_page_segment(avctx, p, segment_length);
break;
case DVBSUB_REGION_SEGMENT:
dvbsub_parse_region_segment(avctx, p, segment_length);
break;
case DVBSUB_CLUT_SEGMENT:
dvbsub_parse_clut_segment(avctx, p, segment_length);
break;
case DVBSUB_OBJECT_SEGMENT:
dvbsub_parse_object_segment(avctx, p, segment_length);
break;
case DVBSUB_DISPLAY_SEGMENT:
*data_size = dvbsub_display_end_segment(avctx, p, segment_length, sub);
break;
default:
dprintf(avctx, "Subtitling segment type 0x%x, page id %d, length %d\n",
segment_type, page_id, segment_length);
break;
}
}
p += segment_length;
}
if (p != p_end) {
dprintf(avctx, "Junk at end of packet\n");
return -1;
}
return buf_size;
}
AVCodec dvbsub_decoder = {
"dvbsub",
AVMEDIA_TYPE_SUBTITLE,
CODEC_ID_DVB_SUBTITLE,
sizeof(DVBSubContext),
dvbsub_init_decoder,
NULL,
dvbsub_close_decoder,
dvbsub_decode,
.long_name = NULL_IF_CONFIG_SMALL("DVB subtitles"),
};
| 123linslouis-android-video-cutter | jni/libavcodec/dvbsubdec.c | C | asf20 | 39,025 |
/*
* G.726 ADPCM audio codec
* Copyright (c) 2004 Roman Shaposhnik
*
* This is a very straightforward rendition of the G.726
* Section 4 "Computational Details".
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <limits.h>
#include "avcodec.h"
#include "get_bits.h"
#include "put_bits.h"
/**
* G.726 11bit float.
* G.726 Standard uses rather odd 11bit floating point arithmentic for
* numerous occasions. It's a mistery to me why they did it this way
* instead of simply using 32bit integer arithmetic.
*/
typedef struct Float11 {
uint8_t sign; /**< 1bit sign */
uint8_t exp; /**< 4bit exponent */
uint8_t mant; /**< 6bit mantissa */
} Float11;
static inline Float11* i2f(int i, Float11* f)
{
f->sign = (i < 0);
if (f->sign)
i = -i;
f->exp = av_log2_16bit(i) + !!i;
f->mant = i? (i<<6) >> f->exp : 1<<5;
return f;
}
static inline int16_t mult(Float11* f1, Float11* f2)
{
int res, exp;
exp = f1->exp + f2->exp;
res = (((f1->mant * f2->mant) + 0x30) >> 4);
res = exp > 19 ? res << (exp - 19) : res >> (19 - exp);
return (f1->sign ^ f2->sign) ? -res : res;
}
static inline int sgn(int value)
{
return (value < 0) ? -1 : 1;
}
typedef struct G726Tables {
const int* quant; /**< quantization table */
const int16_t* iquant; /**< inverse quantization table */
const int16_t* W; /**< special table #1 ;-) */
const uint8_t* F; /**< special table #2 */
} G726Tables;
typedef struct G726Context {
G726Tables tbls; /**< static tables needed for computation */
Float11 sr[2]; /**< prev. reconstructed samples */
Float11 dq[6]; /**< prev. difference */
int a[2]; /**< second order predictor coeffs */
int b[6]; /**< sixth order predictor coeffs */
int pk[2]; /**< signs of prev. 2 sez + dq */
int ap; /**< scale factor control */
int yu; /**< fast scale factor */
int yl; /**< slow scale factor */
int dms; /**< short average magnitude of F[i] */
int dml; /**< long average magnitude of F[i] */
int td; /**< tone detect */
int se; /**< estimated signal for the next iteration */
int sez; /**< estimated second order prediction */
int y; /**< quantizer scaling factor for the next iteration */
int code_size;
} G726Context;
static const int quant_tbl16[] = /**< 16kbit/s 2bits per sample */
{ 260, INT_MAX };
static const int16_t iquant_tbl16[] =
{ 116, 365, 365, 116 };
static const int16_t W_tbl16[] =
{ -22, 439, 439, -22 };
static const uint8_t F_tbl16[] =
{ 0, 7, 7, 0 };
static const int quant_tbl24[] = /**< 24kbit/s 3bits per sample */
{ 7, 217, 330, INT_MAX };
static const int16_t iquant_tbl24[] =
{ INT16_MIN, 135, 273, 373, 373, 273, 135, INT16_MIN };
static const int16_t W_tbl24[] =
{ -4, 30, 137, 582, 582, 137, 30, -4 };
static const uint8_t F_tbl24[] =
{ 0, 1, 2, 7, 7, 2, 1, 0 };
static const int quant_tbl32[] = /**< 32kbit/s 4bits per sample */
{ -125, 79, 177, 245, 299, 348, 399, INT_MAX };
static const int16_t iquant_tbl32[] =
{ INT16_MIN, 4, 135, 213, 273, 323, 373, 425,
425, 373, 323, 273, 213, 135, 4, INT16_MIN };
static const int16_t W_tbl32[] =
{ -12, 18, 41, 64, 112, 198, 355, 1122,
1122, 355, 198, 112, 64, 41, 18, -12};
static const uint8_t F_tbl32[] =
{ 0, 0, 0, 1, 1, 1, 3, 7, 7, 3, 1, 1, 1, 0, 0, 0 };
static const int quant_tbl40[] = /**< 40kbit/s 5bits per sample */
{ -122, -16, 67, 138, 197, 249, 297, 338,
377, 412, 444, 474, 501, 527, 552, INT_MAX };
static const int16_t iquant_tbl40[] =
{ INT16_MIN, -66, 28, 104, 169, 224, 274, 318,
358, 395, 429, 459, 488, 514, 539, 566,
566, 539, 514, 488, 459, 429, 395, 358,
318, 274, 224, 169, 104, 28, -66, INT16_MIN };
static const int16_t W_tbl40[] =
{ 14, 14, 24, 39, 40, 41, 58, 100,
141, 179, 219, 280, 358, 440, 529, 696,
696, 529, 440, 358, 280, 219, 179, 141,
100, 58, 41, 40, 39, 24, 14, 14 };
static const uint8_t F_tbl40[] =
{ 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 3, 4, 5, 6, 6,
6, 6, 5, 4, 3, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
static const G726Tables G726Tables_pool[] =
{{ quant_tbl16, iquant_tbl16, W_tbl16, F_tbl16 },
{ quant_tbl24, iquant_tbl24, W_tbl24, F_tbl24 },
{ quant_tbl32, iquant_tbl32, W_tbl32, F_tbl32 },
{ quant_tbl40, iquant_tbl40, W_tbl40, F_tbl40 }};
/**
* Para 4.2.2 page 18: Adaptive quantizer.
*/
static inline uint8_t quant(G726Context* c, int d)
{
int sign, exp, i, dln;
sign = i = 0;
if (d < 0) {
sign = 1;
d = -d;
}
exp = av_log2_16bit(d);
dln = ((exp<<7) + (((d<<7)>>exp)&0x7f)) - (c->y>>2);
while (c->tbls.quant[i] < INT_MAX && c->tbls.quant[i] < dln)
++i;
if (sign)
i = ~i;
if (c->code_size != 2 && i == 0) /* I'm not sure this is a good idea */
i = 0xff;
return i;
}
/**
* Para 4.2.3 page 22: Inverse adaptive quantizer.
*/
static inline int16_t inverse_quant(G726Context* c, int i)
{
int dql, dex, dqt;
dql = c->tbls.iquant[i] + (c->y >> 2);
dex = (dql>>7) & 0xf; /* 4bit exponent */
dqt = (1<<7) + (dql & 0x7f); /* log2 -> linear */
return (dql < 0) ? 0 : ((dqt<<dex) >> 7);
}
static int16_t g726_decode(G726Context* c, int I)
{
int dq, re_signal, pk0, fa1, i, tr, ylint, ylfrac, thr2, al, dq0;
Float11 f;
int I_sig= I >> (c->code_size - 1);
dq = inverse_quant(c, I);
/* Transition detect */
ylint = (c->yl >> 15);
ylfrac = (c->yl >> 10) & 0x1f;
thr2 = (ylint > 9) ? 0x1f << 10 : (0x20 + ylfrac) << ylint;
tr= (c->td == 1 && dq > ((3*thr2)>>2));
if (I_sig) /* get the sign */
dq = -dq;
re_signal = c->se + dq;
/* Update second order predictor coefficient A2 and A1 */
pk0 = (c->sez + dq) ? sgn(c->sez + dq) : 0;
dq0 = dq ? sgn(dq) : 0;
if (tr) {
c->a[0] = 0;
c->a[1] = 0;
for (i=0; i<6; i++)
c->b[i] = 0;
} else {
/* This is a bit crazy, but it really is +255 not +256 */
fa1 = av_clip((-c->a[0]*c->pk[0]*pk0)>>5, -256, 255);
c->a[1] += 128*pk0*c->pk[1] + fa1 - (c->a[1]>>7);
c->a[1] = av_clip(c->a[1], -12288, 12288);
c->a[0] += 64*3*pk0*c->pk[0] - (c->a[0] >> 8);
c->a[0] = av_clip(c->a[0], -(15360 - c->a[1]), 15360 - c->a[1]);
for (i=0; i<6; i++)
c->b[i] += 128*dq0*sgn(-c->dq[i].sign) - (c->b[i]>>8);
}
/* Update Dq and Sr and Pk */
c->pk[1] = c->pk[0];
c->pk[0] = pk0 ? pk0 : 1;
c->sr[1] = c->sr[0];
i2f(re_signal, &c->sr[0]);
for (i=5; i>0; i--)
c->dq[i] = c->dq[i-1];
i2f(dq, &c->dq[0]);
c->dq[0].sign = I_sig; /* Isn't it crazy ?!?! */
c->td = c->a[1] < -11776;
/* Update Ap */
c->dms += (c->tbls.F[I]<<4) + ((- c->dms) >> 5);
c->dml += (c->tbls.F[I]<<4) + ((- c->dml) >> 7);
if (tr)
c->ap = 256;
else {
c->ap += (-c->ap) >> 4;
if (c->y <= 1535 || c->td || abs((c->dms << 2) - c->dml) >= (c->dml >> 3))
c->ap += 0x20;
}
/* Update Yu and Yl */
c->yu = av_clip(c->y + c->tbls.W[I] + ((-c->y)>>5), 544, 5120);
c->yl += c->yu + ((-c->yl)>>6);
/* Next iteration for Y */
al = (c->ap >= 256) ? 1<<6 : c->ap >> 2;
c->y = (c->yl + (c->yu - (c->yl>>6))*al) >> 6;
/* Next iteration for SE and SEZ */
c->se = 0;
for (i=0; i<6; i++)
c->se += mult(i2f(c->b[i] >> 2, &f), &c->dq[i]);
c->sez = c->se >> 1;
for (i=0; i<2; i++)
c->se += mult(i2f(c->a[i] >> 2, &f), &c->sr[i]);
c->se >>= 1;
return av_clip(re_signal << 2, -0xffff, 0xffff);
}
static av_cold int g726_reset(G726Context* c, int index)
{
int i;
c->tbls = G726Tables_pool[index];
for (i=0; i<2; i++) {
c->sr[i].mant = 1<<5;
c->pk[i] = 1;
}
for (i=0; i<6; i++) {
c->dq[i].mant = 1<<5;
}
c->yu = 544;
c->yl = 34816;
c->y = 544;
return 0;
}
#if CONFIG_ADPCM_G726_ENCODER
static int16_t g726_encode(G726Context* c, int16_t sig)
{
uint8_t i;
i = quant(c, sig/4 - c->se) & ((1<<c->code_size) - 1);
g726_decode(c, i);
return i;
}
#endif
/* Interfacing to the libavcodec */
static av_cold int g726_init(AVCodecContext * avctx)
{
G726Context* c = avctx->priv_data;
unsigned int index;
if (avctx->sample_rate <= 0) {
av_log(avctx, AV_LOG_ERROR, "Samplerate is invalid\n");
return -1;
}
index = (avctx->bit_rate + avctx->sample_rate/2) / avctx->sample_rate - 2;
if (avctx->bit_rate % avctx->sample_rate && avctx->codec->encode) {
av_log(avctx, AV_LOG_ERROR, "Bitrate - Samplerate combination is invalid\n");
return -1;
}
if(avctx->channels != 1){
av_log(avctx, AV_LOG_ERROR, "Only mono is supported\n");
return -1;
}
if(index>3){
av_log(avctx, AV_LOG_ERROR, "Unsupported number of bits %d\n", index+2);
return -1;
}
g726_reset(c, index);
c->code_size = index+2;
avctx->coded_frame = avcodec_alloc_frame();
if (!avctx->coded_frame)
return AVERROR(ENOMEM);
avctx->coded_frame->key_frame = 1;
if (avctx->codec->decode)
avctx->sample_fmt = SAMPLE_FMT_S16;
return 0;
}
static av_cold int g726_close(AVCodecContext *avctx)
{
av_freep(&avctx->coded_frame);
return 0;
}
#if CONFIG_ADPCM_G726_ENCODER
static int g726_encode_frame(AVCodecContext *avctx,
uint8_t *dst, int buf_size, void *data)
{
G726Context *c = avctx->priv_data;
short *samples = data;
PutBitContext pb;
init_put_bits(&pb, dst, 1024*1024);
for (; buf_size; buf_size--)
put_bits(&pb, c->code_size, g726_encode(c, *samples++));
flush_put_bits(&pb);
return put_bits_count(&pb)>>3;
}
#endif
static int g726_decode_frame(AVCodecContext *avctx,
void *data, int *data_size,
AVPacket *avpkt)
{
const uint8_t *buf = avpkt->data;
int buf_size = avpkt->size;
G726Context *c = avctx->priv_data;
short *samples = data;
GetBitContext gb;
init_get_bits(&gb, buf, buf_size * 8);
while (get_bits_count(&gb) + c->code_size <= buf_size*8)
*samples++ = g726_decode(c, get_bits(&gb, c->code_size));
if(buf_size*8 != get_bits_count(&gb))
av_log(avctx, AV_LOG_ERROR, "Frame invalidly split, missing parser?\n");
*data_size = (uint8_t*)samples - (uint8_t*)data;
return buf_size;
}
#if CONFIG_ADPCM_G726_ENCODER
AVCodec adpcm_g726_encoder = {
"g726",
AVMEDIA_TYPE_AUDIO,
CODEC_ID_ADPCM_G726,
sizeof(G726Context),
g726_init,
g726_encode_frame,
g726_close,
NULL,
.sample_fmts = (const enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
.long_name = NULL_IF_CONFIG_SMALL("G.726 ADPCM"),
};
#endif
AVCodec adpcm_g726_decoder = {
"g726",
AVMEDIA_TYPE_AUDIO,
CODEC_ID_ADPCM_G726,
sizeof(G726Context),
g726_init,
NULL,
g726_close,
g726_decode_frame,
.long_name = NULL_IF_CONFIG_SMALL("G.726 ADPCM"),
};
| 123linslouis-android-video-cutter | jni/libavcodec/g726.c | C | asf20 | 12,362 |
/*
* VC-1 and WMV3 decoder
* Copyright (c) 2006-2007 Konstantin Shishkov
* Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* VC-1 and WMV3 decoder
*
*/
#include "internal.h"
#include "dsputil.h"
#include "avcodec.h"
#include "mpegvideo.h"
#include "h263.h"
#include "vc1.h"
#include "vc1data.h"
#include "vc1acdata.h"
#include "msmpeg4data.h"
#include "unary.h"
#include "simple_idct.h"
#include "mathops.h"
#include "vdpau_internal.h"
#undef NDEBUG
#include <assert.h>
#define MB_INTRA_VLC_BITS 9
#define DC_VLC_BITS 9
#define AC_VLC_BITS 9
static const uint16_t table_mb_intra[64][2];
static const uint16_t vlc_offs[] = {
0, 520, 552, 616, 1128, 1160, 1224, 1740, 1772, 1836, 1900, 2436,
2986, 3050, 3610, 4154, 4218, 4746, 5326, 5390, 5902, 6554, 7658, 8620,
9262, 10202, 10756, 11310, 12228, 15078
};
/**
* Init VC-1 specific tables and VC1Context members
* @param v The VC1Context to initialize
* @return Status
*/
static int vc1_init_common(VC1Context *v)
{
static int done = 0;
int i = 0;
static VLC_TYPE vlc_table[15078][2];
v->hrd_rate = v->hrd_buffer = NULL;
/* VLC tables */
if(!done)
{
INIT_VLC_STATIC(&ff_vc1_bfraction_vlc, VC1_BFRACTION_VLC_BITS, 23,
ff_vc1_bfraction_bits, 1, 1,
ff_vc1_bfraction_codes, 1, 1, 1 << VC1_BFRACTION_VLC_BITS);
INIT_VLC_STATIC(&ff_vc1_norm2_vlc, VC1_NORM2_VLC_BITS, 4,
ff_vc1_norm2_bits, 1, 1,
ff_vc1_norm2_codes, 1, 1, 1 << VC1_NORM2_VLC_BITS);
INIT_VLC_STATIC(&ff_vc1_norm6_vlc, VC1_NORM6_VLC_BITS, 64,
ff_vc1_norm6_bits, 1, 1,
ff_vc1_norm6_codes, 2, 2, 556);
INIT_VLC_STATIC(&ff_vc1_imode_vlc, VC1_IMODE_VLC_BITS, 7,
ff_vc1_imode_bits, 1, 1,
ff_vc1_imode_codes, 1, 1, 1 << VC1_IMODE_VLC_BITS);
for (i=0; i<3; i++)
{
ff_vc1_ttmb_vlc[i].table = &vlc_table[vlc_offs[i*3+0]];
ff_vc1_ttmb_vlc[i].table_allocated = vlc_offs[i*3+1] - vlc_offs[i*3+0];
init_vlc(&ff_vc1_ttmb_vlc[i], VC1_TTMB_VLC_BITS, 16,
ff_vc1_ttmb_bits[i], 1, 1,
ff_vc1_ttmb_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
ff_vc1_ttblk_vlc[i].table = &vlc_table[vlc_offs[i*3+1]];
ff_vc1_ttblk_vlc[i].table_allocated = vlc_offs[i*3+2] - vlc_offs[i*3+1];
init_vlc(&ff_vc1_ttblk_vlc[i], VC1_TTBLK_VLC_BITS, 8,
ff_vc1_ttblk_bits[i], 1, 1,
ff_vc1_ttblk_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
ff_vc1_subblkpat_vlc[i].table = &vlc_table[vlc_offs[i*3+2]];
ff_vc1_subblkpat_vlc[i].table_allocated = vlc_offs[i*3+3] - vlc_offs[i*3+2];
init_vlc(&ff_vc1_subblkpat_vlc[i], VC1_SUBBLKPAT_VLC_BITS, 15,
ff_vc1_subblkpat_bits[i], 1, 1,
ff_vc1_subblkpat_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
}
for(i=0; i<4; i++)
{
ff_vc1_4mv_block_pattern_vlc[i].table = &vlc_table[vlc_offs[i*3+9]];
ff_vc1_4mv_block_pattern_vlc[i].table_allocated = vlc_offs[i*3+10] - vlc_offs[i*3+9];
init_vlc(&ff_vc1_4mv_block_pattern_vlc[i], VC1_4MV_BLOCK_PATTERN_VLC_BITS, 16,
ff_vc1_4mv_block_pattern_bits[i], 1, 1,
ff_vc1_4mv_block_pattern_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
ff_vc1_cbpcy_p_vlc[i].table = &vlc_table[vlc_offs[i*3+10]];
ff_vc1_cbpcy_p_vlc[i].table_allocated = vlc_offs[i*3+11] - vlc_offs[i*3+10];
init_vlc(&ff_vc1_cbpcy_p_vlc[i], VC1_CBPCY_P_VLC_BITS, 64,
ff_vc1_cbpcy_p_bits[i], 1, 1,
ff_vc1_cbpcy_p_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
ff_vc1_mv_diff_vlc[i].table = &vlc_table[vlc_offs[i*3+11]];
ff_vc1_mv_diff_vlc[i].table_allocated = vlc_offs[i*3+12] - vlc_offs[i*3+11];
init_vlc(&ff_vc1_mv_diff_vlc[i], VC1_MV_DIFF_VLC_BITS, 73,
ff_vc1_mv_diff_bits[i], 1, 1,
ff_vc1_mv_diff_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
}
for(i=0; i<8; i++){
ff_vc1_ac_coeff_table[i].table = &vlc_table[vlc_offs[i+21]];
ff_vc1_ac_coeff_table[i].table_allocated = vlc_offs[i+22] - vlc_offs[i+21];
init_vlc(&ff_vc1_ac_coeff_table[i], AC_VLC_BITS, vc1_ac_sizes[i],
&vc1_ac_tables[i][0][1], 8, 4,
&vc1_ac_tables[i][0][0], 8, 4, INIT_VLC_USE_NEW_STATIC);
}
done = 1;
}
/* Other defaults */
v->pq = -1;
v->mvrange = 0; /* 7.1.1.18, p80 */
return 0;
}
/***********************************************************************/
/**
* @defgroup vc1bitplane VC-1 Bitplane decoding
* @see 8.7, p56
* @{
*/
/**
* Imode types
* @{
*/
enum Imode {
IMODE_RAW,
IMODE_NORM2,
IMODE_DIFF2,
IMODE_NORM6,
IMODE_DIFF6,
IMODE_ROWSKIP,
IMODE_COLSKIP
};
/** @} */ //imode defines
/** @} */ //Bitplane group
static void vc1_loop_filter_iblk(MpegEncContext *s, int pq)
{
int i, j;
if(!s->first_slice_line)
s->dsp.vc1_v_loop_filter16(s->dest[0], s->linesize, pq);
s->dsp.vc1_v_loop_filter16(s->dest[0] + 8*s->linesize, s->linesize, pq);
for(i = !s->mb_x*8; i < 16; i += 8)
s->dsp.vc1_h_loop_filter16(s->dest[0] + i, s->linesize, pq);
for(j = 0; j < 2; j++){
if(!s->first_slice_line)
s->dsp.vc1_v_loop_filter8(s->dest[j+1], s->uvlinesize, pq);
if(s->mb_x)
s->dsp.vc1_h_loop_filter8(s->dest[j+1], s->uvlinesize, pq);
}
}
/** Put block onto picture
*/
static void vc1_put_block(VC1Context *v, DCTELEM block[6][64])
{
uint8_t *Y;
int ys, us, vs;
DSPContext *dsp = &v->s.dsp;
if(v->rangeredfrm) {
int i, j, k;
for(k = 0; k < 6; k++)
for(j = 0; j < 8; j++)
for(i = 0; i < 8; i++)
block[k][i + j*8] = ((block[k][i + j*8] - 128) << 1) + 128;
}
ys = v->s.current_picture.linesize[0];
us = v->s.current_picture.linesize[1];
vs = v->s.current_picture.linesize[2];
Y = v->s.dest[0];
dsp->put_pixels_clamped(block[0], Y, ys);
dsp->put_pixels_clamped(block[1], Y + 8, ys);
Y += ys * 8;
dsp->put_pixels_clamped(block[2], Y, ys);
dsp->put_pixels_clamped(block[3], Y + 8, ys);
if(!(v->s.flags & CODEC_FLAG_GRAY)) {
dsp->put_pixels_clamped(block[4], v->s.dest[1], us);
dsp->put_pixels_clamped(block[5], v->s.dest[2], vs);
}
}
/** Do motion compensation over 1 macroblock
* Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
*/
static void vc1_mc_1mv(VC1Context *v, int dir)
{
MpegEncContext *s = &v->s;
DSPContext *dsp = &v->s.dsp;
uint8_t *srcY, *srcU, *srcV;
int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
if(!v->s.last_picture.data[0])return;
mx = s->mv[dir][0][0];
my = s->mv[dir][0][1];
// store motion vectors for further use in B frames
if(s->pict_type == FF_P_TYPE) {
s->current_picture.motion_val[1][s->block_index[0]][0] = mx;
s->current_picture.motion_val[1][s->block_index[0]][1] = my;
}
uvmx = (mx + ((mx & 3) == 3)) >> 1;
uvmy = (my + ((my & 3) == 3)) >> 1;
if(v->fastuvmc) {
uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1));
uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1));
}
if(!dir) {
srcY = s->last_picture.data[0];
srcU = s->last_picture.data[1];
srcV = s->last_picture.data[2];
} else {
srcY = s->next_picture.data[0];
srcU = s->next_picture.data[1];
srcV = s->next_picture.data[2];
}
src_x = s->mb_x * 16 + (mx >> 2);
src_y = s->mb_y * 16 + (my >> 2);
uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
if(v->profile != PROFILE_ADVANCED){
src_x = av_clip( src_x, -16, s->mb_width * 16);
src_y = av_clip( src_y, -16, s->mb_height * 16);
uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
}else{
src_x = av_clip( src_x, -17, s->avctx->coded_width);
src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
}
srcY += src_y * s->linesize + src_x;
srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
/* for grayscale we should not try to read from unknown area */
if(s->flags & CODEC_FLAG_GRAY) {
srcU = s->edge_emu_buffer + 18 * s->linesize;
srcV = s->edge_emu_buffer + 18 * s->linesize;
}
if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
|| (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel*3
|| (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 16 - s->mspel*3){
uint8_t *uvbuf= s->edge_emu_buffer + 19 * s->linesize;
srcY -= s->mspel * (1 + s->linesize);
ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 17+s->mspel*2, 17+s->mspel*2,
src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
srcY = s->edge_emu_buffer;
ff_emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8+1, 8+1,
uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
ff_emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8+1, 8+1,
uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
srcU = uvbuf;
srcV = uvbuf + 16;
/* if we deal with range reduction we need to scale source blocks */
if(v->rangeredfrm) {
int i, j;
uint8_t *src, *src2;
src = srcY;
for(j = 0; j < 17 + s->mspel*2; j++) {
for(i = 0; i < 17 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;
src += s->linesize;
}
src = srcU; src2 = srcV;
for(j = 0; j < 9; j++) {
for(i = 0; i < 9; i++) {
src[i] = ((src[i] - 128) >> 1) + 128;
src2[i] = ((src2[i] - 128) >> 1) + 128;
}
src += s->uvlinesize;
src2 += s->uvlinesize;
}
}
/* if we deal with intensity compensation we need to scale source blocks */
if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
int i, j;
uint8_t *src, *src2;
src = srcY;
for(j = 0; j < 17 + s->mspel*2; j++) {
for(i = 0; i < 17 + s->mspel*2; i++) src[i] = v->luty[src[i]];
src += s->linesize;
}
src = srcU; src2 = srcV;
for(j = 0; j < 9; j++) {
for(i = 0; i < 9; i++) {
src[i] = v->lutuv[src[i]];
src2[i] = v->lutuv[src2[i]];
}
src += s->uvlinesize;
src2 += s->uvlinesize;
}
}
srcY += s->mspel * (1 + s->linesize);
}
if(s->mspel) {
dxy = ((my & 3) << 2) | (mx & 3);
dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] , srcY , s->linesize, v->rnd);
dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8, srcY + 8, s->linesize, v->rnd);
srcY += s->linesize * 8;
dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize , srcY , s->linesize, v->rnd);
dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
} else { // hpel mc - always used for luma
dxy = (my & 2) | ((mx & 2) >> 1);
if(!v->rnd)
dsp->put_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
else
dsp->put_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
}
if(s->flags & CODEC_FLAG_GRAY) return;
/* Chroma MC always uses qpel bilinear */
uvmx = (uvmx&3)<<1;
uvmy = (uvmy&3)<<1;
if(!v->rnd){
dsp->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
dsp->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
}else{
dsp->put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
dsp->put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
}
}
/** Do motion compensation for 4-MV macroblock - luminance block
*/
static void vc1_mc_4mv_luma(VC1Context *v, int n)
{
MpegEncContext *s = &v->s;
DSPContext *dsp = &v->s.dsp;
uint8_t *srcY;
int dxy, mx, my, src_x, src_y;
int off;
if(!v->s.last_picture.data[0])return;
mx = s->mv[0][n][0];
my = s->mv[0][n][1];
srcY = s->last_picture.data[0];
off = s->linesize * 4 * (n&2) + (n&1) * 8;
src_x = s->mb_x * 16 + (n&1) * 8 + (mx >> 2);
src_y = s->mb_y * 16 + (n&2) * 4 + (my >> 2);
if(v->profile != PROFILE_ADVANCED){
src_x = av_clip( src_x, -16, s->mb_width * 16);
src_y = av_clip( src_y, -16, s->mb_height * 16);
}else{
src_x = av_clip( src_x, -17, s->avctx->coded_width);
src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
}
srcY += src_y * s->linesize + src_x;
if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
|| (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 8 - s->mspel*2
|| (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 8 - s->mspel*2){
srcY -= s->mspel * (1 + s->linesize);
ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 9+s->mspel*2, 9+s->mspel*2,
src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
srcY = s->edge_emu_buffer;
/* if we deal with range reduction we need to scale source blocks */
if(v->rangeredfrm) {
int i, j;
uint8_t *src;
src = srcY;
for(j = 0; j < 9 + s->mspel*2; j++) {
for(i = 0; i < 9 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;
src += s->linesize;
}
}
/* if we deal with intensity compensation we need to scale source blocks */
if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
int i, j;
uint8_t *src;
src = srcY;
for(j = 0; j < 9 + s->mspel*2; j++) {
for(i = 0; i < 9 + s->mspel*2; i++) src[i] = v->luty[src[i]];
src += s->linesize;
}
}
srcY += s->mspel * (1 + s->linesize);
}
if(s->mspel) {
dxy = ((my & 3) << 2) | (mx & 3);
dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize, v->rnd);
} else { // hpel mc - always used for luma
dxy = (my & 2) | ((mx & 2) >> 1);
if(!v->rnd)
dsp->put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
else
dsp->put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
}
}
static inline int median4(int a, int b, int c, int d)
{
if(a < b) {
if(c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
else return (FFMIN(b, c) + FFMAX(a, d)) / 2;
} else {
if(c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
else return (FFMIN(a, c) + FFMAX(b, d)) / 2;
}
}
/** Do motion compensation for 4-MV macroblock - both chroma blocks
*/
static void vc1_mc_4mv_chroma(VC1Context *v)
{
MpegEncContext *s = &v->s;
DSPContext *dsp = &v->s.dsp;
uint8_t *srcU, *srcV;
int uvmx, uvmy, uvsrc_x, uvsrc_y;
int i, idx, tx = 0, ty = 0;
int mvx[4], mvy[4], intra[4];
static const int count[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
if(!v->s.last_picture.data[0])return;
if(s->flags & CODEC_FLAG_GRAY) return;
for(i = 0; i < 4; i++) {
mvx[i] = s->mv[0][i][0];
mvy[i] = s->mv[0][i][1];
intra[i] = v->mb_type[0][s->block_index[i]];
}
/* calculate chroma MV vector from four luma MVs */
idx = (intra[3] << 3) | (intra[2] << 2) | (intra[1] << 1) | intra[0];
if(!idx) { // all blocks are inter
tx = median4(mvx[0], mvx[1], mvx[2], mvx[3]);
ty = median4(mvy[0], mvy[1], mvy[2], mvy[3]);
} else if(count[idx] == 1) { // 3 inter blocks
switch(idx) {
case 0x1:
tx = mid_pred(mvx[1], mvx[2], mvx[3]);
ty = mid_pred(mvy[1], mvy[2], mvy[3]);
break;
case 0x2:
tx = mid_pred(mvx[0], mvx[2], mvx[3]);
ty = mid_pred(mvy[0], mvy[2], mvy[3]);
break;
case 0x4:
tx = mid_pred(mvx[0], mvx[1], mvx[3]);
ty = mid_pred(mvy[0], mvy[1], mvy[3]);
break;
case 0x8:
tx = mid_pred(mvx[0], mvx[1], mvx[2]);
ty = mid_pred(mvy[0], mvy[1], mvy[2]);
break;
}
} else if(count[idx] == 2) {
int t1 = 0, t2 = 0;
for(i=0; i<3;i++) if(!intra[i]) {t1 = i; break;}
for(i= t1+1; i<4; i++)if(!intra[i]) {t2 = i; break;}
tx = (mvx[t1] + mvx[t2]) / 2;
ty = (mvy[t1] + mvy[t2]) / 2;
} else {
s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
return; //no need to do MC for inter blocks
}
s->current_picture.motion_val[1][s->block_index[0]][0] = tx;
s->current_picture.motion_val[1][s->block_index[0]][1] = ty;
uvmx = (tx + ((tx&3) == 3)) >> 1;
uvmy = (ty + ((ty&3) == 3)) >> 1;
if(v->fastuvmc) {
uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1));
uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1));
}
uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
if(v->profile != PROFILE_ADVANCED){
uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
}else{
uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
}
srcU = s->last_picture.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
srcV = s->last_picture.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
|| (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
|| (unsigned)uvsrc_y > (s->v_edge_pos >> 1) - 9){
ff_emulated_edge_mc(s->edge_emu_buffer , srcU, s->uvlinesize, 8+1, 8+1,
uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
ff_emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize, 8+1, 8+1,
uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
srcU = s->edge_emu_buffer;
srcV = s->edge_emu_buffer + 16;
/* if we deal with range reduction we need to scale source blocks */
if(v->rangeredfrm) {
int i, j;
uint8_t *src, *src2;
src = srcU; src2 = srcV;
for(j = 0; j < 9; j++) {
for(i = 0; i < 9; i++) {
src[i] = ((src[i] - 128) >> 1) + 128;
src2[i] = ((src2[i] - 128) >> 1) + 128;
}
src += s->uvlinesize;
src2 += s->uvlinesize;
}
}
/* if we deal with intensity compensation we need to scale source blocks */
if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
int i, j;
uint8_t *src, *src2;
src = srcU; src2 = srcV;
for(j = 0; j < 9; j++) {
for(i = 0; i < 9; i++) {
src[i] = v->lutuv[src[i]];
src2[i] = v->lutuv[src2[i]];
}
src += s->uvlinesize;
src2 += s->uvlinesize;
}
}
}
/* Chroma MC always uses qpel bilinear */
uvmx = (uvmx&3)<<1;
uvmy = (uvmy&3)<<1;
if(!v->rnd){
dsp->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
dsp->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
}else{
dsp->put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
dsp->put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
}
}
/***********************************************************************/
/**
* @defgroup vc1block VC-1 Block-level functions
* @see 7.1.4, p91 and 8.1.1.7, p(1)04
* @{
*/
/**
* @def GET_MQUANT
* @brief Get macroblock-level quantizer scale
*/
#define GET_MQUANT() \
if (v->dquantfrm) \
{ \
int edges = 0; \
if (v->dqprofile == DQPROFILE_ALL_MBS) \
{ \
if (v->dqbilevel) \
{ \
mquant = (get_bits1(gb)) ? v->altpq : v->pq; \
} \
else \
{ \
mqdiff = get_bits(gb, 3); \
if (mqdiff != 7) mquant = v->pq + mqdiff; \
else mquant = get_bits(gb, 5); \
} \
} \
if(v->dqprofile == DQPROFILE_SINGLE_EDGE) \
edges = 1 << v->dqsbedge; \
else if(v->dqprofile == DQPROFILE_DOUBLE_EDGES) \
edges = (3 << v->dqsbedge) % 15; \
else if(v->dqprofile == DQPROFILE_FOUR_EDGES) \
edges = 15; \
if((edges&1) && !s->mb_x) \
mquant = v->altpq; \
if((edges&2) && s->first_slice_line) \
mquant = v->altpq; \
if((edges&4) && s->mb_x == (s->mb_width - 1)) \
mquant = v->altpq; \
if((edges&8) && s->mb_y == (s->mb_height - 1)) \
mquant = v->altpq; \
}
/**
* @def GET_MVDATA(_dmv_x, _dmv_y)
* @brief Get MV differentials
* @see MVDATA decoding from 8.3.5.2, p(1)20
* @param _dmv_x Horizontal differential for decoded MV
* @param _dmv_y Vertical differential for decoded MV
*/
#define GET_MVDATA(_dmv_x, _dmv_y) \
index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table,\
VC1_MV_DIFF_VLC_BITS, 2); \
if (index > 36) \
{ \
mb_has_coeffs = 1; \
index -= 37; \
} \
else mb_has_coeffs = 0; \
s->mb_intra = 0; \
if (!index) { _dmv_x = _dmv_y = 0; } \
else if (index == 35) \
{ \
_dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample); \
_dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample); \
} \
else if (index == 36) \
{ \
_dmv_x = 0; \
_dmv_y = 0; \
s->mb_intra = 1; \
} \
else \
{ \
index1 = index%6; \
if (!s->quarter_sample && index1 == 5) val = 1; \
else val = 0; \
if(size_table[index1] - val > 0) \
val = get_bits(gb, size_table[index1] - val); \
else val = 0; \
sign = 0 - (val&1); \
_dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
\
index1 = index/6; \
if (!s->quarter_sample && index1 == 5) val = 1; \
else val = 0; \
if(size_table[index1] - val > 0) \
val = get_bits(gb, size_table[index1] - val); \
else val = 0; \
sign = 0 - (val&1); \
_dmv_y = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
}
/** Predict and set motion vector
*/
static inline void vc1_pred_mv(MpegEncContext *s, int n, int dmv_x, int dmv_y, int mv1, int r_x, int r_y, uint8_t* is_intra)
{
int xy, wrap, off = 0;
int16_t *A, *B, *C;
int px, py;
int sum;
/* scale MV difference to be quad-pel */
dmv_x <<= 1 - s->quarter_sample;
dmv_y <<= 1 - s->quarter_sample;
wrap = s->b8_stride;
xy = s->block_index[n];
if(s->mb_intra){
s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = 0;
s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = 0;
s->current_picture.motion_val[1][xy][0] = 0;
s->current_picture.motion_val[1][xy][1] = 0;
if(mv1) { /* duplicate motion data for 1-MV block */
s->current_picture.motion_val[0][xy + 1][0] = 0;
s->current_picture.motion_val[0][xy + 1][1] = 0;
s->current_picture.motion_val[0][xy + wrap][0] = 0;
s->current_picture.motion_val[0][xy + wrap][1] = 0;
s->current_picture.motion_val[0][xy + wrap + 1][0] = 0;
s->current_picture.motion_val[0][xy + wrap + 1][1] = 0;
s->current_picture.motion_val[1][xy + 1][0] = 0;
s->current_picture.motion_val[1][xy + 1][1] = 0;
s->current_picture.motion_val[1][xy + wrap][0] = 0;
s->current_picture.motion_val[1][xy + wrap][1] = 0;
s->current_picture.motion_val[1][xy + wrap + 1][0] = 0;
s->current_picture.motion_val[1][xy + wrap + 1][1] = 0;
}
return;
}
C = s->current_picture.motion_val[0][xy - 1];
A = s->current_picture.motion_val[0][xy - wrap];
if(mv1)
off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
else {
//in 4-MV mode different blocks have different B predictor position
switch(n){
case 0:
off = (s->mb_x > 0) ? -1 : 1;
break;
case 1:
off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
break;
case 2:
off = 1;
break;
case 3:
off = -1;
}
}
B = s->current_picture.motion_val[0][xy - wrap + off];
if(!s->first_slice_line || (n==2 || n==3)) { // predictor A is not out of bounds
if(s->mb_width == 1) {
px = A[0];
py = A[1];
} else {
px = mid_pred(A[0], B[0], C[0]);
py = mid_pred(A[1], B[1], C[1]);
}
} else if(s->mb_x || (n==1 || n==3)) { // predictor C is not out of bounds
px = C[0];
py = C[1];
} else {
px = py = 0;
}
/* Pullback MV as specified in 8.3.5.3.4 */
{
int qx, qy, X, Y;
qx = (s->mb_x << 6) + ((n==1 || n==3) ? 32 : 0);
qy = (s->mb_y << 6) + ((n==2 || n==3) ? 32 : 0);
X = (s->mb_width << 6) - 4;
Y = (s->mb_height << 6) - 4;
if(mv1) {
if(qx + px < -60) px = -60 - qx;
if(qy + py < -60) py = -60 - qy;
} else {
if(qx + px < -28) px = -28 - qx;
if(qy + py < -28) py = -28 - qy;
}
if(qx + px > X) px = X - qx;
if(qy + py > Y) py = Y - qy;
}
/* Calculate hybrid prediction as specified in 8.3.5.3.5 */
if((!s->first_slice_line || (n==2 || n==3)) && (s->mb_x || (n==1 || n==3))) {
if(is_intra[xy - wrap])
sum = FFABS(px) + FFABS(py);
else
sum = FFABS(px - A[0]) + FFABS(py - A[1]);
if(sum > 32) {
if(get_bits1(&s->gb)) {
px = A[0];
py = A[1];
} else {
px = C[0];
py = C[1];
}
} else {
if(is_intra[xy - 1])
sum = FFABS(px) + FFABS(py);
else
sum = FFABS(px - C[0]) + FFABS(py - C[1]);
if(sum > 32) {
if(get_bits1(&s->gb)) {
px = A[0];
py = A[1];
} else {
px = C[0];
py = C[1];
}
}
}
}
/* store MV using signed modulus of MV range defined in 4.11 */
s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
if(mv1) { /* duplicate motion data for 1-MV block */
s->current_picture.motion_val[0][xy + 1][0] = s->current_picture.motion_val[0][xy][0];
s->current_picture.motion_val[0][xy + 1][1] = s->current_picture.motion_val[0][xy][1];
s->current_picture.motion_val[0][xy + wrap][0] = s->current_picture.motion_val[0][xy][0];
s->current_picture.motion_val[0][xy + wrap][1] = s->current_picture.motion_val[0][xy][1];
s->current_picture.motion_val[0][xy + wrap + 1][0] = s->current_picture.motion_val[0][xy][0];
s->current_picture.motion_val[0][xy + wrap + 1][1] = s->current_picture.motion_val[0][xy][1];
}
}
/** Motion compensation for direct or interpolated blocks in B-frames
*/
static void vc1_interp_mc(VC1Context *v)
{
MpegEncContext *s = &v->s;
DSPContext *dsp = &v->s.dsp;
uint8_t *srcY, *srcU, *srcV;
int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
if(!v->s.next_picture.data[0])return;
mx = s->mv[1][0][0];
my = s->mv[1][0][1];
uvmx = (mx + ((mx & 3) == 3)) >> 1;
uvmy = (my + ((my & 3) == 3)) >> 1;
if(v->fastuvmc) {
uvmx = uvmx + ((uvmx<0)?-(uvmx&1):(uvmx&1));
uvmy = uvmy + ((uvmy<0)?-(uvmy&1):(uvmy&1));
}
srcY = s->next_picture.data[0];
srcU = s->next_picture.data[1];
srcV = s->next_picture.data[2];
src_x = s->mb_x * 16 + (mx >> 2);
src_y = s->mb_y * 16 + (my >> 2);
uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
if(v->profile != PROFILE_ADVANCED){
src_x = av_clip( src_x, -16, s->mb_width * 16);
src_y = av_clip( src_y, -16, s->mb_height * 16);
uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
}else{
src_x = av_clip( src_x, -17, s->avctx->coded_width);
src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
}
srcY += src_y * s->linesize + src_x;
srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
/* for grayscale we should not try to read from unknown area */
if(s->flags & CODEC_FLAG_GRAY) {
srcU = s->edge_emu_buffer + 18 * s->linesize;
srcV = s->edge_emu_buffer + 18 * s->linesize;
}
if(v->rangeredfrm
|| (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel*3
|| (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 16 - s->mspel*3){
uint8_t *uvbuf= s->edge_emu_buffer + 19 * s->linesize;
srcY -= s->mspel * (1 + s->linesize);
ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 17+s->mspel*2, 17+s->mspel*2,
src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
srcY = s->edge_emu_buffer;
ff_emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8+1, 8+1,
uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
ff_emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8+1, 8+1,
uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
srcU = uvbuf;
srcV = uvbuf + 16;
/* if we deal with range reduction we need to scale source blocks */
if(v->rangeredfrm) {
int i, j;
uint8_t *src, *src2;
src = srcY;
for(j = 0; j < 17 + s->mspel*2; j++) {
for(i = 0; i < 17 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;
src += s->linesize;
}
src = srcU; src2 = srcV;
for(j = 0; j < 9; j++) {
for(i = 0; i < 9; i++) {
src[i] = ((src[i] - 128) >> 1) + 128;
src2[i] = ((src2[i] - 128) >> 1) + 128;
}
src += s->uvlinesize;
src2 += s->uvlinesize;
}
}
srcY += s->mspel * (1 + s->linesize);
}
if(s->mspel) {
dxy = ((my & 3) << 2) | (mx & 3);
dsp->avg_vc1_mspel_pixels_tab[dxy](s->dest[0] , srcY , s->linesize, v->rnd);
dsp->avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8, srcY + 8, s->linesize, v->rnd);
srcY += s->linesize * 8;
dsp->avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize , srcY , s->linesize, v->rnd);
dsp->avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
} else { // hpel mc
dxy = (my & 2) | ((mx & 2) >> 1);
if(!v->rnd)
dsp->avg_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
else
dsp->avg_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
}
if(s->flags & CODEC_FLAG_GRAY) return;
/* Chroma MC always uses qpel blilinear */
uvmx = (uvmx&3)<<1;
uvmy = (uvmy&3)<<1;
if(!v->rnd){
dsp->avg_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
dsp->avg_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
}else{
dsp->avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
dsp->avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
}
}
static av_always_inline int scale_mv(int value, int bfrac, int inv, int qs)
{
int n = bfrac;
#if B_FRACTION_DEN==256
if(inv)
n -= 256;
if(!qs)
return 2 * ((value * n + 255) >> 9);
return (value * n + 128) >> 8;
#else
if(inv)
n -= B_FRACTION_DEN;
if(!qs)
return 2 * ((value * n + B_FRACTION_DEN - 1) / (2 * B_FRACTION_DEN));
return (value * n + B_FRACTION_DEN/2) / B_FRACTION_DEN;
#endif
}
/** Reconstruct motion vector for B-frame and do motion compensation
*/
static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2], int direct, int mode)
{
if(v->use_ic) {
v->mv_mode2 = v->mv_mode;
v->mv_mode = MV_PMODE_INTENSITY_COMP;
}
if(direct) {
vc1_mc_1mv(v, 0);
vc1_interp_mc(v);
if(v->use_ic) v->mv_mode = v->mv_mode2;
return;
}
if(mode == BMV_TYPE_INTERPOLATED) {
vc1_mc_1mv(v, 0);
vc1_interp_mc(v);
if(v->use_ic) v->mv_mode = v->mv_mode2;
return;
}
if(v->use_ic && (mode == BMV_TYPE_BACKWARD)) v->mv_mode = v->mv_mode2;
vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
if(v->use_ic) v->mv_mode = v->mv_mode2;
}
static inline void vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2], int direct, int mvtype)
{
MpegEncContext *s = &v->s;
int xy, wrap, off = 0;
int16_t *A, *B, *C;
int px, py;
int sum;
int r_x, r_y;
const uint8_t *is_intra = v->mb_type[0];
r_x = v->range_x;
r_y = v->range_y;
/* scale MV difference to be quad-pel */
dmv_x[0] <<= 1 - s->quarter_sample;
dmv_y[0] <<= 1 - s->quarter_sample;
dmv_x[1] <<= 1 - s->quarter_sample;
dmv_y[1] <<= 1 - s->quarter_sample;
wrap = s->b8_stride;
xy = s->block_index[0];
if(s->mb_intra) {
s->current_picture.motion_val[0][xy][0] =
s->current_picture.motion_val[0][xy][1] =
s->current_picture.motion_val[1][xy][0] =
s->current_picture.motion_val[1][xy][1] = 0;
return;
}
s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
/* Pullback predicted motion vectors as specified in 8.4.5.4 */
s->mv[0][0][0] = av_clip(s->mv[0][0][0], -60 - (s->mb_x << 6), (s->mb_width << 6) - 4 - (s->mb_x << 6));
s->mv[0][0][1] = av_clip(s->mv[0][0][1], -60 - (s->mb_y << 6), (s->mb_height << 6) - 4 - (s->mb_y << 6));
s->mv[1][0][0] = av_clip(s->mv[1][0][0], -60 - (s->mb_x << 6), (s->mb_width << 6) - 4 - (s->mb_x << 6));
s->mv[1][0][1] = av_clip(s->mv[1][0][1], -60 - (s->mb_y << 6), (s->mb_height << 6) - 4 - (s->mb_y << 6));
if(direct) {
s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
return;
}
if((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
C = s->current_picture.motion_val[0][xy - 2];
A = s->current_picture.motion_val[0][xy - wrap*2];
off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
B = s->current_picture.motion_val[0][xy - wrap*2 + off];
if(!s->mb_x) C[0] = C[1] = 0;
if(!s->first_slice_line) { // predictor A is not out of bounds
if(s->mb_width == 1) {
px = A[0];
py = A[1];
} else {
px = mid_pred(A[0], B[0], C[0]);
py = mid_pred(A[1], B[1], C[1]);
}
} else if(s->mb_x) { // predictor C is not out of bounds
px = C[0];
py = C[1];
} else {
px = py = 0;
}
/* Pullback MV as specified in 8.3.5.3.4 */
{
int qx, qy, X, Y;
if(v->profile < PROFILE_ADVANCED) {
qx = (s->mb_x << 5);
qy = (s->mb_y << 5);
X = (s->mb_width << 5) - 4;
Y = (s->mb_height << 5) - 4;
if(qx + px < -28) px = -28 - qx;
if(qy + py < -28) py = -28 - qy;
if(qx + px > X) px = X - qx;
if(qy + py > Y) py = Y - qy;
} else {
qx = (s->mb_x << 6);
qy = (s->mb_y << 6);
X = (s->mb_width << 6) - 4;
Y = (s->mb_height << 6) - 4;
if(qx + px < -60) px = -60 - qx;
if(qy + py < -60) py = -60 - qy;
if(qx + px > X) px = X - qx;
if(qy + py > Y) py = Y - qy;
}
}
/* Calculate hybrid prediction as specified in 8.3.5.3.5 */
if(0 && !s->first_slice_line && s->mb_x) {
if(is_intra[xy - wrap])
sum = FFABS(px) + FFABS(py);
else
sum = FFABS(px - A[0]) + FFABS(py - A[1]);
if(sum > 32) {
if(get_bits1(&s->gb)) {
px = A[0];
py = A[1];
} else {
px = C[0];
py = C[1];
}
} else {
if(is_intra[xy - 2])
sum = FFABS(px) + FFABS(py);
else
sum = FFABS(px - C[0]) + FFABS(py - C[1]);
if(sum > 32) {
if(get_bits1(&s->gb)) {
px = A[0];
py = A[1];
} else {
px = C[0];
py = C[1];
}
}
}
}
/* store MV using signed modulus of MV range defined in 4.11 */
s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
}
if((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
C = s->current_picture.motion_val[1][xy - 2];
A = s->current_picture.motion_val[1][xy - wrap*2];
off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
B = s->current_picture.motion_val[1][xy - wrap*2 + off];
if(!s->mb_x) C[0] = C[1] = 0;
if(!s->first_slice_line) { // predictor A is not out of bounds
if(s->mb_width == 1) {
px = A[0];
py = A[1];
} else {
px = mid_pred(A[0], B[0], C[0]);
py = mid_pred(A[1], B[1], C[1]);
}
} else if(s->mb_x) { // predictor C is not out of bounds
px = C[0];
py = C[1];
} else {
px = py = 0;
}
/* Pullback MV as specified in 8.3.5.3.4 */
{
int qx, qy, X, Y;
if(v->profile < PROFILE_ADVANCED) {
qx = (s->mb_x << 5);
qy = (s->mb_y << 5);
X = (s->mb_width << 5) - 4;
Y = (s->mb_height << 5) - 4;
if(qx + px < -28) px = -28 - qx;
if(qy + py < -28) py = -28 - qy;
if(qx + px > X) px = X - qx;
if(qy + py > Y) py = Y - qy;
} else {
qx = (s->mb_x << 6);
qy = (s->mb_y << 6);
X = (s->mb_width << 6) - 4;
Y = (s->mb_height << 6) - 4;
if(qx + px < -60) px = -60 - qx;
if(qy + py < -60) py = -60 - qy;
if(qx + px > X) px = X - qx;
if(qy + py > Y) py = Y - qy;
}
}
/* Calculate hybrid prediction as specified in 8.3.5.3.5 */
if(0 && !s->first_slice_line && s->mb_x) {
if(is_intra[xy - wrap])
sum = FFABS(px) + FFABS(py);
else
sum = FFABS(px - A[0]) + FFABS(py - A[1]);
if(sum > 32) {
if(get_bits1(&s->gb)) {
px = A[0];
py = A[1];
} else {
px = C[0];
py = C[1];
}
} else {
if(is_intra[xy - 2])
sum = FFABS(px) + FFABS(py);
else
sum = FFABS(px - C[0]) + FFABS(py - C[1]);
if(sum > 32) {
if(get_bits1(&s->gb)) {
px = A[0];
py = A[1];
} else {
px = C[0];
py = C[1];
}
}
}
}
/* store MV using signed modulus of MV range defined in 4.11 */
s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
}
s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
}
/** Get predicted DC value for I-frames only
* prediction dir: left=0, top=1
* @param s MpegEncContext
* @param overlap flag indicating that overlap filtering is used
* @param pq integer part of picture quantizer
* @param[in] n block index in the current MB
* @param dc_val_ptr Pointer to DC predictor
* @param dir_ptr Prediction direction for use in AC prediction
*/
static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
int16_t **dc_val_ptr, int *dir_ptr)
{
int a, b, c, wrap, pred, scale;
int16_t *dc_val;
static const uint16_t dcpred[32] = {
-1, 1024, 512, 341, 256, 205, 171, 146, 128,
114, 102, 93, 85, 79, 73, 68, 64,
60, 57, 54, 51, 49, 47, 45, 43,
41, 39, 38, 37, 35, 34, 33
};
/* find prediction - wmv3_dc_scale always used here in fact */
if (n < 4) scale = s->y_dc_scale;
else scale = s->c_dc_scale;
wrap = s->block_wrap[n];
dc_val= s->dc_val[0] + s->block_index[n];
/* B A
* C X
*/
c = dc_val[ - 1];
b = dc_val[ - 1 - wrap];
a = dc_val[ - wrap];
if (pq < 9 || !overlap)
{
/* Set outer values */
if (s->first_slice_line && (n!=2 && n!=3)) b=a=dcpred[scale];
if (s->mb_x == 0 && (n!=1 && n!=3)) b=c=dcpred[scale];
}
else
{
/* Set outer values */
if (s->first_slice_line && (n!=2 && n!=3)) b=a=0;
if (s->mb_x == 0 && (n!=1 && n!=3)) b=c=0;
}
if (abs(a - b) <= abs(b - c)) {
pred = c;
*dir_ptr = 1;//left
} else {
pred = a;
*dir_ptr = 0;//top
}
/* update predictor */
*dc_val_ptr = &dc_val[0];
return pred;
}
/** Get predicted DC value
* prediction dir: left=0, top=1
* @param s MpegEncContext
* @param overlap flag indicating that overlap filtering is used
* @param pq integer part of picture quantizer
* @param[in] n block index in the current MB
* @param a_avail flag indicating top block availability
* @param c_avail flag indicating left block availability
* @param dc_val_ptr Pointer to DC predictor
* @param dir_ptr Prediction direction for use in AC prediction
*/
static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
int a_avail, int c_avail,
int16_t **dc_val_ptr, int *dir_ptr)
{
int a, b, c, wrap, pred;
int16_t *dc_val;
int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
int q1, q2 = 0;
wrap = s->block_wrap[n];
dc_val= s->dc_val[0] + s->block_index[n];
/* B A
* C X
*/
c = dc_val[ - 1];
b = dc_val[ - 1 - wrap];
a = dc_val[ - wrap];
/* scale predictors if needed */
q1 = s->current_picture.qscale_table[mb_pos];
if(c_avail && (n!= 1 && n!=3)) {
q2 = s->current_picture.qscale_table[mb_pos - 1];
if(q2 && q2 != q1)
c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
}
if(a_avail && (n!= 2 && n!=3)) {
q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
if(q2 && q2 != q1)
a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
}
if(a_avail && c_avail && (n!=3)) {
int off = mb_pos;
if(n != 1) off--;
if(n != 2) off -= s->mb_stride;
q2 = s->current_picture.qscale_table[off];
if(q2 && q2 != q1)
b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
}
if(a_avail && c_avail) {
if(abs(a - b) <= abs(b - c)) {
pred = c;
*dir_ptr = 1;//left
} else {
pred = a;
*dir_ptr = 0;//top
}
} else if(a_avail) {
pred = a;
*dir_ptr = 0;//top
} else if(c_avail) {
pred = c;
*dir_ptr = 1;//left
} else {
pred = 0;
*dir_ptr = 1;//left
}
/* update predictor */
*dc_val_ptr = &dc_val[0];
return pred;
}
/** @} */ // Block group
/**
* @defgroup vc1_std_mb VC1 Macroblock-level functions in Simple/Main Profiles
* @see 7.1.4, p91 and 8.1.1.7, p(1)04
* @{
*/
static inline int vc1_coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
{
int xy, wrap, pred, a, b, c;
xy = s->block_index[n];
wrap = s->b8_stride;
/* B C
* A X
*/
a = s->coded_block[xy - 1 ];
b = s->coded_block[xy - 1 - wrap];
c = s->coded_block[xy - wrap];
if (b == c) {
pred = a;
} else {
pred = c;
}
/* store value */
*coded_block_ptr = &s->coded_block[xy];
return pred;
}
/**
* Decode one AC coefficient
* @param v The VC1 context
* @param last Last coefficient
* @param skip How much zero coefficients to skip
* @param value Decoded AC coefficient value
* @param codingset set of VLC to decode data
* @see 8.1.3.4
*/
static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip, int *value, int codingset)
{
GetBitContext *gb = &v->s.gb;
int index, escape, run = 0, level = 0, lst = 0;
index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
if (index != vc1_ac_sizes[codingset] - 1) {
run = vc1_index_decode_table[codingset][index][0];
level = vc1_index_decode_table[codingset][index][1];
lst = index >= vc1_last_decode_table[codingset];
if(get_bits1(gb))
level = -level;
} else {
escape = decode210(gb);
if (escape != 2) {
index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
run = vc1_index_decode_table[codingset][index][0];
level = vc1_index_decode_table[codingset][index][1];
lst = index >= vc1_last_decode_table[codingset];
if(escape == 0) {
if(lst)
level += vc1_last_delta_level_table[codingset][run];
else
level += vc1_delta_level_table[codingset][run];
} else {
if(lst)
run += vc1_last_delta_run_table[codingset][level] + 1;
else
run += vc1_delta_run_table[codingset][level] + 1;
}
if(get_bits1(gb))
level = -level;
} else {
int sign;
lst = get_bits1(gb);
if(v->s.esc3_level_length == 0) {
if(v->pq < 8 || v->dquantfrm) { // table 59
v->s.esc3_level_length = get_bits(gb, 3);
if(!v->s.esc3_level_length)
v->s.esc3_level_length = get_bits(gb, 2) + 8;
} else { //table 60
v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;
}
v->s.esc3_run_length = 3 + get_bits(gb, 2);
}
run = get_bits(gb, v->s.esc3_run_length);
sign = get_bits1(gb);
level = get_bits(gb, v->s.esc3_level_length);
if(sign)
level = -level;
}
}
*last = lst;
*skip = run;
*value = level;
}
/** Decode intra block in intra frames - should be faster than decode_intra_block
* @param v VC1Context
* @param block block to decode
* @param[in] n subblock index
* @param coded are AC coeffs present or not
* @param codingset set of VLC to decode data
*/
static int vc1_decode_i_block(VC1Context *v, DCTELEM block[64], int n, int coded, int codingset)
{
GetBitContext *gb = &v->s.gb;
MpegEncContext *s = &v->s;
int dc_pred_dir = 0; /* Direction of the DC prediction used */
int i;
int16_t *dc_val;
int16_t *ac_val, *ac_val2;
int dcdiff;
/* Get DC differential */
if (n < 4) {
dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
} else {
dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
}
if (dcdiff < 0){
av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
return -1;
}
if (dcdiff)
{
if (dcdiff == 119 /* ESC index value */)
{
/* TODO: Optimize */
if (v->pq == 1) dcdiff = get_bits(gb, 10);
else if (v->pq == 2) dcdiff = get_bits(gb, 9);
else dcdiff = get_bits(gb, 8);
}
else
{
if (v->pq == 1)
dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
else if (v->pq == 2)
dcdiff = (dcdiff<<1) + get_bits1(gb) - 1;
}
if (get_bits1(gb))
dcdiff = -dcdiff;
}
/* Prediction */
dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
*dc_val = dcdiff;
/* Store the quantized DC coeff, used for prediction */
if (n < 4) {
block[0] = dcdiff * s->y_dc_scale;
} else {
block[0] = dcdiff * s->c_dc_scale;
}
/* Skip ? */
if (!coded) {
goto not_coded;
}
//AC Decoding
i = 1;
{
int last = 0, skip, value;
const int8_t *zz_table;
int scale;
int k;
scale = v->pq * 2 + v->halfpq;
if(v->s.ac_pred) {
if(!dc_pred_dir)
zz_table = wmv1_scantable[2];
else
zz_table = wmv1_scantable[3];
} else
zz_table = wmv1_scantable[1];
ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
ac_val2 = ac_val;
if(dc_pred_dir) //left
ac_val -= 16;
else //top
ac_val -= 16 * s->block_wrap[n];
while (!last) {
vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
i += skip;
if(i > 63)
break;
block[zz_table[i++]] = value;
}
/* apply AC prediction if needed */
if(s->ac_pred) {
if(dc_pred_dir) { //left
for(k = 1; k < 8; k++)
block[k << 3] += ac_val[k];
} else { //top
for(k = 1; k < 8; k++)
block[k] += ac_val[k + 8];
}
}
/* save AC coeffs for further prediction */
for(k = 1; k < 8; k++) {
ac_val2[k] = block[k << 3];
ac_val2[k + 8] = block[k];
}
/* scale AC coeffs */
for(k = 1; k < 64; k++)
if(block[k]) {
block[k] *= scale;
if(!v->pquantizer)
block[k] += (block[k] < 0) ? -v->pq : v->pq;
}
if(s->ac_pred) i = 63;
}
not_coded:
if(!coded) {
int k, scale;
ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
ac_val2 = ac_val;
i = 0;
scale = v->pq * 2 + v->halfpq;
memset(ac_val2, 0, 16 * 2);
if(dc_pred_dir) {//left
ac_val -= 16;
if(s->ac_pred)
memcpy(ac_val2, ac_val, 8 * 2);
} else {//top
ac_val -= 16 * s->block_wrap[n];
if(s->ac_pred)
memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
}
/* apply AC prediction if needed */
if(s->ac_pred) {
if(dc_pred_dir) { //left
for(k = 1; k < 8; k++) {
block[k << 3] = ac_val[k] * scale;
if(!v->pquantizer && block[k << 3])
block[k << 3] += (block[k << 3] < 0) ? -v->pq : v->pq;
}
} else { //top
for(k = 1; k < 8; k++) {
block[k] = ac_val[k + 8] * scale;
if(!v->pquantizer && block[k])
block[k] += (block[k] < 0) ? -v->pq : v->pq;
}
}
i = 63;
}
}
s->block_last_index[n] = i;
return 0;
}
/** Decode intra block in intra frames - should be faster than decode_intra_block
* @param v VC1Context
* @param block block to decode
* @param[in] n subblock number
* @param coded are AC coeffs present or not
* @param codingset set of VLC to decode data
* @param mquant quantizer value for this macroblock
*/
static int vc1_decode_i_block_adv(VC1Context *v, DCTELEM block[64], int n, int coded, int codingset, int mquant)
{
GetBitContext *gb = &v->s.gb;
MpegEncContext *s = &v->s;
int dc_pred_dir = 0; /* Direction of the DC prediction used */
int i;
int16_t *dc_val;
int16_t *ac_val, *ac_val2;
int dcdiff;
int a_avail = v->a_avail, c_avail = v->c_avail;
int use_pred = s->ac_pred;
int scale;
int q1, q2 = 0;
int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
/* Get DC differential */
if (n < 4) {
dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
} else {
dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
}
if (dcdiff < 0){
av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
return -1;
}
if (dcdiff)
{
if (dcdiff == 119 /* ESC index value */)
{
/* TODO: Optimize */
if (mquant == 1) dcdiff = get_bits(gb, 10);
else if (mquant == 2) dcdiff = get_bits(gb, 9);
else dcdiff = get_bits(gb, 8);
}
else
{
if (mquant == 1)
dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
else if (mquant == 2)
dcdiff = (dcdiff<<1) + get_bits1(gb) - 1;
}
if (get_bits1(gb))
dcdiff = -dcdiff;
}
/* Prediction */
dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
*dc_val = dcdiff;
/* Store the quantized DC coeff, used for prediction */
if (n < 4) {
block[0] = dcdiff * s->y_dc_scale;
} else {
block[0] = dcdiff * s->c_dc_scale;
}
//AC Decoding
i = 1;
/* check if AC is needed at all */
if(!a_avail && !c_avail) use_pred = 0;
ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
ac_val2 = ac_val;
scale = mquant * 2 + ((mquant == v->pq) ? v->halfpq : 0);
if(dc_pred_dir) //left
ac_val -= 16;
else //top
ac_val -= 16 * s->block_wrap[n];
q1 = s->current_picture.qscale_table[mb_pos];
if(dc_pred_dir && c_avail && mb_pos) q2 = s->current_picture.qscale_table[mb_pos - 1];
if(!dc_pred_dir && a_avail && mb_pos >= s->mb_stride) q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
if(dc_pred_dir && n==1) q2 = q1;
if(!dc_pred_dir && n==2) q2 = q1;
if(n==3) q2 = q1;
if(coded) {
int last = 0, skip, value;
const int8_t *zz_table;
int k;
if(v->s.ac_pred) {
if(!dc_pred_dir)
zz_table = wmv1_scantable[2];
else
zz_table = wmv1_scantable[3];
} else
zz_table = wmv1_scantable[1];
while (!last) {
vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
i += skip;
if(i > 63)
break;
block[zz_table[i++]] = value;
}
/* apply AC prediction if needed */
if(use_pred) {
/* scale predictors if needed*/
if(q2 && q1!=q2) {
q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
if(dc_pred_dir) { //left
for(k = 1; k < 8; k++)
block[k << 3] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
} else { //top
for(k = 1; k < 8; k++)
block[k] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
}
} else {
if(dc_pred_dir) { //left
for(k = 1; k < 8; k++)
block[k << 3] += ac_val[k];
} else { //top
for(k = 1; k < 8; k++)
block[k] += ac_val[k + 8];
}
}
}
/* save AC coeffs for further prediction */
for(k = 1; k < 8; k++) {
ac_val2[k] = block[k << 3];
ac_val2[k + 8] = block[k];
}
/* scale AC coeffs */
for(k = 1; k < 64; k++)
if(block[k]) {
block[k] *= scale;
if(!v->pquantizer)
block[k] += (block[k] < 0) ? -mquant : mquant;
}
if(use_pred) i = 63;
} else { // no AC coeffs
int k;
memset(ac_val2, 0, 16 * 2);
if(dc_pred_dir) {//left
if(use_pred) {
memcpy(ac_val2, ac_val, 8 * 2);
if(q2 && q1!=q2) {
q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
for(k = 1; k < 8; k++)
ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
}
}
} else {//top
if(use_pred) {
memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
if(q2 && q1!=q2) {
q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
for(k = 1; k < 8; k++)
ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
}
}
}
/* apply AC prediction if needed */
if(use_pred) {
if(dc_pred_dir) { //left
for(k = 1; k < 8; k++) {
block[k << 3] = ac_val2[k] * scale;
if(!v->pquantizer && block[k << 3])
block[k << 3] += (block[k << 3] < 0) ? -mquant : mquant;
}
} else { //top
for(k = 1; k < 8; k++) {
block[k] = ac_val2[k + 8] * scale;
if(!v->pquantizer && block[k])
block[k] += (block[k] < 0) ? -mquant : mquant;
}
}
i = 63;
}
}
s->block_last_index[n] = i;
return 0;
}
/** Decode intra block in inter frames - more generic version than vc1_decode_i_block
* @param v VC1Context
* @param block block to decode
* @param[in] n subblock index
* @param coded are AC coeffs present or not
* @param mquant block quantizer
* @param codingset set of VLC to decode data
*/
static int vc1_decode_intra_block(VC1Context *v, DCTELEM block[64], int n, int coded, int mquant, int codingset)
{
GetBitContext *gb = &v->s.gb;
MpegEncContext *s = &v->s;
int dc_pred_dir = 0; /* Direction of the DC prediction used */
int i;
int16_t *dc_val;
int16_t *ac_val, *ac_val2;
int dcdiff;
int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
int a_avail = v->a_avail, c_avail = v->c_avail;
int use_pred = s->ac_pred;
int scale;
int q1, q2 = 0;
s->dsp.clear_block(block);
/* XXX: Guard against dumb values of mquant */
mquant = (mquant < 1) ? 0 : ( (mquant>31) ? 31 : mquant );
/* Set DC scale - y and c use the same */
s->y_dc_scale = s->y_dc_scale_table[mquant];
s->c_dc_scale = s->c_dc_scale_table[mquant];
/* Get DC differential */
if (n < 4) {
dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
} else {
dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
}
if (dcdiff < 0){
av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
return -1;
}
if (dcdiff)
{
if (dcdiff == 119 /* ESC index value */)
{
/* TODO: Optimize */
if (mquant == 1) dcdiff = get_bits(gb, 10);
else if (mquant == 2) dcdiff = get_bits(gb, 9);
else dcdiff = get_bits(gb, 8);
}
else
{
if (mquant == 1)
dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
else if (mquant == 2)
dcdiff = (dcdiff<<1) + get_bits1(gb) - 1;
}
if (get_bits1(gb))
dcdiff = -dcdiff;
}
/* Prediction */
dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
*dc_val = dcdiff;
/* Store the quantized DC coeff, used for prediction */
if (n < 4) {
block[0] = dcdiff * s->y_dc_scale;
} else {
block[0] = dcdiff * s->c_dc_scale;
}
//AC Decoding
i = 1;
/* check if AC is needed at all and adjust direction if needed */
if(!a_avail) dc_pred_dir = 1;
if(!c_avail) dc_pred_dir = 0;
if(!a_avail && !c_avail) use_pred = 0;
ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
ac_val2 = ac_val;
scale = mquant * 2 + v->halfpq;
if(dc_pred_dir) //left
ac_val -= 16;
else //top
ac_val -= 16 * s->block_wrap[n];
q1 = s->current_picture.qscale_table[mb_pos];
if(dc_pred_dir && c_avail && mb_pos) q2 = s->current_picture.qscale_table[mb_pos - 1];
if(!dc_pred_dir && a_avail && mb_pos >= s->mb_stride) q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
if(dc_pred_dir && n==1) q2 = q1;
if(!dc_pred_dir && n==2) q2 = q1;
if(n==3) q2 = q1;
if(coded) {
int last = 0, skip, value;
const int8_t *zz_table;
int k;
zz_table = wmv1_scantable[0];
while (!last) {
vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
i += skip;
if(i > 63)
break;
block[zz_table[i++]] = value;
}
/* apply AC prediction if needed */
if(use_pred) {
/* scale predictors if needed*/
if(q2 && q1!=q2) {
q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
if(dc_pred_dir) { //left
for(k = 1; k < 8; k++)
block[k << 3] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
} else { //top
for(k = 1; k < 8; k++)
block[k] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
}
} else {
if(dc_pred_dir) { //left
for(k = 1; k < 8; k++)
block[k << 3] += ac_val[k];
} else { //top
for(k = 1; k < 8; k++)
block[k] += ac_val[k + 8];
}
}
}
/* save AC coeffs for further prediction */
for(k = 1; k < 8; k++) {
ac_val2[k] = block[k << 3];
ac_val2[k + 8] = block[k];
}
/* scale AC coeffs */
for(k = 1; k < 64; k++)
if(block[k]) {
block[k] *= scale;
if(!v->pquantizer)
block[k] += (block[k] < 0) ? -mquant : mquant;
}
if(use_pred) i = 63;
} else { // no AC coeffs
int k;
memset(ac_val2, 0, 16 * 2);
if(dc_pred_dir) {//left
if(use_pred) {
memcpy(ac_val2, ac_val, 8 * 2);
if(q2 && q1!=q2) {
q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
for(k = 1; k < 8; k++)
ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
}
}
} else {//top
if(use_pred) {
memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
if(q2 && q1!=q2) {
q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
for(k = 1; k < 8; k++)
ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
}
}
}
/* apply AC prediction if needed */
if(use_pred) {
if(dc_pred_dir) { //left
for(k = 1; k < 8; k++) {
block[k << 3] = ac_val2[k] * scale;
if(!v->pquantizer && block[k << 3])
block[k << 3] += (block[k << 3] < 0) ? -mquant : mquant;
}
} else { //top
for(k = 1; k < 8; k++) {
block[k] = ac_val2[k + 8] * scale;
if(!v->pquantizer && block[k])
block[k] += (block[k] < 0) ? -mquant : mquant;
}
}
i = 63;
}
}
s->block_last_index[n] = i;
return 0;
}
/** Decode P block
*/
static int vc1_decode_p_block(VC1Context *v, DCTELEM block[64], int n, int mquant, int ttmb, int first_block,
uint8_t *dst, int linesize, int skip_block, int apply_filter, int cbp_top, int cbp_left)
{
MpegEncContext *s = &v->s;
GetBitContext *gb = &s->gb;
int i, j;
int subblkpat = 0;
int scale, off, idx, last, skip, value;
int ttblk = ttmb & 7;
int pat = 0;
s->dsp.clear_block(block);
if(ttmb == -1) {
ttblk = ff_vc1_ttblk_to_tt[v->tt_index][get_vlc2(gb, ff_vc1_ttblk_vlc[v->tt_index].table, VC1_TTBLK_VLC_BITS, 1)];
}
if(ttblk == TT_4X4) {
subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
}
if((ttblk != TT_8X8 && ttblk != TT_4X4) && (v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))) {
subblkpat = decode012(gb);
if(subblkpat) subblkpat ^= 3; //swap decoded pattern bits
if(ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) ttblk = TT_8X4;
if(ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) ttblk = TT_4X8;
}
scale = 2 * mquant + ((v->pq == mquant) ? v->halfpq : 0);
// convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
if(ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
subblkpat = 2 - (ttblk == TT_8X4_TOP);
ttblk = TT_8X4;
}
if(ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
subblkpat = 2 - (ttblk == TT_4X8_LEFT);
ttblk = TT_4X8;
}
switch(ttblk) {
case TT_8X8:
pat = 0xF;
i = 0;
last = 0;
while (!last) {
vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
i += skip;
if(i > 63)
break;
idx = wmv1_scantable[0][i++];
block[idx] = value * scale;
if(!v->pquantizer)
block[idx] += (block[idx] < 0) ? -mquant : mquant;
}
if(!skip_block){
if(i==1)
s->dsp.vc1_inv_trans_8x8_dc(dst, linesize, block);
else{
s->dsp.vc1_inv_trans_8x8(block);
s->dsp.add_pixels_clamped(block, dst, linesize);
}
if(apply_filter && cbp_top & 0xC)
s->dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
if(apply_filter && cbp_left & 0xA)
s->dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
}
break;
case TT_4X4:
pat = ~subblkpat & 0xF;
for(j = 0; j < 4; j++) {
last = subblkpat & (1 << (3 - j));
i = 0;
off = (j & 1) * 4 + (j & 2) * 16;
while (!last) {
vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
i += skip;
if(i > 15)
break;
idx = ff_vc1_simple_progressive_4x4_zz[i++];
block[idx + off] = value * scale;
if(!v->pquantizer)
block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
}
if(!(subblkpat & (1 << (3 - j))) && !skip_block){
if(i==1)
s->dsp.vc1_inv_trans_4x4_dc(dst + (j&1)*4 + (j&2)*2*linesize, linesize, block + off);
else
s->dsp.vc1_inv_trans_4x4(dst + (j&1)*4 + (j&2)*2*linesize, linesize, block + off);
if(apply_filter && (j&2 ? pat & (1<<(j-2)) : (cbp_top & (1 << (j + 2)))))
s->dsp.vc1_v_loop_filter4(dst + (j&1)*4 + (j&2)*2*linesize, linesize, v->pq);
if(apply_filter && (j&1 ? pat & (1<<(j-1)) : (cbp_left & (1 << (j + 1)))))
s->dsp.vc1_h_loop_filter4(dst + (j&1)*4 + (j&2)*2*linesize, linesize, v->pq);
}
}
break;
case TT_8X4:
pat = ~((subblkpat & 2)*6 + (subblkpat & 1)*3) & 0xF;
for(j = 0; j < 2; j++) {
last = subblkpat & (1 << (1 - j));
i = 0;
off = j * 32;
while (!last) {
vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
i += skip;
if(i > 31)
break;
idx = v->zz_8x4[i++]+off;
block[idx] = value * scale;
if(!v->pquantizer)
block[idx] += (block[idx] < 0) ? -mquant : mquant;
}
if(!(subblkpat & (1 << (1 - j))) && !skip_block){
if(i==1)
s->dsp.vc1_inv_trans_8x4_dc(dst + j*4*linesize, linesize, block + off);
else
s->dsp.vc1_inv_trans_8x4(dst + j*4*linesize, linesize, block + off);
if(apply_filter && j ? pat & 0x3 : (cbp_top & 0xC))
s->dsp.vc1_v_loop_filter8(dst + j*4*linesize, linesize, v->pq);
if(apply_filter && cbp_left & (2 << j))
s->dsp.vc1_h_loop_filter4(dst + j*4*linesize, linesize, v->pq);
}
}
break;
case TT_4X8:
pat = ~(subblkpat*5) & 0xF;
for(j = 0; j < 2; j++) {
last = subblkpat & (1 << (1 - j));
i = 0;
off = j * 4;
while (!last) {
vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
i += skip;
if(i > 31)
break;
idx = v->zz_4x8[i++]+off;
block[idx] = value * scale;
if(!v->pquantizer)
block[idx] += (block[idx] < 0) ? -mquant : mquant;
}
if(!(subblkpat & (1 << (1 - j))) && !skip_block){
if(i==1)
s->dsp.vc1_inv_trans_4x8_dc(dst + j*4, linesize, block + off);
else
s->dsp.vc1_inv_trans_4x8(dst + j*4, linesize, block + off);
if(apply_filter && cbp_top & (2 << j))
s->dsp.vc1_v_loop_filter4(dst + j*4, linesize, v->pq);
if(apply_filter && j ? pat & 0x5 : (cbp_left & 0xA))
s->dsp.vc1_h_loop_filter8(dst + j*4, linesize, v->pq);
}
}
break;
}
return pat;
}
/** @} */ // Macroblock group
static const int size_table [6] = { 0, 2, 3, 4, 5, 8 };
static const int offset_table[6] = { 0, 1, 3, 7, 15, 31 };
/** Decode one P-frame MB (in Simple/Main profile)
*/
static int vc1_decode_p_mb(VC1Context *v)
{
MpegEncContext *s = &v->s;
GetBitContext *gb = &s->gb;
int i, j;
int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
int cbp; /* cbp decoding stuff */
int mqdiff, mquant; /* MB quantization */
int ttmb = v->ttfrm; /* MB Transform type */
int mb_has_coeffs = 1; /* last_flag */
int dmv_x, dmv_y; /* Differential MV components */
int index, index1; /* LUT indexes */
int val, sign; /* temp values */
int first_block = 1;
int dst_idx, off;
int skipped, fourmv;
int block_cbp = 0, pat;
int apply_loop_filter;
mquant = v->pq; /* Loosy initialization */
if (v->mv_type_is_raw)
fourmv = get_bits1(gb);
else
fourmv = v->mv_type_mb_plane[mb_pos];
if (v->skip_is_raw)
skipped = get_bits1(gb);
else
skipped = v->s.mbskip_table[mb_pos];
apply_loop_filter = s->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY);
if (!fourmv) /* 1MV mode */
{
if (!skipped)
{
GET_MVDATA(dmv_x, dmv_y);
if (s->mb_intra) {
s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
}
s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
vc1_pred_mv(s, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0]);
/* FIXME Set DC val for inter block ? */
if (s->mb_intra && !mb_has_coeffs)
{
GET_MQUANT();
s->ac_pred = get_bits1(gb);
cbp = 0;
}
else if (mb_has_coeffs)
{
if (s->mb_intra) s->ac_pred = get_bits1(gb);
cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
GET_MQUANT();
}
else
{
mquant = v->pq;
cbp = 0;
}
s->current_picture.qscale_table[mb_pos] = mquant;
if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
VC1_TTMB_VLC_BITS, 2);
if(!s->mb_intra) vc1_mc_1mv(v, 0);
dst_idx = 0;
for (i=0; i<6; i++)
{
s->dc_val[0][s->block_index[i]] = 0;
dst_idx += i >> 2;
val = ((cbp >> (5 - i)) & 1);
off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
v->mb_type[0][s->block_index[i]] = s->mb_intra;
if(s->mb_intra) {
/* check if prediction blocks A and C are available */
v->a_avail = v->c_avail = 0;
if(i == 2 || i == 3 || !s->first_slice_line)
v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
if(i == 1 || i == 3 || s->mb_x)
v->c_avail = v->mb_type[0][s->block_index[i] - 1];
vc1_decode_intra_block(v, s->block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);
if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
s->dsp.vc1_inv_trans_8x8(s->block[i]);
if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;
s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
if(v->pq >= 9 && v->overlap) {
if(v->c_avail)
s->dsp.vc1_h_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
if(v->a_avail)
s->dsp.vc1_v_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
}
if(apply_loop_filter && s->mb_x && s->mb_x != (s->mb_width - 1) && s->mb_y && s->mb_y != (s->mb_height - 1)){
int left_cbp, top_cbp;
if(i & 4){
left_cbp = v->cbp[s->mb_x - 1] >> (i * 4);
top_cbp = v->cbp[s->mb_x - s->mb_stride] >> (i * 4);
}else{
left_cbp = (i & 1) ? (cbp >> ((i-1)*4)) : (v->cbp[s->mb_x - 1] >> ((i+1)*4));
top_cbp = (i & 2) ? (cbp >> ((i-2)*4)) : (v->cbp[s->mb_x - s->mb_stride] >> ((i+2)*4));
}
if(left_cbp & 0xC)
s->dsp.vc1_v_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
if(top_cbp & 0xA)
s->dsp.vc1_h_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
}
block_cbp |= 0xF << (i << 2);
} else if(val) {
int left_cbp = 0, top_cbp = 0, filter = 0;
if(apply_loop_filter && s->mb_x && s->mb_x != (s->mb_width - 1) && s->mb_y && s->mb_y != (s->mb_height - 1)){
filter = 1;
if(i & 4){
left_cbp = v->cbp[s->mb_x - 1] >> (i * 4);
top_cbp = v->cbp[s->mb_x - s->mb_stride] >> (i * 4);
}else{
left_cbp = (i & 1) ? (cbp >> ((i-1)*4)) : (v->cbp[s->mb_x - 1] >> ((i+1)*4));
top_cbp = (i & 2) ? (cbp >> ((i-2)*4)) : (v->cbp[s->mb_x - s->mb_stride] >> ((i+2)*4));
}
if(left_cbp & 0xC)
s->dsp.vc1_v_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
if(top_cbp & 0xA)
s->dsp.vc1_h_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
}
pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block, s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize, (i&4) && (s->flags & CODEC_FLAG_GRAY), filter, left_cbp, top_cbp);
block_cbp |= pat << (i << 2);
if(!v->ttmbf && ttmb < 8) ttmb = -1;
first_block = 0;
}
}
}
else //Skipped
{
s->mb_intra = 0;
for(i = 0; i < 6; i++) {
v->mb_type[0][s->block_index[i]] = 0;
s->dc_val[0][s->block_index[i]] = 0;
}
s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
s->current_picture.qscale_table[mb_pos] = 0;
vc1_pred_mv(s, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0]);
vc1_mc_1mv(v, 0);
return 0;
}
} //1MV mode
else //4MV mode
{
if (!skipped /* unskipped MB */)
{
int intra_count = 0, coded_inter = 0;
int is_intra[6], is_coded[6];
/* Get CBPCY */
cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
for (i=0; i<6; i++)
{
val = ((cbp >> (5 - i)) & 1);
s->dc_val[0][s->block_index[i]] = 0;
s->mb_intra = 0;
if(i < 4) {
dmv_x = dmv_y = 0;
s->mb_intra = 0;
mb_has_coeffs = 0;
if(val) {
GET_MVDATA(dmv_x, dmv_y);
}
vc1_pred_mv(s, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0]);
if(!s->mb_intra) vc1_mc_4mv_luma(v, i);
intra_count += s->mb_intra;
is_intra[i] = s->mb_intra;
is_coded[i] = mb_has_coeffs;
}
if(i&4){
is_intra[i] = (intra_count >= 3);
is_coded[i] = val;
}
if(i == 4) vc1_mc_4mv_chroma(v);
v->mb_type[0][s->block_index[i]] = is_intra[i];
if(!coded_inter) coded_inter = !is_intra[i] & is_coded[i];
}
// if there are no coded blocks then don't do anything more
if(!intra_count && !coded_inter) return 0;
dst_idx = 0;
GET_MQUANT();
s->current_picture.qscale_table[mb_pos] = mquant;
/* test if block is intra and has pred */
{
int intrapred = 0;
for(i=0; i<6; i++)
if(is_intra[i]) {
if(((!s->first_slice_line || (i==2 || i==3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
|| ((s->mb_x || (i==1 || i==3)) && v->mb_type[0][s->block_index[i] - 1])) {
intrapred = 1;
break;
}
}
if(intrapred)s->ac_pred = get_bits1(gb);
else s->ac_pred = 0;
}
if (!v->ttmbf && coded_inter)
ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
for (i=0; i<6; i++)
{
dst_idx += i >> 2;
off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
s->mb_intra = is_intra[i];
if (is_intra[i]) {
/* check if prediction blocks A and C are available */
v->a_avail = v->c_avail = 0;
if(i == 2 || i == 3 || !s->first_slice_line)
v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
if(i == 1 || i == 3 || s->mb_x)
v->c_avail = v->mb_type[0][s->block_index[i] - 1];
vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant, (i&4)?v->codingset2:v->codingset);
if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
s->dsp.vc1_inv_trans_8x8(s->block[i]);
if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;
s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize);
if(v->pq >= 9 && v->overlap) {
if(v->c_avail)
s->dsp.vc1_h_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
if(v->a_avail)
s->dsp.vc1_v_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
}
if(v->s.loop_filter && s->mb_x && s->mb_x != (s->mb_width - 1) && s->mb_y && s->mb_y != (s->mb_height - 1)){
int left_cbp, top_cbp;
if(i & 4){
left_cbp = v->cbp[s->mb_x - 1] >> (i * 4);
top_cbp = v->cbp[s->mb_x - s->mb_stride] >> (i * 4);
}else{
left_cbp = (i & 1) ? (cbp >> ((i-1)*4)) : (v->cbp[s->mb_x - 1] >> ((i+1)*4));
top_cbp = (i & 2) ? (cbp >> ((i-2)*4)) : (v->cbp[s->mb_x - s->mb_stride] >> ((i+2)*4));
}
if(left_cbp & 0xC)
s->dsp.vc1_v_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
if(top_cbp & 0xA)
s->dsp.vc1_h_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
}
block_cbp |= 0xF << (i << 2);
} else if(is_coded[i]) {
int left_cbp = 0, top_cbp = 0, filter = 0;
if(v->s.loop_filter && s->mb_x && s->mb_x != (s->mb_width - 1) && s->mb_y && s->mb_y != (s->mb_height - 1)){
filter = 1;
if(i & 4){
left_cbp = v->cbp[s->mb_x - 1] >> (i * 4);
top_cbp = v->cbp[s->mb_x - s->mb_stride] >> (i * 4);
}else{
left_cbp = (i & 1) ? (cbp >> ((i-1)*4)) : (v->cbp[s->mb_x - 1] >> ((i+1)*4));
top_cbp = (i & 2) ? (cbp >> ((i-2)*4)) : (v->cbp[s->mb_x - s->mb_stride] >> ((i+2)*4));
}
if(left_cbp & 0xC)
s->dsp.vc1_v_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
if(top_cbp & 0xA)
s->dsp.vc1_h_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
}
pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block, s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize, (i&4) && (s->flags & CODEC_FLAG_GRAY), filter, left_cbp, top_cbp);
block_cbp |= pat << (i << 2);
if(!v->ttmbf && ttmb < 8) ttmb = -1;
first_block = 0;
}
}
return 0;
}
else //Skipped MB
{
s->mb_intra = 0;
s->current_picture.qscale_table[mb_pos] = 0;
for (i=0; i<6; i++) {
v->mb_type[0][s->block_index[i]] = 0;
s->dc_val[0][s->block_index[i]] = 0;
}
for (i=0; i<4; i++)
{
vc1_pred_mv(s, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0]);
vc1_mc_4mv_luma(v, i);
}
vc1_mc_4mv_chroma(v);
s->current_picture.qscale_table[mb_pos] = 0;
return 0;
}
}
v->cbp[s->mb_x] = block_cbp;
/* Should never happen */
return -1;
}
/** Decode one B-frame MB (in Main profile)
*/
static void vc1_decode_b_mb(VC1Context *v)
{
MpegEncContext *s = &v->s;
GetBitContext *gb = &s->gb;
int i, j;
int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
int cbp = 0; /* cbp decoding stuff */
int mqdiff, mquant; /* MB quantization */
int ttmb = v->ttfrm; /* MB Transform type */
int mb_has_coeffs = 0; /* last_flag */
int index, index1; /* LUT indexes */
int val, sign; /* temp values */
int first_block = 1;
int dst_idx, off;
int skipped, direct;
int dmv_x[2], dmv_y[2];
int bmvtype = BMV_TYPE_BACKWARD;
mquant = v->pq; /* Loosy initialization */
s->mb_intra = 0;
if (v->dmb_is_raw)
direct = get_bits1(gb);
else
direct = v->direct_mb_plane[mb_pos];
if (v->skip_is_raw)
skipped = get_bits1(gb);
else
skipped = v->s.mbskip_table[mb_pos];
dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
for(i = 0; i < 6; i++) {
v->mb_type[0][s->block_index[i]] = 0;
s->dc_val[0][s->block_index[i]] = 0;
}
s->current_picture.qscale_table[mb_pos] = 0;
if (!direct) {
if (!skipped) {
GET_MVDATA(dmv_x[0], dmv_y[0]);
dmv_x[1] = dmv_x[0];
dmv_y[1] = dmv_y[0];
}
if(skipped || !s->mb_intra) {
bmvtype = decode012(gb);
switch(bmvtype) {
case 0:
bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
break;
case 1:
bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
break;
case 2:
bmvtype = BMV_TYPE_INTERPOLATED;
dmv_x[0] = dmv_y[0] = 0;
}
}
}
for(i = 0; i < 6; i++)
v->mb_type[0][s->block_index[i]] = s->mb_intra;
if (skipped) {
if(direct) bmvtype = BMV_TYPE_INTERPOLATED;
vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
return;
}
if (direct) {
cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
GET_MQUANT();
s->mb_intra = 0;
s->current_picture.qscale_table[mb_pos] = mquant;
if(!v->ttmbf)
ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
} else {
if(!mb_has_coeffs && !s->mb_intra) {
/* no coded blocks - effectively skipped */
vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
return;
}
if(s->mb_intra && !mb_has_coeffs) {
GET_MQUANT();
s->current_picture.qscale_table[mb_pos] = mquant;
s->ac_pred = get_bits1(gb);
cbp = 0;
vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
} else {
if(bmvtype == BMV_TYPE_INTERPOLATED) {
GET_MVDATA(dmv_x[0], dmv_y[0]);
if(!mb_has_coeffs) {
/* interpolated skipped block */
vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
return;
}
}
vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
if(!s->mb_intra) {
vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
}
if(s->mb_intra)
s->ac_pred = get_bits1(gb);
cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
GET_MQUANT();
s->current_picture.qscale_table[mb_pos] = mquant;
if(!v->ttmbf && !s->mb_intra && mb_has_coeffs)
ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
}
}
dst_idx = 0;
for (i=0; i<6; i++)
{
s->dc_val[0][s->block_index[i]] = 0;
dst_idx += i >> 2;
val = ((cbp >> (5 - i)) & 1);
off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
v->mb_type[0][s->block_index[i]] = s->mb_intra;
if(s->mb_intra) {
/* check if prediction blocks A and C are available */
v->a_avail = v->c_avail = 0;
if(i == 2 || i == 3 || !s->first_slice_line)
v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
if(i == 1 || i == 3 || s->mb_x)
v->c_avail = v->mb_type[0][s->block_index[i] - 1];
vc1_decode_intra_block(v, s->block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);
if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
s->dsp.vc1_inv_trans_8x8(s->block[i]);
if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;
s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
} else if(val) {
vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block, s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize, (i&4) && (s->flags & CODEC_FLAG_GRAY), 0, 0, 0);
if(!v->ttmbf && ttmb < 8) ttmb = -1;
first_block = 0;
}
}
}
/** Decode blocks of I-frame
*/
static void vc1_decode_i_blocks(VC1Context *v)
{
int k, j;
MpegEncContext *s = &v->s;
int cbp, val;
uint8_t *coded_val;
int mb_pos;
/* select codingmode used for VLC tables selection */
switch(v->y_ac_table_index){
case 0:
v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
break;
case 1:
v->codingset = CS_HIGH_MOT_INTRA;
break;
case 2:
v->codingset = CS_MID_RATE_INTRA;
break;
}
switch(v->c_ac_table_index){
case 0:
v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
break;
case 1:
v->codingset2 = CS_HIGH_MOT_INTER;
break;
case 2:
v->codingset2 = CS_MID_RATE_INTER;
break;
}
/* Set DC scale - y and c use the same */
s->y_dc_scale = s->y_dc_scale_table[v->pq];
s->c_dc_scale = s->c_dc_scale_table[v->pq];
//do frame decode
s->mb_x = s->mb_y = 0;
s->mb_intra = 1;
s->first_slice_line = 1;
for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
s->mb_x = 0;
ff_init_block_index(s);
for(; s->mb_x < s->mb_width; s->mb_x++) {
ff_update_block_index(s);
s->dsp.clear_blocks(s->block[0]);
mb_pos = s->mb_x + s->mb_y * s->mb_width;
s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
s->current_picture.qscale_table[mb_pos] = v->pq;
s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
// do actual MB decoding and displaying
cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
v->s.ac_pred = get_bits1(&v->s.gb);
for(k = 0; k < 6; k++) {
val = ((cbp >> (5 - k)) & 1);
if (k < 4) {
int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
val = val ^ pred;
*coded_val = val;
}
cbp |= val << (5 - k);
vc1_decode_i_block(v, s->block[k], k, val, (k<4)? v->codingset : v->codingset2);
s->dsp.vc1_inv_trans_8x8(s->block[k]);
if(v->pq >= 9 && v->overlap) {
for(j = 0; j < 64; j++) s->block[k][j] += 128;
}
}
vc1_put_block(v, s->block);
if(v->pq >= 9 && v->overlap) {
if(s->mb_x) {
s->dsp.vc1_h_overlap(s->dest[0], s->linesize);
s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
if(!(s->flags & CODEC_FLAG_GRAY)) {
s->dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
s->dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
}
}
s->dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
if(!s->first_slice_line) {
s->dsp.vc1_v_overlap(s->dest[0], s->linesize);
s->dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
if(!(s->flags & CODEC_FLAG_GRAY)) {
s->dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
s->dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
}
}
s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
}
if(v->s.loop_filter) vc1_loop_filter_iblk(s, v->pq);
if(get_bits_count(&s->gb) > v->bits) {
ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END));
av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n", get_bits_count(&s->gb), v->bits);
return;
}
}
ff_draw_horiz_band(s, s->mb_y * 16, 16);
s->first_slice_line = 0;
}
ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
}
/** Decode blocks of I-frame for advanced profile
*/
static void vc1_decode_i_blocks_adv(VC1Context *v)
{
int k, j;
MpegEncContext *s = &v->s;
int cbp, val;
uint8_t *coded_val;
int mb_pos;
int mquant = v->pq;
int mqdiff;
int overlap;
GetBitContext *gb = &s->gb;
/* select codingmode used for VLC tables selection */
switch(v->y_ac_table_index){
case 0:
v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
break;
case 1:
v->codingset = CS_HIGH_MOT_INTRA;
break;
case 2:
v->codingset = CS_MID_RATE_INTRA;
break;
}
switch(v->c_ac_table_index){
case 0:
v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
break;
case 1:
v->codingset2 = CS_HIGH_MOT_INTER;
break;
case 2:
v->codingset2 = CS_MID_RATE_INTER;
break;
}
//do frame decode
s->mb_x = s->mb_y = 0;
s->mb_intra = 1;
s->first_slice_line = 1;
for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
s->mb_x = 0;
ff_init_block_index(s);
for(;s->mb_x < s->mb_width; s->mb_x++) {
ff_update_block_index(s);
s->dsp.clear_blocks(s->block[0]);
mb_pos = s->mb_x + s->mb_y * s->mb_stride;
s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
// do actual MB decoding and displaying
cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
if(v->acpred_is_raw)
v->s.ac_pred = get_bits1(&v->s.gb);
else
v->s.ac_pred = v->acpred_plane[mb_pos];
if(v->condover == CONDOVER_SELECT) {
if(v->overflg_is_raw)
overlap = get_bits1(&v->s.gb);
else
overlap = v->over_flags_plane[mb_pos];
} else
overlap = (v->condover == CONDOVER_ALL);
GET_MQUANT();
s->current_picture.qscale_table[mb_pos] = mquant;
/* Set DC scale - y and c use the same */
s->y_dc_scale = s->y_dc_scale_table[mquant];
s->c_dc_scale = s->c_dc_scale_table[mquant];
for(k = 0; k < 6; k++) {
val = ((cbp >> (5 - k)) & 1);
if (k < 4) {
int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
val = val ^ pred;
*coded_val = val;
}
cbp |= val << (5 - k);
v->a_avail = !s->first_slice_line || (k==2 || k==3);
v->c_avail = !!s->mb_x || (k==1 || k==3);
vc1_decode_i_block_adv(v, s->block[k], k, val, (k<4)? v->codingset : v->codingset2, mquant);
s->dsp.vc1_inv_trans_8x8(s->block[k]);
for(j = 0; j < 64; j++) s->block[k][j] += 128;
}
vc1_put_block(v, s->block);
if(overlap) {
if(s->mb_x) {
s->dsp.vc1_h_overlap(s->dest[0], s->linesize);
s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
if(!(s->flags & CODEC_FLAG_GRAY)) {
s->dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
s->dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
}
}
s->dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
if(!s->first_slice_line) {
s->dsp.vc1_v_overlap(s->dest[0], s->linesize);
s->dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
if(!(s->flags & CODEC_FLAG_GRAY)) {
s->dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
s->dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
}
}
s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
}
if(v->s.loop_filter) vc1_loop_filter_iblk(s, v->pq);
if(get_bits_count(&s->gb) > v->bits) {
ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END));
av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n", get_bits_count(&s->gb), v->bits);
return;
}
}
ff_draw_horiz_band(s, s->mb_y * 16, 16);
s->first_slice_line = 0;
}
ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
}
static void vc1_decode_p_blocks(VC1Context *v)
{
MpegEncContext *s = &v->s;
/* select codingmode used for VLC tables selection */
switch(v->c_ac_table_index){
case 0:
v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
break;
case 1:
v->codingset = CS_HIGH_MOT_INTRA;
break;
case 2:
v->codingset = CS_MID_RATE_INTRA;
break;
}
switch(v->c_ac_table_index){
case 0:
v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
break;
case 1:
v->codingset2 = CS_HIGH_MOT_INTER;
break;
case 2:
v->codingset2 = CS_MID_RATE_INTER;
break;
}
s->first_slice_line = 1;
memset(v->cbp_base, 0, sizeof(v->cbp_base[0])*2*s->mb_stride);
for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
s->mb_x = 0;
ff_init_block_index(s);
for(; s->mb_x < s->mb_width; s->mb_x++) {
ff_update_block_index(s);
vc1_decode_p_mb(v);
if(get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END));
av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n", get_bits_count(&s->gb), v->bits,s->mb_x,s->mb_y);
return;
}
}
memmove(v->cbp_base, v->cbp, sizeof(v->cbp_base[0])*s->mb_stride);
ff_draw_horiz_band(s, s->mb_y * 16, 16);
s->first_slice_line = 0;
}
ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
}
static void vc1_decode_b_blocks(VC1Context *v)
{
MpegEncContext *s = &v->s;
/* select codingmode used for VLC tables selection */
switch(v->c_ac_table_index){
case 0:
v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
break;
case 1:
v->codingset = CS_HIGH_MOT_INTRA;
break;
case 2:
v->codingset = CS_MID_RATE_INTRA;
break;
}
switch(v->c_ac_table_index){
case 0:
v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
break;
case 1:
v->codingset2 = CS_HIGH_MOT_INTER;
break;
case 2:
v->codingset2 = CS_MID_RATE_INTER;
break;
}
s->first_slice_line = 1;
for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
s->mb_x = 0;
ff_init_block_index(s);
for(; s->mb_x < s->mb_width; s->mb_x++) {
ff_update_block_index(s);
vc1_decode_b_mb(v);
if(get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END));
av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n", get_bits_count(&s->gb), v->bits,s->mb_x,s->mb_y);
return;
}
if(v->s.loop_filter) vc1_loop_filter_iblk(s, v->pq);
}
ff_draw_horiz_band(s, s->mb_y * 16, 16);
s->first_slice_line = 0;
}
ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
}
static void vc1_decode_skip_blocks(VC1Context *v)
{
MpegEncContext *s = &v->s;
ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
s->first_slice_line = 1;
for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
s->mb_x = 0;
ff_init_block_index(s);
ff_update_block_index(s);
memcpy(s->dest[0], s->last_picture.data[0] + s->mb_y * 16 * s->linesize, s->linesize * 16);
memcpy(s->dest[1], s->last_picture.data[1] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
memcpy(s->dest[2], s->last_picture.data[2] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
ff_draw_horiz_band(s, s->mb_y * 16, 16);
s->first_slice_line = 0;
}
s->pict_type = FF_P_TYPE;
}
static void vc1_decode_blocks(VC1Context *v)
{
v->s.esc3_level_length = 0;
if(v->x8_type){
ff_intrax8_decode_picture(&v->x8, 2*v->pq+v->halfpq, v->pq*(!v->pquantizer) );
}else{
switch(v->s.pict_type) {
case FF_I_TYPE:
if(v->profile == PROFILE_ADVANCED)
vc1_decode_i_blocks_adv(v);
else
vc1_decode_i_blocks(v);
break;
case FF_P_TYPE:
if(v->p_frame_skipped)
vc1_decode_skip_blocks(v);
else
vc1_decode_p_blocks(v);
break;
case FF_B_TYPE:
if(v->bi_type){
if(v->profile == PROFILE_ADVANCED)
vc1_decode_i_blocks_adv(v);
else
vc1_decode_i_blocks(v);
}else
vc1_decode_b_blocks(v);
break;
}
}
}
/** Initialize a VC1/WMV3 decoder
* @todo TODO: Handle VC-1 IDUs (Transport level?)
* @todo TODO: Decypher remaining bits in extra_data
*/
static av_cold int vc1_decode_init(AVCodecContext *avctx)
{
VC1Context *v = avctx->priv_data;
MpegEncContext *s = &v->s;
GetBitContext gb;
if (!avctx->extradata_size || !avctx->extradata) return -1;
if (!(avctx->flags & CODEC_FLAG_GRAY))
avctx->pix_fmt = avctx->get_format(avctx, avctx->codec->pix_fmts);
else
avctx->pix_fmt = PIX_FMT_GRAY8;
avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
v->s.avctx = avctx;
avctx->flags |= CODEC_FLAG_EMU_EDGE;
v->s.flags |= CODEC_FLAG_EMU_EDGE;
if(avctx->idct_algo==FF_IDCT_AUTO){
avctx->idct_algo=FF_IDCT_WMV2;
}
if(ff_msmpeg4_decode_init(avctx) < 0)
return -1;
if (vc1_init_common(v) < 0) return -1;
avctx->coded_width = avctx->width;
avctx->coded_height = avctx->height;
if (avctx->codec_id == CODEC_ID_WMV3)
{
int count = 0;
// looks like WMV3 has a sequence header stored in the extradata
// advanced sequence header may be before the first frame
// the last byte of the extradata is a version number, 1 for the
// samples we can decode
init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
if (vc1_decode_sequence_header(avctx, v, &gb) < 0)
return -1;
count = avctx->extradata_size*8 - get_bits_count(&gb);
if (count>0)
{
av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
count, get_bits(&gb, count));
}
else if (count < 0)
{
av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
}
} else { // VC1/WVC1
const uint8_t *start = avctx->extradata;
uint8_t *end = avctx->extradata + avctx->extradata_size;
const uint8_t *next;
int size, buf2_size;
uint8_t *buf2 = NULL;
int seq_initialized = 0, ep_initialized = 0;
if(avctx->extradata_size < 16) {
av_log(avctx, AV_LOG_ERROR, "Extradata size too small: %i\n", avctx->extradata_size);
return -1;
}
buf2 = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
start = find_next_marker(start, end); // in WVC1 extradata first byte is its size, but can be 0 in mkv
next = start;
for(; next < end; start = next){
next = find_next_marker(start + 4, end);
size = next - start - 4;
if(size <= 0) continue;
buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
init_get_bits(&gb, buf2, buf2_size * 8);
switch(AV_RB32(start)){
case VC1_CODE_SEQHDR:
if(vc1_decode_sequence_header(avctx, v, &gb) < 0){
av_free(buf2);
return -1;
}
seq_initialized = 1;
break;
case VC1_CODE_ENTRYPOINT:
if(vc1_decode_entry_point(avctx, v, &gb) < 0){
av_free(buf2);
return -1;
}
ep_initialized = 1;
break;
}
}
av_free(buf2);
if(!seq_initialized || !ep_initialized){
av_log(avctx, AV_LOG_ERROR, "Incomplete extradata\n");
return -1;
}
}
avctx->has_b_frames= !!(avctx->max_b_frames);
s->low_delay = !avctx->has_b_frames;
s->mb_width = (avctx->coded_width+15)>>4;
s->mb_height = (avctx->coded_height+15)>>4;
/* Allocate mb bitplanes */
v->mv_type_mb_plane = av_malloc(s->mb_stride * s->mb_height);
v->direct_mb_plane = av_malloc(s->mb_stride * s->mb_height);
v->acpred_plane = av_malloc(s->mb_stride * s->mb_height);
v->over_flags_plane = av_malloc(s->mb_stride * s->mb_height);
v->cbp_base = av_malloc(sizeof(v->cbp_base[0]) * 2 * s->mb_stride);
v->cbp = v->cbp_base + s->mb_stride;
/* allocate block type info in that way so it could be used with s->block_index[] */
v->mb_type_base = av_malloc(s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
v->mb_type[0] = v->mb_type_base + s->b8_stride + 1;
v->mb_type[1] = v->mb_type_base + s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride + 1;
v->mb_type[2] = v->mb_type[1] + s->mb_stride * (s->mb_height + 1);
/* Init coded blocks info */
if (v->profile == PROFILE_ADVANCED)
{
// if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
// return -1;
// if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
// return -1;
}
ff_intrax8_common_init(&v->x8,s);
return 0;
}
/** Decode a VC1/WMV3 frame
* @todo TODO: Handle VC-1 IDUs (Transport level?)
*/
static int vc1_decode_frame(AVCodecContext *avctx,
void *data, int *data_size,
AVPacket *avpkt)
{
const uint8_t *buf = avpkt->data;
int buf_size = avpkt->size;
VC1Context *v = avctx->priv_data;
MpegEncContext *s = &v->s;
AVFrame *pict = data;
uint8_t *buf2 = NULL;
const uint8_t *buf_start = buf;
/* no supplementary picture */
if (buf_size == 0) {
/* special case for last picture */
if (s->low_delay==0 && s->next_picture_ptr) {
*pict= *(AVFrame*)s->next_picture_ptr;
s->next_picture_ptr= NULL;
*data_size = sizeof(AVFrame);
}
return 0;
}
/* We need to set current_picture_ptr before reading the header,
* otherwise we cannot store anything in there. */
if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
int i= ff_find_unused_picture(s, 0);
s->current_picture_ptr= &s->picture[i];
}
if (s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU){
if (v->profile < PROFILE_ADVANCED)
avctx->pix_fmt = PIX_FMT_VDPAU_WMV3;
else
avctx->pix_fmt = PIX_FMT_VDPAU_VC1;
}
//for advanced profile we may need to parse and unescape data
if (avctx->codec_id == CODEC_ID_VC1) {
int buf_size2 = 0;
buf2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
if(IS_MARKER(AV_RB32(buf))){ /* frame starts with marker and needs to be parsed */
const uint8_t *start, *end, *next;
int size;
next = buf;
for(start = buf, end = buf + buf_size; next < end; start = next){
next = find_next_marker(start + 4, end);
size = next - start - 4;
if(size <= 0) continue;
switch(AV_RB32(start)){
case VC1_CODE_FRAME:
if (avctx->hwaccel ||
s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
buf_start = start;
buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
break;
case VC1_CODE_ENTRYPOINT: /* it should be before frame data */
buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
init_get_bits(&s->gb, buf2, buf_size2*8);
vc1_decode_entry_point(avctx, v, &s->gb);
break;
case VC1_CODE_SLICE:
av_log(avctx, AV_LOG_ERROR, "Sliced decoding is not implemented (yet)\n");
av_free(buf2);
return -1;
}
}
}else if(v->interlace && ((buf[0] & 0xC0) == 0xC0)){ /* WVC1 interlaced stores both fields divided by marker */
const uint8_t *divider;
divider = find_next_marker(buf, buf + buf_size);
if((divider == (buf + buf_size)) || AV_RB32(divider) != VC1_CODE_FIELD){
av_log(avctx, AV_LOG_ERROR, "Error in WVC1 interlaced frame\n");
av_free(buf2);
return -1;
}
buf_size2 = vc1_unescape_buffer(buf, divider - buf, buf2);
// TODO
if(!v->warn_interlaced++)
av_log(v->s.avctx, AV_LOG_ERROR, "Interlaced WVC1 support is not implemented\n");
av_free(buf2);return -1;
}else{
buf_size2 = vc1_unescape_buffer(buf, buf_size, buf2);
}
init_get_bits(&s->gb, buf2, buf_size2*8);
} else
init_get_bits(&s->gb, buf, buf_size*8);
// do parse frame header
if(v->profile < PROFILE_ADVANCED) {
if(vc1_parse_frame_header(v, &s->gb) == -1) {
av_free(buf2);
return -1;
}
} else {
if(vc1_parse_frame_header_adv(v, &s->gb) == -1) {
av_free(buf2);
return -1;
}
}
if(s->pict_type != FF_I_TYPE && !v->res_rtm_flag){
av_free(buf2);
return -1;
}
// for hurry_up==5
s->current_picture.pict_type= s->pict_type;
s->current_picture.key_frame= s->pict_type == FF_I_TYPE;
/* skip B-frames if we don't have reference frames */
if(s->last_picture_ptr==NULL && (s->pict_type==FF_B_TYPE || s->dropable)){
av_free(buf2);
return -1;//buf_size;
}
/* skip b frames if we are in a hurry */
if(avctx->hurry_up && s->pict_type==FF_B_TYPE) return -1;//buf_size;
if( (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==FF_B_TYPE)
|| (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=FF_I_TYPE)
|| avctx->skip_frame >= AVDISCARD_ALL) {
av_free(buf2);
return buf_size;
}
/* skip everything if we are in a hurry>=5 */
if(avctx->hurry_up>=5) {
av_free(buf2);
return -1;//buf_size;
}
if(s->next_p_frame_damaged){
if(s->pict_type==FF_B_TYPE)
return buf_size;
else
s->next_p_frame_damaged=0;
}
if(MPV_frame_start(s, avctx) < 0) {
av_free(buf2);
return -1;
}
s->me.qpel_put= s->dsp.put_qpel_pixels_tab;
s->me.qpel_avg= s->dsp.avg_qpel_pixels_tab;
if ((CONFIG_VC1_VDPAU_DECODER)
&&s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
ff_vdpau_vc1_decode_picture(s, buf_start, (buf + buf_size) - buf_start);
else if (avctx->hwaccel) {
if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
return -1;
if (avctx->hwaccel->decode_slice(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
return -1;
if (avctx->hwaccel->end_frame(avctx) < 0)
return -1;
} else {
ff_er_frame_start(s);
v->bits = buf_size * 8;
vc1_decode_blocks(v);
//av_log(s->avctx, AV_LOG_INFO, "Consumed %i/%i bits\n", get_bits_count(&s->gb), buf_size*8);
// if(get_bits_count(&s->gb) > buf_size * 8)
// return -1;
ff_er_frame_end(s);
}
MPV_frame_end(s);
assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
assert(s->current_picture.pict_type == s->pict_type);
if (s->pict_type == FF_B_TYPE || s->low_delay) {
*pict= *(AVFrame*)s->current_picture_ptr;
} else if (s->last_picture_ptr != NULL) {
*pict= *(AVFrame*)s->last_picture_ptr;
}
if(s->last_picture_ptr || s->low_delay){
*data_size = sizeof(AVFrame);
ff_print_debug_info(s, pict);
}
av_free(buf2);
return buf_size;
}
/** Close a VC1/WMV3 decoder
* @warning Initial try at using MpegEncContext stuff
*/
static av_cold int vc1_decode_end(AVCodecContext *avctx)
{
VC1Context *v = avctx->priv_data;
av_freep(&v->hrd_rate);
av_freep(&v->hrd_buffer);
MPV_common_end(&v->s);
av_freep(&v->mv_type_mb_plane);
av_freep(&v->direct_mb_plane);
av_freep(&v->acpred_plane);
av_freep(&v->over_flags_plane);
av_freep(&v->mb_type_base);
av_freep(&v->cbp_base);
ff_intrax8_common_end(&v->x8);
return 0;
}
AVCodec vc1_decoder = {
"vc1",
AVMEDIA_TYPE_VIDEO,
CODEC_ID_VC1,
sizeof(VC1Context),
vc1_decode_init,
NULL,
vc1_decode_end,
vc1_decode_frame,
CODEC_CAP_DR1 | CODEC_CAP_DELAY,
NULL,
.long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1"),
.pix_fmts = ff_hwaccel_pixfmt_list_420
};
#if CONFIG_WMV3_DECODER
AVCodec wmv3_decoder = {
"wmv3",
AVMEDIA_TYPE_VIDEO,
CODEC_ID_WMV3,
sizeof(VC1Context),
vc1_decode_init,
NULL,
vc1_decode_end,
vc1_decode_frame,
CODEC_CAP_DR1 | CODEC_CAP_DELAY,
NULL,
.long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9"),
.pix_fmts = ff_hwaccel_pixfmt_list_420
};
#endif
#if CONFIG_WMV3_VDPAU_DECODER
AVCodec wmv3_vdpau_decoder = {
"wmv3_vdpau",
AVMEDIA_TYPE_VIDEO,
CODEC_ID_WMV3,
sizeof(VC1Context),
vc1_decode_init,
NULL,
vc1_decode_end,
vc1_decode_frame,
CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
NULL,
.long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 VDPAU"),
.pix_fmts = (const enum PixelFormat[]){PIX_FMT_VDPAU_WMV3, PIX_FMT_NONE}
};
#endif
#if CONFIG_VC1_VDPAU_DECODER
AVCodec vc1_vdpau_decoder = {
"vc1_vdpau",
AVMEDIA_TYPE_VIDEO,
CODEC_ID_VC1,
sizeof(VC1Context),
vc1_decode_init,
NULL,
vc1_decode_end,
vc1_decode_frame,
CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
NULL,
.long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1 VDPAU"),
.pix_fmts = (const enum PixelFormat[]){PIX_FMT_VDPAU_VC1, PIX_FMT_NONE}
};
#endif
| 123linslouis-android-video-cutter | jni/libavcodec/vc1dec.c | C | asf20 | 124,799 |
/**
* LPC utility code
* Copyright (c) 2006 Justin Ruggles <justin.ruggles@gmail.com>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "libavutil/lls.h"
#include "dsputil.h"
#define LPC_USE_DOUBLE
#include "lpc.h"
/**
* Apply Welch window function to audio block
*/
static void apply_welch_window(const int32_t *data, int len, double *w_data)
{
int i, n2;
double w;
double c;
assert(!(len&1)); //the optimization in r11881 does not support odd len
//if someone wants odd len extend the change in r11881
n2 = (len >> 1);
c = 2.0 / (len - 1.0);
w_data+=n2;
data+=n2;
for(i=0; i<n2; i++) {
w = c - n2 + i;
w = 1.0 - (w * w);
w_data[-i-1] = data[-i-1] * w;
w_data[+i ] = data[+i ] * w;
}
}
/**
* Calculates autocorrelation data from audio samples
* A Welch window function is applied before calculation.
*/
void ff_lpc_compute_autocorr(const int32_t *data, int len, int lag,
double *autoc)
{
int i, j;
double tmp[len + lag + 1];
double *data1= tmp + lag;
apply_welch_window(data, len, data1);
for(j=0; j<lag; j++)
data1[j-lag]= 0.0;
data1[len] = 0.0;
for(j=0; j<lag; j+=2){
double sum0 = 1.0, sum1 = 1.0;
for(i=j; i<len; i++){
sum0 += data1[i] * data1[i-j];
sum1 += data1[i] * data1[i-j-1];
}
autoc[j ] = sum0;
autoc[j+1] = sum1;
}
if(j==lag){
double sum = 1.0;
for(i=j-1; i<len; i+=2){
sum += data1[i ] * data1[i-j ]
+ data1[i+1] * data1[i-j+1];
}
autoc[j] = sum;
}
}
/**
* Quantize LPC coefficients
*/
static void quantize_lpc_coefs(double *lpc_in, int order, int precision,
int32_t *lpc_out, int *shift, int max_shift, int zero_shift)
{
int i;
double cmax, error;
int32_t qmax;
int sh;
/* define maximum levels */
qmax = (1 << (precision - 1)) - 1;
/* find maximum coefficient value */
cmax = 0.0;
for(i=0; i<order; i++) {
cmax= FFMAX(cmax, fabs(lpc_in[i]));
}
/* if maximum value quantizes to zero, return all zeros */
if(cmax * (1 << max_shift) < 1.0) {
*shift = zero_shift;
memset(lpc_out, 0, sizeof(int32_t) * order);
return;
}
/* calculate level shift which scales max coeff to available bits */
sh = max_shift;
while((cmax * (1 << sh) > qmax) && (sh > 0)) {
sh--;
}
/* since negative shift values are unsupported in decoder, scale down
coefficients instead */
if(sh == 0 && cmax > qmax) {
double scale = ((double)qmax) / cmax;
for(i=0; i<order; i++) {
lpc_in[i] *= scale;
}
}
/* output quantized coefficients and level shift */
error=0;
for(i=0; i<order; i++) {
error -= lpc_in[i] * (1 << sh);
lpc_out[i] = av_clip(lrintf(error), -qmax, qmax);
error -= lpc_out[i];
}
*shift = sh;
}
static int estimate_best_order(double *ref, int min_order, int max_order)
{
int i, est;
est = min_order;
for(i=max_order-1; i>=min_order-1; i--) {
if(ref[i] > 0.10) {
est = i+1;
break;
}
}
return est;
}
/**
* Calculate LPC coefficients for multiple orders
*
* @param use_lpc LPC method for determining coefficients
* 0 = LPC with fixed pre-defined coeffs
* 1 = LPC with coeffs determined by Levinson-Durbin recursion
* 2+ = LPC with coeffs determined by Cholesky factorization using (use_lpc-1) passes.
*/
int ff_lpc_calc_coefs(DSPContext *s,
const int32_t *samples, int blocksize, int min_order,
int max_order, int precision,
int32_t coefs[][MAX_LPC_ORDER], int *shift, int use_lpc,
int omethod, int max_shift, int zero_shift)
{
double autoc[MAX_LPC_ORDER+1];
double ref[MAX_LPC_ORDER];
double lpc[MAX_LPC_ORDER][MAX_LPC_ORDER];
int i, j, pass;
int opt_order;
assert(max_order >= MIN_LPC_ORDER && max_order <= MAX_LPC_ORDER && use_lpc > 0);
if(use_lpc == 1){
s->lpc_compute_autocorr(samples, blocksize, max_order, autoc);
compute_lpc_coefs(autoc, max_order, &lpc[0][0], MAX_LPC_ORDER, 0, 1);
for(i=0; i<max_order; i++)
ref[i] = fabs(lpc[i][i]);
}else{
LLSModel m[2];
double var[MAX_LPC_ORDER+1], av_uninit(weight);
for(pass=0; pass<use_lpc-1; pass++){
av_init_lls(&m[pass&1], max_order);
weight=0;
for(i=max_order; i<blocksize; i++){
for(j=0; j<=max_order; j++)
var[j]= samples[i-j];
if(pass){
double eval, inv, rinv;
eval= av_evaluate_lls(&m[(pass-1)&1], var+1, max_order-1);
eval= (512>>pass) + fabs(eval - var[0]);
inv = 1/eval;
rinv = sqrt(inv);
for(j=0; j<=max_order; j++)
var[j] *= rinv;
weight += inv;
}else
weight++;
av_update_lls(&m[pass&1], var, 1.0);
}
av_solve_lls(&m[pass&1], 0.001, 0);
}
for(i=0; i<max_order; i++){
for(j=0; j<max_order; j++)
lpc[i][j]=-m[(pass-1)&1].coeff[i][j];
ref[i]= sqrt(m[(pass-1)&1].variance[i] / weight) * (blocksize - max_order) / 4000;
}
for(i=max_order-1; i>0; i--)
ref[i] = ref[i-1] - ref[i];
}
opt_order = max_order;
if(omethod == ORDER_METHOD_EST) {
opt_order = estimate_best_order(ref, min_order, max_order);
i = opt_order-1;
quantize_lpc_coefs(lpc[i], i+1, precision, coefs[i], &shift[i], max_shift, zero_shift);
} else {
for(i=min_order-1; i<max_order; i++) {
quantize_lpc_coefs(lpc[i], i+1, precision, coefs[i], &shift[i], max_shift, zero_shift);
}
}
return opt_order;
}
| 123linslouis-android-video-cutter | jni/libavcodec/lpc.c | C | asf20 | 6,853 |
/*
* Common code between the AC-3 encoder and decoder
* Copyright (c) 2000, 2001, 2002 Fabrice Bellard
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* Common code between the AC-3 encoder and decoder.
*/
#ifndef AVCODEC_AC3_H
#define AVCODEC_AC3_H
#include "ac3tab.h"
#define AC3_MAX_CODED_FRAME_SIZE 3840 /* in bytes */
#define AC3_MAX_CHANNELS 6 /* including LFE channel */
#define NB_BLOCKS 6 /* number of PCM blocks inside an AC-3 frame */
#define AC3_FRAME_SIZE (NB_BLOCKS * 256)
/* exponent encoding strategy */
#define EXP_REUSE 0
#define EXP_NEW 1
#define EXP_D15 1
#define EXP_D25 2
#define EXP_D45 3
/** Delta bit allocation strategy */
typedef enum {
DBA_REUSE = 0,
DBA_NEW,
DBA_NONE,
DBA_RESERVED
} AC3DeltaStrategy;
/** Channel mode (audio coding mode) */
typedef enum {
AC3_CHMODE_DUALMONO = 0,
AC3_CHMODE_MONO,
AC3_CHMODE_STEREO,
AC3_CHMODE_3F,
AC3_CHMODE_2F1R,
AC3_CHMODE_3F1R,
AC3_CHMODE_2F2R,
AC3_CHMODE_3F2R
} AC3ChannelMode;
typedef struct AC3BitAllocParameters {
int sr_code;
int sr_shift;
int slow_gain, slow_decay, fast_decay, db_per_bit, floor;
int cpl_fast_leak, cpl_slow_leak;
} AC3BitAllocParameters;
/**
* @struct AC3HeaderInfo
* Coded AC-3 header values up to the lfeon element, plus derived values.
*/
typedef struct {
/** @defgroup coded Coded elements
* @{
*/
uint16_t sync_word;
uint16_t crc1;
uint8_t sr_code;
uint8_t bitstream_id;
uint8_t channel_mode;
uint8_t lfe_on;
uint8_t frame_type;
int substreamid; ///< substream identification
int center_mix_level; ///< Center mix level index
int surround_mix_level; ///< Surround mix level index
uint16_t channel_map;
int num_blocks; ///< number of audio blocks
/** @} */
/** @defgroup derived Derived values
* @{
*/
uint8_t sr_shift;
uint16_t sample_rate;
uint32_t bit_rate;
uint8_t channels;
uint16_t frame_size;
int64_t channel_layout;
/** @} */
} AC3HeaderInfo;
typedef enum {
EAC3_FRAME_TYPE_INDEPENDENT = 0,
EAC3_FRAME_TYPE_DEPENDENT,
EAC3_FRAME_TYPE_AC3_CONVERT,
EAC3_FRAME_TYPE_RESERVED
} EAC3FrameType;
void ac3_common_init(void);
/**
* Calculates the log power-spectral density of the input signal.
* This gives a rough estimate of signal power in the frequency domain by using
* the spectral envelope (exponents). The psd is also separately grouped
* into critical bands for use in the calculating the masking curve.
* 128 units in psd = -6 dB. The dbknee parameter in AC3BitAllocParameters
* determines the reference level.
*
* @param[in] exp frequency coefficient exponents
* @param[in] start starting bin location
* @param[in] end ending bin location
* @param[out] psd signal power for each frequency bin
* @param[out] band_psd signal power for each critical band
*/
void ff_ac3_bit_alloc_calc_psd(int8_t *exp, int start, int end, int16_t *psd,
int16_t *band_psd);
/**
* Calculates the masking curve.
* First, the excitation is calculated using parameters in s and the signal
* power in each critical band. The excitation is compared with a predefined
* hearing threshold table to produce the masking curve. If delta bit
* allocation information is provided, it is used for adjusting the masking
* curve, usually to give a closer match to a better psychoacoustic model.
*
* @param[in] s adjustable bit allocation parameters
* @param[in] band_psd signal power for each critical band
* @param[in] start starting bin location
* @param[in] end ending bin location
* @param[in] fast_gain fast gain (estimated signal-to-mask ratio)
* @param[in] is_lfe whether or not the channel being processed is the LFE
* @param[in] dba_mode delta bit allocation mode (none, reuse, or new)
* @param[in] dba_nsegs number of delta segments
* @param[in] dba_offsets location offsets for each segment
* @param[in] dba_lengths length of each segment
* @param[in] dba_values delta bit allocation for each segment
* @param[out] mask calculated masking curve
* @return returns 0 for success, non-zero for error
*/
int ff_ac3_bit_alloc_calc_mask(AC3BitAllocParameters *s, int16_t *band_psd,
int start, int end, int fast_gain, int is_lfe,
int dba_mode, int dba_nsegs, uint8_t *dba_offsets,
uint8_t *dba_lengths, uint8_t *dba_values,
int16_t *mask);
/**
* Calculates bit allocation pointers.
* The SNR is the difference between the masking curve and the signal. AC-3
* uses this value for each frequency bin to allocate bits. The snroffset
* parameter is a global adjustment to the SNR for all bins.
*
* @param[in] mask masking curve
* @param[in] psd signal power for each frequency bin
* @param[in] start starting bin location
* @param[in] end ending bin location
* @param[in] snr_offset SNR adjustment
* @param[in] floor noise floor
* @param[in] bap_tab look-up table for bit allocation pointers
* @param[out] bap bit allocation pointers
*/
void ff_ac3_bit_alloc_calc_bap(int16_t *mask, int16_t *psd, int start, int end,
int snr_offset, int floor,
const uint8_t *bap_tab, uint8_t *bap);
void ac3_parametric_bit_allocation(AC3BitAllocParameters *s, uint8_t *bap,
int8_t *exp, int start, int end,
int snr_offset, int fast_gain, int is_lfe,
int dba_mode, int dba_nsegs,
uint8_t *dba_offsets, uint8_t *dba_lengths,
uint8_t *dba_values);
#endif /* AVCODEC_AC3_H */
| 123linslouis-android-video-cutter | jni/libavcodec/ac3.h | C | asf20 | 6,751 |
/*
* Block Gilbert-Moore decoder
* Copyright (c) 2010 Thilo Borgmann <thilo.borgmann _at_ googlemail.com>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* Block Gilbert-Moore decoder header
* @author Thilo Borgmann <thilo.borgmann _at_ googlemail.com>
*/
#ifndef AVCODEC_BGMC_H
#define AVCODEC_BGMC_H
#include "avcodec.h"
#include "get_bits.h"
int ff_bgmc_init(AVCodecContext *avctx, uint8_t **cf_lut, unsigned int **cf_lut_status);
void ff_bgmc_end(uint8_t **cf_lut, unsigned int **cf_lut_status);
void ff_bgmc_decode_init(GetBitContext *gb,
unsigned int *h, unsigned int *l, unsigned int *v);
void ff_bgmc_decode_end(GetBitContext *gb);
void ff_bgmc_decode(GetBitContext *gb, unsigned int num, int32_t *dst,
unsigned int delta, unsigned int sx,
unsigned int *h, unsigned int *l, unsigned int *v,
uint8_t *cf_lut, unsigned int *cf_lut_status);
#endif /* AVCODEC_BGMC_H */
| 123linslouis-android-video-cutter | jni/libavcodec/bgmc.h | C | asf20 | 1,697 |
/*
* Ratecontrol
* Copyright (c) 2000, 2001, 2002 Fabrice Bellard
* Copyright (c) 2002-2004 Michael Niedermayer
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef AVCODEC_RATECONTROL_H
#define AVCODEC_RATECONTROL_H
/**
* @file
* ratecontrol header.
*/
#include <stdio.h>
#include <stdint.h>
#include "eval.h"
typedef struct Predictor{
double coeff;
double count;
double decay;
} Predictor;
typedef struct RateControlEntry{
int pict_type;
float qscale;
int mv_bits;
int i_tex_bits;
int p_tex_bits;
int misc_bits;
int header_bits;
uint64_t expected_bits;
int new_pict_type;
float new_qscale;
int mc_mb_var_sum;
int mb_var_sum;
int i_count;
int skip_count;
int f_code;
int b_code;
}RateControlEntry;
/**
* rate control context.
*/
typedef struct RateControlContext{
FILE *stats_file;
int num_entries; ///< number of RateControlEntries
RateControlEntry *entry;
double buffer_index; ///< amount of bits in the video/audio buffer
Predictor pred[5];
double short_term_qsum; ///< sum of recent qscales
double short_term_qcount; ///< count of recent qscales
double pass1_rc_eq_output_sum;///< sum of the output of the rc equation, this is used for normalization
double pass1_wanted_bits; ///< bits which should have been outputed by the pass1 code (including complexity init)
double last_qscale;
double last_qscale_for[5]; ///< last qscale for a specific pict type, used for max_diff & ipb factor stuff
int last_mc_mb_var_sum;
int last_mb_var_sum;
uint64_t i_cplx_sum[5];
uint64_t p_cplx_sum[5];
uint64_t mv_bits_sum[5];
uint64_t qscale_sum[5];
int frame_count[5];
int last_non_b_pict_type;
void *non_lavc_opaque; ///< context for non lavc rc code (for example xvid)
float dry_run_qscale; ///< for xvid rc
int last_picture_number; ///< for xvid rc
AVExpr * rc_eq_eval;
}RateControlContext;
struct MpegEncContext;
/* rate control */
int ff_rate_control_init(struct MpegEncContext *s);
float ff_rate_estimate_qscale(struct MpegEncContext *s, int dry_run);
void ff_write_pass1_stats(struct MpegEncContext *s);
void ff_rate_control_uninit(struct MpegEncContext *s);
int ff_vbv_update(struct MpegEncContext *s, int frame_size);
void ff_get_2pass_fcode(struct MpegEncContext *s);
int ff_xvid_rate_control_init(struct MpegEncContext *s);
void ff_xvid_rate_control_uninit(struct MpegEncContext *s);
float ff_xvid_rate_estimate_qscale(struct MpegEncContext *s, int dry_run);
#endif /* AVCODEC_RATECONTROL_H */
| 123linslouis-android-video-cutter | jni/libavcodec/ratecontrol.h | C | asf20 | 3,359 |
/*
* DivX (XSUB) subtitle encoder
* Copyright (c) 2005 DivX, Inc.
* Copyright (c) 2009 Bjorn Axelsson
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "avcodec.h"
#include "bytestream.h"
#include "put_bits.h"
/**
* Number of pixels to pad left and right.
*
* The official encoder pads the subtitles with two pixels on either side,
* but until we find out why, we won't do it (we will pad to have width
* divisible by 2 though).
*/
#define PADDING 0
#define PADDING_COLOR 0
/**
* Encodes a single color run. At most 16 bits will be used.
* \param len length of the run, values > 255 mean "until end of line", may not be < 0.
* \param color color to encode, only the lowest two bits are used and all others must be 0.
*/
static void put_xsub_rle(PutBitContext *pb, int len, int color)
{
if (len <= 255)
put_bits(pb, 2 + ((ff_log2_tab[len] >> 1) << 2), len);
else
put_bits(pb, 14, 0);
put_bits(pb, 2, color);
}
/**
* Encodes a 4-color bitmap with XSUB rle.
*
* The encoded bitmap may be wider than the source bitmap due to padding.
*/
static int xsub_encode_rle(PutBitContext *pb, const uint8_t *bitmap,
int linesize, int w, int h)
{
int x0, x1, y, len, color = PADDING_COLOR;
for (y = 0; y < h; y++) {
x0 = 0;
while (x0 < w) {
// Make sure we have enough room for at least one run and padding
if (pb->size_in_bits - put_bits_count(pb) < 7*8)
return -1;
x1 = x0;
color = bitmap[x1++] & 3;
while (x1 < w && (bitmap[x1] & 3) == color)
x1++;
len = x1 - x0;
if (PADDING && x0 == 0) {
if (color == PADDING_COLOR) {
len += PADDING;
x0 -= PADDING;
} else
put_xsub_rle(pb, PADDING, PADDING_COLOR);
}
// Run can't be longer than 255, unless it is the rest of a row
if (x1 == w && color == PADDING_COLOR) {
len += PADDING + (w&1);
} else
len = FFMIN(len, 255);
put_xsub_rle(pb, len, color);
x0 += len;
}
if (color != PADDING_COLOR && (PADDING + (w&1)))
put_xsub_rle(pb, PADDING + (w&1), PADDING_COLOR);
align_put_bits(pb);
bitmap += linesize;
}
return 0;
}
static int make_tc(uint64_t ms, int *tc)
{
static const int tc_divs[3] = { 1000, 60, 60 };
int i;
for (i=0; i<3; i++) {
tc[i] = ms % tc_divs[i];
ms /= tc_divs[i];
}
tc[3] = ms;
return ms > 99;
}
static int xsub_encode(AVCodecContext *avctx, unsigned char *buf,
int bufsize, void *data)
{
AVSubtitle *h = data;
uint64_t startTime = h->pts / 1000; // FIXME: need better solution...
uint64_t endTime = startTime + h->end_display_time - h->start_display_time;
int start_tc[4], end_tc[4];
uint8_t *hdr = buf + 27; // Point behind the timestamp
uint8_t *rlelenptr;
uint16_t width, height;
int i;
PutBitContext pb;
if (bufsize < 27 + 7*2 + 4*3) {
av_log(avctx, AV_LOG_ERROR, "Buffer too small for XSUB header.\n");
return -1;
}
// TODO: support multiple rects
if (h->num_rects > 1)
av_log(avctx, AV_LOG_WARNING, "Only single rects supported (%d in subtitle.)\n", h->num_rects);
// TODO: render text-based subtitles into bitmaps
if (!h->rects[0]->pict.data[0] || !h->rects[0]->pict.data[1]) {
av_log(avctx, AV_LOG_WARNING, "No subtitle bitmap available.\n");
return -1;
}
// TODO: color reduction, similar to dvdsub encoder
if (h->rects[0]->nb_colors > 4)
av_log(avctx, AV_LOG_WARNING, "No more than 4 subtitle colors supported (%d found.)\n", h->rects[0]->nb_colors);
// TODO: Palette swapping if color zero is not transparent
if (((uint32_t *)h->rects[0]->pict.data[1])[0] & 0xff)
av_log(avctx, AV_LOG_WARNING, "Color index 0 is not transparent. Transparency will be messed up.\n");
if (make_tc(startTime, start_tc) || make_tc(endTime, end_tc)) {
av_log(avctx, AV_LOG_WARNING, "Time code >= 100 hours.\n");
return -1;
}
snprintf(buf, 28,
"[%02d:%02d:%02d.%03d-%02d:%02d:%02d.%03d]",
start_tc[3], start_tc[2], start_tc[1], start_tc[0],
end_tc[3], end_tc[2], end_tc[1], end_tc[0]);
// Width and height must probably be multiples of 2.
// 2 pixels required on either side of subtitle.
// Possibly due to limitations of hardware renderers.
// TODO: check if the bitmap is already padded
width = FFALIGN(h->rects[0]->w, 2) + PADDING * 2;
height = FFALIGN(h->rects[0]->h, 2);
bytestream_put_le16(&hdr, width);
bytestream_put_le16(&hdr, height);
bytestream_put_le16(&hdr, h->rects[0]->x);
bytestream_put_le16(&hdr, h->rects[0]->y);
bytestream_put_le16(&hdr, h->rects[0]->x + width);
bytestream_put_le16(&hdr, h->rects[0]->y + height);
rlelenptr = hdr; // Will store length of first field here later.
hdr+=2;
// Palette
for (i=0; i<4; i++)
bytestream_put_be24(&hdr, ((uint32_t *)h->rects[0]->pict.data[1])[i]);
// Bitmap
// RLE buffer. Reserve 2 bytes for possible padding after the last row.
init_put_bits(&pb, hdr, bufsize - (hdr - buf) - 2);
if (xsub_encode_rle(&pb, h->rects[0]->pict.data[0],
h->rects[0]->pict.linesize[0]*2,
h->rects[0]->w, (h->rects[0]->h + 1) >> 1))
return -1;
bytestream_put_le16(&rlelenptr, put_bits_count(&pb) >> 3); // Length of first field
if (xsub_encode_rle(&pb, h->rects[0]->pict.data[0] + h->rects[0]->pict.linesize[0],
h->rects[0]->pict.linesize[0]*2,
h->rects[0]->w, h->rects[0]->h >> 1))
return -1;
// Enforce total height to be be multiple of 2
if (h->rects[0]->h & 1) {
put_xsub_rle(&pb, h->rects[0]->w, PADDING_COLOR);
align_put_bits(&pb);
}
flush_put_bits(&pb);
return hdr - buf + put_bits_count(&pb)/8;
}
static av_cold int xsub_encoder_init(AVCodecContext *avctx)
{
if (!avctx->codec_tag)
avctx->codec_tag = MKTAG('D','X','S','B');
return 0;
}
AVCodec xsub_encoder = {
"xsub",
AVMEDIA_TYPE_SUBTITLE,
CODEC_ID_XSUB,
0,
xsub_encoder_init,
xsub_encode,
NULL,
.long_name = NULL_IF_CONFIG_SMALL("DivX subtitles (XSUB)"),
};
| 123linslouis-android-video-cutter | jni/libavcodec/xsubenc.c | C | asf20 | 7,259 |
/*
* DCA compatible decoder
* Copyright (C) 2004 Gildas Bazin
* Copyright (C) 2004 Benjamin Zores
* Copyright (C) 2006 Benjamin Larsson
* Copyright (C) 2007 Konstantin Shishkov
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef AVCODEC_DCA_H
#define AVCODEC_DCA_H
/** DCA syncwords, also used for bitstream type detection */
#define DCA_MARKER_RAW_BE 0x7FFE8001
#define DCA_MARKER_RAW_LE 0xFE7F0180
#define DCA_MARKER_14B_BE 0x1FFFE800
#define DCA_MARKER_14B_LE 0xFF1F00E8
/** DCA-HD specific block starts with this marker. */
#define DCA_HD_MARKER 0x64582025
#endif /* AVCODEC_DCA_H */
| 123linslouis-android-video-cutter | jni/libavcodec/dca.h | C | asf20 | 1,320 |
/*
* MJPEG parser
* Copyright (c) 2000, 2001 Fabrice Bellard
* Copyright (c) 2003 Alex Beregszaszi
* Copyright (c) 2003-2004 Michael Niedermayer
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* MJPEG parser.
*/
#include "parser.h"
/**
* finds the end of the current frame in the bitstream.
* @return the position of the first byte of the next frame, or -1
*/
static int find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size){
int vop_found, i;
uint16_t state;
vop_found= pc->frame_start_found;
state= pc->state;
i=0;
if(!vop_found){
for(i=0; i<buf_size; i++){
state= (state<<8) | buf[i];
if(state == 0xFFD8){
i++;
vop_found=1;
break;
}
}
}
if(vop_found){
/* EOF considered as end of frame */
if (buf_size == 0)
return 0;
for(; i<buf_size; i++){
state= (state<<8) | buf[i];
if(state == 0xFFD8){
pc->frame_start_found=0;
pc->state=0;
return i-1;
}
}
}
pc->frame_start_found= vop_found;
pc->state= state;
return END_NOT_FOUND;
}
static int jpeg_parse(AVCodecParserContext *s,
AVCodecContext *avctx,
const uint8_t **poutbuf, int *poutbuf_size,
const uint8_t *buf, int buf_size)
{
ParseContext *pc = s->priv_data;
int next;
if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
next= buf_size;
}else{
next= find_frame_end(pc, buf, buf_size);
if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
*poutbuf = NULL;
*poutbuf_size = 0;
return buf_size;
}
}
*poutbuf = buf;
*poutbuf_size = buf_size;
return next;
}
AVCodecParser mjpeg_parser = {
{ CODEC_ID_MJPEG },
sizeof(ParseContext),
NULL,
jpeg_parse,
ff_parse_close,
};
| 123linslouis-android-video-cutter | jni/libavcodec/mjpeg_parser.c | C | asf20 | 2,731 |
/*
* AVPacket functions for libavcodec
* Copyright (c) 2000, 2001, 2002 Fabrice Bellard
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "avcodec.h"
void av_destruct_packet_nofree(AVPacket *pkt)
{
pkt->data = NULL; pkt->size = 0;
}
void av_destruct_packet(AVPacket *pkt)
{
av_free(pkt->data);
pkt->data = NULL; pkt->size = 0;
}
void av_init_packet(AVPacket *pkt)
{
pkt->pts = AV_NOPTS_VALUE;
pkt->dts = AV_NOPTS_VALUE;
pkt->pos = -1;
pkt->duration = 0;
pkt->convergence_duration = 0;
pkt->flags = 0;
pkt->stream_index = 0;
pkt->destruct= NULL;
}
int av_new_packet(AVPacket *pkt, int size)
{
uint8_t *data= NULL;
if((unsigned)size < (unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE)
data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
if (data){
memset(data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
}else
size=0;
av_init_packet(pkt);
pkt->data = data;
pkt->size = size;
pkt->destruct = av_destruct_packet;
if(!data)
return AVERROR(ENOMEM);
return 0;
}
void av_shrink_packet(AVPacket *pkt, int size)
{
if (pkt->size <= size) return;
pkt->size = size;
memset(pkt->data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
}
int av_dup_packet(AVPacket *pkt)
{
if (((pkt->destruct == av_destruct_packet_nofree) || (pkt->destruct == NULL)) && pkt->data) {
uint8_t *data;
/* We duplicate the packet and don't forget to add the padding again. */
if((unsigned)pkt->size > (unsigned)pkt->size + FF_INPUT_BUFFER_PADDING_SIZE)
return AVERROR(ENOMEM);
data = av_malloc(pkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
if (!data) {
return AVERROR(ENOMEM);
}
memcpy(data, pkt->data, pkt->size);
memset(data + pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
pkt->data = data;
pkt->destruct = av_destruct_packet;
}
return 0;
}
void av_free_packet(AVPacket *pkt)
{
if (pkt) {
if (pkt->destruct) pkt->destruct(pkt);
pkt->data = NULL; pkt->size = 0;
}
}
| 123linslouis-android-video-cutter | jni/libavcodec/avpacket.c | C | asf20 | 2,824 |
/*
* PNM image format
* Copyright (c) 2002, 2003 Fabrice Bellard
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "avcodec.h"
#include "pnm.h"
static inline int pnm_space(int c)
{
return c == ' ' || c == '\n' || c == '\r' || c == '\t';
}
static void pnm_get(PNMContext *sc, char *str, int buf_size)
{
char *s;
int c;
/* skip spaces and comments */
for (;;) {
c = *sc->bytestream++;
if (c == '#') {
do {
c = *sc->bytestream++;
} while (c != '\n' && sc->bytestream < sc->bytestream_end);
} else if (!pnm_space(c)) {
break;
}
}
s = str;
while (sc->bytestream < sc->bytestream_end && !pnm_space(c)) {
if ((s - str) < buf_size - 1)
*s++ = c;
c = *sc->bytestream++;
}
*s = '\0';
}
int ff_pnm_decode_header(AVCodecContext *avctx, PNMContext * const s)
{
char buf1[32], tuple_type[32];
int h, w, depth, maxval;
pnm_get(s, buf1, sizeof(buf1));
s->type= buf1[1]-'0';
if(buf1[0] != 'P')
return -1;
if (s->type==1 || s->type==4) {
avctx->pix_fmt = PIX_FMT_MONOWHITE;
} else if (s->type==2 || s->type==5) {
if (avctx->codec_id == CODEC_ID_PGMYUV)
avctx->pix_fmt = PIX_FMT_YUV420P;
else
avctx->pix_fmt = PIX_FMT_GRAY8;
} else if (s->type==3 || s->type==6) {
avctx->pix_fmt = PIX_FMT_RGB24;
} else if (s->type==7) {
w = -1;
h = -1;
maxval = -1;
depth = -1;
tuple_type[0] = '\0';
for (;;) {
pnm_get(s, buf1, sizeof(buf1));
if (!strcmp(buf1, "WIDTH")) {
pnm_get(s, buf1, sizeof(buf1));
w = strtol(buf1, NULL, 10);
} else if (!strcmp(buf1, "HEIGHT")) {
pnm_get(s, buf1, sizeof(buf1));
h = strtol(buf1, NULL, 10);
} else if (!strcmp(buf1, "DEPTH")) {
pnm_get(s, buf1, sizeof(buf1));
depth = strtol(buf1, NULL, 10);
} else if (!strcmp(buf1, "MAXVAL")) {
pnm_get(s, buf1, sizeof(buf1));
maxval = strtol(buf1, NULL, 10);
} else if (!strcmp(buf1, "TUPLETYPE")) {
pnm_get(s, tuple_type, sizeof(tuple_type));
} else if (!strcmp(buf1, "ENDHDR")) {
break;
} else {
return -1;
}
}
/* check that all tags are present */
if (w <= 0 || h <= 0 || maxval <= 0 || depth <= 0 || tuple_type[0] == '\0' || avcodec_check_dimensions(avctx, w, h))
return -1;
avctx->width = w;
avctx->height = h;
if (depth == 1) {
if (maxval == 1)
avctx->pix_fmt = PIX_FMT_MONOWHITE;
else
avctx->pix_fmt = PIX_FMT_GRAY8;
} else if (depth == 3) {
if (maxval < 256) {
avctx->pix_fmt = PIX_FMT_RGB24;
} else {
av_log(avctx, AV_LOG_ERROR, "16-bit components are only supported for grayscale\n");
avctx->pix_fmt = PIX_FMT_NONE;
return -1;
}
} else if (depth == 4) {
avctx->pix_fmt = PIX_FMT_RGB32;
} else {
return -1;
}
return 0;
} else {
return -1;
}
pnm_get(s, buf1, sizeof(buf1));
avctx->width = atoi(buf1);
if (avctx->width <= 0)
return -1;
pnm_get(s, buf1, sizeof(buf1));
avctx->height = atoi(buf1);
if(avcodec_check_dimensions(avctx, avctx->width, avctx->height))
return -1;
if (avctx->pix_fmt != PIX_FMT_MONOWHITE) {
pnm_get(s, buf1, sizeof(buf1));
s->maxval = atoi(buf1);
if (s->maxval >= 256) {
if (avctx->pix_fmt == PIX_FMT_GRAY8) {
avctx->pix_fmt = PIX_FMT_GRAY16BE;
if (s->maxval != 65535)
avctx->pix_fmt = PIX_FMT_GRAY16;
} else if (avctx->pix_fmt == PIX_FMT_RGB24) {
if (s->maxval > 255)
avctx->pix_fmt = PIX_FMT_RGB48BE;
} else {
av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format\n");
avctx->pix_fmt = PIX_FMT_NONE;
return -1;
}
}
}else
s->maxval=1;
/* more check if YUV420 */
if (avctx->pix_fmt == PIX_FMT_YUV420P) {
if ((avctx->width & 1) != 0)
return -1;
h = (avctx->height * 2);
if ((h % 3) != 0)
return -1;
h /= 3;
avctx->height = h;
}
return 0;
}
av_cold int ff_pnm_end(AVCodecContext *avctx)
{
PNMContext *s = avctx->priv_data;
if (s->picture.data[0])
avctx->release_buffer(avctx, &s->picture);
return 0;
}
av_cold int ff_pnm_init(AVCodecContext *avctx)
{
PNMContext *s = avctx->priv_data;
avcodec_get_frame_defaults((AVFrame*)&s->picture);
avctx->coded_frame = (AVFrame*)&s->picture;
return 0;
}
| 123linslouis-android-video-cutter | jni/libavcodec/pnm.c | C | asf20 | 5,779 |
/*
* Interplay MVE Video Decoder
* Copyright (C) 2003 the ffmpeg project
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* Interplay MVE Video Decoder by Mike Melanson (melanson@pcisys.net)
* For more information about the Interplay MVE format, visit:
* http://www.pcisys.net/~melanson/codecs/interplay-mve.txt
* This code is written in such a way that the identifiers match up
* with the encoding descriptions in the document.
*
* This decoder presently only supports a PAL8 output colorspace.
*
* An Interplay video frame consists of 2 parts: The decoding map and
* the video data. A demuxer must load these 2 parts together in a single
* buffer before sending it through the stream to this decoder.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "avcodec.h"
#include "bytestream.h"
#include "dsputil.h"
#define ALT_BITSTREAM_READER_LE
#include "get_bits.h"
#define PALETTE_COUNT 256
/* debugging support */
#define DEBUG_INTERPLAY 0
#if DEBUG_INTERPLAY
#define debug_interplay(x,...) av_log(NULL, AV_LOG_DEBUG, x, __VA_ARGS__)
#else
static inline void debug_interplay(const char *format, ...) { }
#endif
typedef struct IpvideoContext {
AVCodecContext *avctx;
DSPContext dsp;
AVFrame second_last_frame;
AVFrame last_frame;
AVFrame current_frame;
const unsigned char *decoding_map;
int decoding_map_size;
const unsigned char *buf;
int size;
int is_16bpp;
const unsigned char *stream_ptr;
const unsigned char *stream_end;
const uint8_t *mv_ptr;
const uint8_t *mv_end;
unsigned char *pixel_ptr;
int line_inc;
int stride;
int upper_motion_limit_offset;
} IpvideoContext;
#define CHECK_STREAM_PTR(stream_ptr, stream_end, n) \
if (stream_end - stream_ptr < n) { \
av_log(s->avctx, AV_LOG_ERROR, "Interplay video warning: stream_ptr out of bounds (%p >= %p)\n", \
stream_ptr + n, stream_end); \
return -1; \
}
static int copy_from(IpvideoContext *s, AVFrame *src, int delta_x, int delta_y)
{
int current_offset = s->pixel_ptr - s->current_frame.data[0];
int motion_offset = current_offset + delta_y * s->current_frame.linesize[0]
+ delta_x * (1 + s->is_16bpp);
if (motion_offset < 0) {
av_log(s->avctx, AV_LOG_ERROR, " Interplay video: motion offset < 0 (%d)\n", motion_offset);
return -1;
} else if (motion_offset > s->upper_motion_limit_offset) {
av_log(s->avctx, AV_LOG_ERROR, " Interplay video: motion offset above limit (%d >= %d)\n",
motion_offset, s->upper_motion_limit_offset);
return -1;
}
s->dsp.put_pixels_tab[!s->is_16bpp][0](s->pixel_ptr, src->data[0] + motion_offset,
s->current_frame.linesize[0], 8);
return 0;
}
static int ipvideo_decode_block_opcode_0x0(IpvideoContext *s)
{
return copy_from(s, &s->last_frame, 0, 0);
}
static int ipvideo_decode_block_opcode_0x1(IpvideoContext *s)
{
return copy_from(s, &s->second_last_frame, 0, 0);
}
static int ipvideo_decode_block_opcode_0x2(IpvideoContext *s)
{
unsigned char B;
int x, y;
/* copy block from 2 frames ago using a motion vector; need 1 more byte */
if (!s->is_16bpp) {
CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 1);
B = *s->stream_ptr++;
} else {
CHECK_STREAM_PTR(s->mv_ptr, s->mv_end, 1);
B = *s->mv_ptr++;
}
if (B < 56) {
x = 8 + (B % 7);
y = B / 7;
} else {
x = -14 + ((B - 56) % 29);
y = 8 + ((B - 56) / 29);
}
debug_interplay (" motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
return copy_from(s, &s->second_last_frame, x, y);
}
static int ipvideo_decode_block_opcode_0x3(IpvideoContext *s)
{
unsigned char B;
int x, y;
/* copy 8x8 block from current frame from an up/left block */
/* need 1 more byte for motion */
if (!s->is_16bpp) {
CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 1);
B = *s->stream_ptr++;
} else {
CHECK_STREAM_PTR(s->mv_ptr, s->mv_end, 1);
B = *s->mv_ptr++;
}
if (B < 56) {
x = -(8 + (B % 7));
y = -(B / 7);
} else {
x = -(-14 + ((B - 56) % 29));
y = -( 8 + ((B - 56) / 29));
}
debug_interplay (" motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
return copy_from(s, &s->current_frame, x, y);
}
static int ipvideo_decode_block_opcode_0x4(IpvideoContext *s)
{
int x, y;
unsigned char B, BL, BH;
/* copy a block from the previous frame; need 1 more byte */
if (!s->is_16bpp) {
CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 1);
B = *s->stream_ptr++;
} else {
CHECK_STREAM_PTR(s->mv_ptr, s->mv_end, 1);
B = *s->mv_ptr++;
}
BL = B & 0x0F;
BH = (B >> 4) & 0x0F;
x = -8 + BL;
y = -8 + BH;
debug_interplay (" motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
return copy_from(s, &s->last_frame, x, y);
}
static int ipvideo_decode_block_opcode_0x5(IpvideoContext *s)
{
signed char x, y;
/* copy a block from the previous frame using an expanded range;
* need 2 more bytes */
CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 2);
x = *s->stream_ptr++;
y = *s->stream_ptr++;
debug_interplay (" motion bytes = %d, %d\n", x, y);
return copy_from(s, &s->last_frame, x, y);
}
static int ipvideo_decode_block_opcode_0x6(IpvideoContext *s)
{
/* mystery opcode? skip multiple blocks? */
av_log(s->avctx, AV_LOG_ERROR, " Interplay video: Help! Mystery opcode 0x6 seen\n");
/* report success */
return 0;
}
static int ipvideo_decode_block_opcode_0x7(IpvideoContext *s)
{
int x, y;
unsigned char P[2];
unsigned int flags;
/* 2-color encoding */
CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 2);
P[0] = *s->stream_ptr++;
P[1] = *s->stream_ptr++;
if (P[0] <= P[1]) {
/* need 8 more bytes from the stream */
CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 8);
for (y = 0; y < 8; y++) {
flags = *s->stream_ptr++ | 0x100;
for (; flags != 1; flags >>= 1)
*s->pixel_ptr++ = P[flags & 1];
s->pixel_ptr += s->line_inc;
}
} else {
/* need 2 more bytes from the stream */
CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 2);
flags = bytestream_get_le16(&s->stream_ptr);
for (y = 0; y < 8; y += 2) {
for (x = 0; x < 8; x += 2, flags >>= 1) {
s->pixel_ptr[x ] =
s->pixel_ptr[x + 1 ] =
s->pixel_ptr[x + s->stride] =
s->pixel_ptr[x + 1 + s->stride] = P[flags & 1];
}
s->pixel_ptr += s->stride * 2;
}
}
/* report success */
return 0;
}
static int ipvideo_decode_block_opcode_0x8(IpvideoContext *s)
{
int x, y;
unsigned char P[2];
unsigned int flags = 0;
/* 2-color encoding for each 4x4 quadrant, or 2-color encoding on
* either top and bottom or left and right halves */
CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 2);
P[0] = *s->stream_ptr++;
P[1] = *s->stream_ptr++;
if (P[0] <= P[1]) {
CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 14);
s->stream_ptr -= 2;
for (y = 0; y < 16; y++) {
// new values for each 4x4 block
if (!(y & 3)) {
P[0] = *s->stream_ptr++; P[1] = *s->stream_ptr++;
flags = bytestream_get_le16(&s->stream_ptr);
}
for (x = 0; x < 4; x++, flags >>= 1)
*s->pixel_ptr++ = P[flags & 1];
s->pixel_ptr += s->stride - 4;
// switch to right half
if (y == 7) s->pixel_ptr -= 8 * s->stride - 4;
}
} else {
/* need 10 more bytes */
CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 10);
if (s->stream_ptr[4] <= s->stream_ptr[5]) {
flags = bytestream_get_le32(&s->stream_ptr);
/* vertical split; left & right halves are 2-color encoded */
for (y = 0; y < 16; y++) {
for (x = 0; x < 4; x++, flags >>= 1)
*s->pixel_ptr++ = P[flags & 1];
s->pixel_ptr += s->stride - 4;
// switch to right half
if (y == 7) {
s->pixel_ptr -= 8 * s->stride - 4;
P[0] = *s->stream_ptr++; P[1] = *s->stream_ptr++;
flags = bytestream_get_le32(&s->stream_ptr);
}
}
} else {
/* horizontal split; top & bottom halves are 2-color encoded */
for (y = 0; y < 8; y++) {
if (y == 4) {
P[0] = *s->stream_ptr++;
P[1] = *s->stream_ptr++;
}
flags = *s->stream_ptr++ | 0x100;
for (; flags != 1; flags >>= 1)
*s->pixel_ptr++ = P[flags & 1];
s->pixel_ptr += s->line_inc;
}
}
}
/* report success */
return 0;
}
static int ipvideo_decode_block_opcode_0x9(IpvideoContext *s)
{
int x, y;
unsigned char P[4];
/* 4-color encoding */
CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 4);
memcpy(P, s->stream_ptr, 4);
s->stream_ptr += 4;
if (P[0] <= P[1]) {
if (P[2] <= P[3]) {
/* 1 of 4 colors for each pixel, need 16 more bytes */
CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 16);
for (y = 0; y < 8; y++) {
/* get the next set of 8 2-bit flags */
int flags = bytestream_get_le16(&s->stream_ptr);
for (x = 0; x < 8; x++, flags >>= 2)
*s->pixel_ptr++ = P[flags & 0x03];
s->pixel_ptr += s->line_inc;
}
} else {
uint32_t flags;
/* 1 of 4 colors for each 2x2 block, need 4 more bytes */
CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 4);
flags = bytestream_get_le32(&s->stream_ptr);
for (y = 0; y < 8; y += 2) {
for (x = 0; x < 8; x += 2, flags >>= 2) {
s->pixel_ptr[x ] =
s->pixel_ptr[x + 1 ] =
s->pixel_ptr[x + s->stride] =
s->pixel_ptr[x + 1 + s->stride] = P[flags & 0x03];
}
s->pixel_ptr += s->stride * 2;
}
}
} else {
uint64_t flags;
/* 1 of 4 colors for each 2x1 or 1x2 block, need 8 more bytes */
CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 8);
flags = bytestream_get_le64(&s->stream_ptr);
if (P[2] <= P[3]) {
for (y = 0; y < 8; y++) {
for (x = 0; x < 8; x += 2, flags >>= 2) {
s->pixel_ptr[x ] =
s->pixel_ptr[x + 1] = P[flags & 0x03];
}
s->pixel_ptr += s->stride;
}
} else {
for (y = 0; y < 8; y += 2) {
for (x = 0; x < 8; x++, flags >>= 2) {
s->pixel_ptr[x ] =
s->pixel_ptr[x + s->stride] = P[flags & 0x03];
}
s->pixel_ptr += s->stride * 2;
}
}
}
/* report success */
return 0;
}
static int ipvideo_decode_block_opcode_0xA(IpvideoContext *s)
{
int x, y;
unsigned char P[4];
int flags = 0;
/* 4-color encoding for each 4x4 quadrant, or 4-color encoding on
* either top and bottom or left and right halves */
CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 24);
if (s->stream_ptr[0] <= s->stream_ptr[1]) {
/* 4-color encoding for each quadrant; need 32 bytes */
CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 32);
for (y = 0; y < 16; y++) {
// new values for each 4x4 block
if (!(y & 3)) {
memcpy(P, s->stream_ptr, 4);
s->stream_ptr += 4;
flags = bytestream_get_le32(&s->stream_ptr);
}
for (x = 0; x < 4; x++, flags >>= 2)
*s->pixel_ptr++ = P[flags & 0x03];
s->pixel_ptr += s->stride - 4;
// switch to right half
if (y == 7) s->pixel_ptr -= 8 * s->stride - 4;
}
} else {
// vertical split?
int vert = s->stream_ptr[12] <= s->stream_ptr[13];
uint64_t flags = 0;
/* 4-color encoding for either left and right or top and bottom
* halves */
for (y = 0; y < 16; y++) {
// load values for each half
if (!(y & 7)) {
memcpy(P, s->stream_ptr, 4);
s->stream_ptr += 4;
flags = bytestream_get_le64(&s->stream_ptr);
}
for (x = 0; x < 4; x++, flags >>= 2)
*s->pixel_ptr++ = P[flags & 0x03];
if (vert) {
s->pixel_ptr += s->stride - 4;
// switch to right half
if (y == 7) s->pixel_ptr -= 8 * s->stride - 4;
} else if (y & 1) s->pixel_ptr += s->line_inc;
}
}
/* report success */
return 0;
}
static int ipvideo_decode_block_opcode_0xB(IpvideoContext *s)
{
int y;
/* 64-color encoding (each pixel in block is a different color) */
CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 64);
for (y = 0; y < 8; y++) {
memcpy(s->pixel_ptr, s->stream_ptr, 8);
s->stream_ptr += 8;
s->pixel_ptr += s->stride;
}
/* report success */
return 0;
}
static int ipvideo_decode_block_opcode_0xC(IpvideoContext *s)
{
int x, y;
/* 16-color block encoding: each 2x2 block is a different color */
CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 16);
for (y = 0; y < 8; y += 2) {
for (x = 0; x < 8; x += 2) {
s->pixel_ptr[x ] =
s->pixel_ptr[x + 1 ] =
s->pixel_ptr[x + s->stride] =
s->pixel_ptr[x + 1 + s->stride] = *s->stream_ptr++;
}
s->pixel_ptr += s->stride * 2;
}
/* report success */
return 0;
}
static int ipvideo_decode_block_opcode_0xD(IpvideoContext *s)
{
int y;
unsigned char P[2];
/* 4-color block encoding: each 4x4 block is a different color */
CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 4);
for (y = 0; y < 8; y++) {
if (!(y & 3)) {
P[0] = *s->stream_ptr++;
P[1] = *s->stream_ptr++;
}
memset(s->pixel_ptr, P[0], 4);
memset(s->pixel_ptr + 4, P[1], 4);
s->pixel_ptr += s->stride;
}
/* report success */
return 0;
}
static int ipvideo_decode_block_opcode_0xE(IpvideoContext *s)
{
int y;
unsigned char pix;
/* 1-color encoding: the whole block is 1 solid color */
CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 1);
pix = *s->stream_ptr++;
for (y = 0; y < 8; y++) {
memset(s->pixel_ptr, pix, 8);
s->pixel_ptr += s->stride;
}
/* report success */
return 0;
}
static int ipvideo_decode_block_opcode_0xF(IpvideoContext *s)
{
int x, y;
unsigned char sample[2];
/* dithered encoding */
CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 2);
sample[0] = *s->stream_ptr++;
sample[1] = *s->stream_ptr++;
for (y = 0; y < 8; y++) {
for (x = 0; x < 8; x += 2) {
*s->pixel_ptr++ = sample[ y & 1 ];
*s->pixel_ptr++ = sample[!(y & 1)];
}
s->pixel_ptr += s->line_inc;
}
/* report success */
return 0;
}
static int ipvideo_decode_block_opcode_0x6_16(IpvideoContext *s)
{
signed char x, y;
/* copy a block from the second last frame using an expanded range */
CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 2);
x = *s->stream_ptr++;
y = *s->stream_ptr++;
debug_interplay (" motion bytes = %d, %d\n", x, y);
return copy_from(s, &s->second_last_frame, x, y);
}
static int ipvideo_decode_block_opcode_0x7_16(IpvideoContext *s)
{
int x, y;
uint16_t P[2];
unsigned int flags;
uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
/* 2-color encoding */
CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 4);
P[0] = bytestream_get_le16(&s->stream_ptr);
P[1] = bytestream_get_le16(&s->stream_ptr);
if (!(P[0] & 0x8000)) {
CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 8);
for (y = 0; y < 8; y++) {
flags = *s->stream_ptr++ | 0x100;
for (; flags != 1; flags >>= 1)
*pixel_ptr++ = P[flags & 1];
pixel_ptr += s->line_inc;
}
} else {
CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 2);
flags = bytestream_get_le16(&s->stream_ptr);
for (y = 0; y < 8; y += 2) {
for (x = 0; x < 8; x += 2, flags >>= 1) {
pixel_ptr[x ] =
pixel_ptr[x + 1 ] =
pixel_ptr[x + s->stride] =
pixel_ptr[x + 1 + s->stride] = P[flags & 1];
}
pixel_ptr += s->stride * 2;
}
}
return 0;
}
static int ipvideo_decode_block_opcode_0x8_16(IpvideoContext *s)
{
int x, y;
uint16_t P[2];
unsigned int flags = 0;
uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
/* 2-color encoding for each 4x4 quadrant, or 2-color encoding on
* either top and bottom or left and right halves */
CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 4);
P[0] = bytestream_get_le16(&s->stream_ptr);
P[1] = bytestream_get_le16(&s->stream_ptr);
if (!(P[0] & 0x8000)) {
CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 24);
s->stream_ptr -= 4;
for (y = 0; y < 16; y++) {
// new values for each 4x4 block
if (!(y & 3)) {
P[0] = bytestream_get_le16(&s->stream_ptr);
P[1] = bytestream_get_le16(&s->stream_ptr);
flags = bytestream_get_le16(&s->stream_ptr);
}
for (x = 0; x < 4; x++, flags >>= 1)
*pixel_ptr++ = P[flags & 1];
pixel_ptr += s->stride - 4;
// switch to right half
if (y == 7) pixel_ptr -= 8 * s->stride - 4;
}
} else {
CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 12);
if (!(AV_RL16(s->stream_ptr + 4) & 0x8000)) {
flags = bytestream_get_le32(&s->stream_ptr);
/* vertical split; left & right halves are 2-color encoded */
for (y = 0; y < 16; y++) {
for (x = 0; x < 4; x++, flags >>= 1)
*pixel_ptr++ = P[flags & 1];
pixel_ptr += s->stride - 4;
// switch to right half
if (y == 7) {
pixel_ptr -= 8 * s->stride - 4;
P[0] = bytestream_get_le16(&s->stream_ptr);
P[1] = bytestream_get_le16(&s->stream_ptr);
flags = bytestream_get_le32(&s->stream_ptr);
}
}
} else {
/* horizontal split; top & bottom halves are 2-color encoded */
for (y = 0; y < 8; y++) {
if (y == 4) {
P[0] = bytestream_get_le16(&s->stream_ptr);
P[1] = bytestream_get_le16(&s->stream_ptr);
}
flags = *s->stream_ptr++ | 0x100;
for (; flags != 1; flags >>= 1)
*pixel_ptr++ = P[flags & 1];
pixel_ptr += s->line_inc;
}
}
}
/* report success */
return 0;
}
static int ipvideo_decode_block_opcode_0x9_16(IpvideoContext *s)
{
int x, y;
uint16_t P[4];
uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
/* 4-color encoding */
CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 8);
for (x = 0; x < 4; x++)
P[x] = bytestream_get_le16(&s->stream_ptr);
if (!(P[0] & 0x8000)) {
if (!(P[2] & 0x8000)) {
/* 1 of 4 colors for each pixel */
CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 16);
for (y = 0; y < 8; y++) {
/* get the next set of 8 2-bit flags */
int flags = bytestream_get_le16(&s->stream_ptr);
for (x = 0; x < 8; x++, flags >>= 2)
*pixel_ptr++ = P[flags & 0x03];
pixel_ptr += s->line_inc;
}
} else {
uint32_t flags;
/* 1 of 4 colors for each 2x2 block */
CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 4);
flags = bytestream_get_le32(&s->stream_ptr);
for (y = 0; y < 8; y += 2) {
for (x = 0; x < 8; x += 2, flags >>= 2) {
pixel_ptr[x ] =
pixel_ptr[x + 1 ] =
pixel_ptr[x + s->stride] =
pixel_ptr[x + 1 + s->stride] = P[flags & 0x03];
}
pixel_ptr += s->stride * 2;
}
}
} else {
uint64_t flags;
/* 1 of 4 colors for each 2x1 or 1x2 block */
CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 8);
flags = bytestream_get_le64(&s->stream_ptr);
if (!(P[2] & 0x8000)) {
for (y = 0; y < 8; y++) {
for (x = 0; x < 8; x += 2, flags >>= 2) {
pixel_ptr[x ] =
pixel_ptr[x + 1] = P[flags & 0x03];
}
pixel_ptr += s->stride;
}
} else {
for (y = 0; y < 8; y += 2) {
for (x = 0; x < 8; x++, flags >>= 2) {
pixel_ptr[x ] =
pixel_ptr[x + s->stride] = P[flags & 0x03];
}
pixel_ptr += s->stride * 2;
}
}
}
/* report success */
return 0;
}
static int ipvideo_decode_block_opcode_0xA_16(IpvideoContext *s)
{
int x, y;
uint16_t P[4];
int flags = 0;
uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
/* 4-color encoding for each 4x4 quadrant, or 4-color encoding on
* either top and bottom or left and right halves */
CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 24);
if (!(AV_RL16(s->stream_ptr) & 0x8000)) {
/* 4-color encoding for each quadrant */
CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 48);
for (y = 0; y < 16; y++) {
// new values for each 4x4 block
if (!(y & 3)) {
for (x = 0; x < 4; x++)
P[x] = bytestream_get_le16(&s->stream_ptr);
flags = bytestream_get_le32(&s->stream_ptr);
}
for (x = 0; x < 4; x++, flags >>= 2)
*pixel_ptr++ = P[flags & 0x03];
pixel_ptr += s->stride - 4;
// switch to right half
if (y == 7) pixel_ptr -= 8 * s->stride - 4;
}
} else {
// vertical split?
int vert = !(AV_RL16(s->stream_ptr + 16) & 0x8000);
uint64_t flags = 0;
/* 4-color encoding for either left and right or top and bottom
* halves */
for (y = 0; y < 16; y++) {
// load values for each half
if (!(y & 7)) {
for (x = 0; x < 4; x++)
P[x] = bytestream_get_le16(&s->stream_ptr);
flags = bytestream_get_le64(&s->stream_ptr);
}
for (x = 0; x < 4; x++, flags >>= 2)
*pixel_ptr++ = P[flags & 0x03];
if (vert) {
pixel_ptr += s->stride - 4;
// switch to right half
if (y == 7) pixel_ptr -= 8 * s->stride - 4;
} else if (y & 1) pixel_ptr += s->line_inc;
}
}
/* report success */
return 0;
}
static int ipvideo_decode_block_opcode_0xB_16(IpvideoContext *s)
{
int x, y;
uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
/* 64-color encoding (each pixel in block is a different color) */
CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 128);
for (y = 0; y < 8; y++) {
for (x = 0; x < 8; x++)
pixel_ptr[x] = bytestream_get_le16(&s->stream_ptr);
pixel_ptr += s->stride;
}
/* report success */
return 0;
}
static int ipvideo_decode_block_opcode_0xC_16(IpvideoContext *s)
{
int x, y;
uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
/* 16-color block encoding: each 2x2 block is a different color */
CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 32);
for (y = 0; y < 8; y += 2) {
for (x = 0; x < 8; x += 2) {
pixel_ptr[x ] =
pixel_ptr[x + 1 ] =
pixel_ptr[x + s->stride] =
pixel_ptr[x + 1 + s->stride] = bytestream_get_le16(&s->stream_ptr);
}
pixel_ptr += s->stride * 2;
}
/* report success */
return 0;
}
static int ipvideo_decode_block_opcode_0xD_16(IpvideoContext *s)
{
int x, y;
uint16_t P[2];
uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
/* 4-color block encoding: each 4x4 block is a different color */
CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 8);
for (y = 0; y < 8; y++) {
if (!(y & 3)) {
P[0] = bytestream_get_le16(&s->stream_ptr);
P[1] = bytestream_get_le16(&s->stream_ptr);
}
for (x = 0; x < 8; x++)
pixel_ptr[x] = P[x >> 2];
pixel_ptr += s->stride;
}
/* report success */
return 0;
}
static int ipvideo_decode_block_opcode_0xE_16(IpvideoContext *s)
{
int x, y;
uint16_t pix;
uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
/* 1-color encoding: the whole block is 1 solid color */
CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 2);
pix = bytestream_get_le16(&s->stream_ptr);
for (y = 0; y < 8; y++) {
for (x = 0; x < 8; x++)
pixel_ptr[x] = pix;
pixel_ptr += s->stride;
}
/* report success */
return 0;
}
static int (* const ipvideo_decode_block[])(IpvideoContext *s) = {
ipvideo_decode_block_opcode_0x0, ipvideo_decode_block_opcode_0x1,
ipvideo_decode_block_opcode_0x2, ipvideo_decode_block_opcode_0x3,
ipvideo_decode_block_opcode_0x4, ipvideo_decode_block_opcode_0x5,
ipvideo_decode_block_opcode_0x6, ipvideo_decode_block_opcode_0x7,
ipvideo_decode_block_opcode_0x8, ipvideo_decode_block_opcode_0x9,
ipvideo_decode_block_opcode_0xA, ipvideo_decode_block_opcode_0xB,
ipvideo_decode_block_opcode_0xC, ipvideo_decode_block_opcode_0xD,
ipvideo_decode_block_opcode_0xE, ipvideo_decode_block_opcode_0xF,
};
static int (* const ipvideo_decode_block16[])(IpvideoContext *s) = {
ipvideo_decode_block_opcode_0x0, ipvideo_decode_block_opcode_0x1,
ipvideo_decode_block_opcode_0x2, ipvideo_decode_block_opcode_0x3,
ipvideo_decode_block_opcode_0x4, ipvideo_decode_block_opcode_0x5,
ipvideo_decode_block_opcode_0x6_16, ipvideo_decode_block_opcode_0x7_16,
ipvideo_decode_block_opcode_0x8_16, ipvideo_decode_block_opcode_0x9_16,
ipvideo_decode_block_opcode_0xA_16, ipvideo_decode_block_opcode_0xB_16,
ipvideo_decode_block_opcode_0xC_16, ipvideo_decode_block_opcode_0xD_16,
ipvideo_decode_block_opcode_0xE_16, ipvideo_decode_block_opcode_0x1,
};
static void ipvideo_decode_opcodes(IpvideoContext *s)
{
int x, y;
unsigned char opcode;
int ret;
static int frame = 0;
GetBitContext gb;
debug_interplay("------------------ frame %d\n", frame);
frame++;
if (!s->is_16bpp) {
/* this is PAL8, so make the palette available */
memcpy(s->current_frame.data[1], s->avctx->palctrl->palette, PALETTE_COUNT * 4);
s->stride = s->current_frame.linesize[0];
s->stream_ptr = s->buf + 14; /* data starts 14 bytes in */
s->stream_end = s->buf + s->size;
} else {
s->stride = s->current_frame.linesize[0] >> 1;
s->stream_ptr = s->buf + 16;
s->stream_end =
s->mv_ptr = s->buf + 14 + AV_RL16(s->buf+14);
s->mv_end = s->buf + s->size;
}
s->line_inc = s->stride - 8;
s->upper_motion_limit_offset = (s->avctx->height - 8) * s->current_frame.linesize[0]
+ (s->avctx->width - 8) * (1 + s->is_16bpp);
init_get_bits(&gb, s->decoding_map, s->decoding_map_size * 8);
for (y = 0; y < s->avctx->height; y += 8) {
for (x = 0; x < s->avctx->width; x += 8) {
opcode = get_bits(&gb, 4);
debug_interplay(" block @ (%3d, %3d): encoding 0x%X, data ptr @ %p\n",
x, y, opcode, s->stream_ptr);
if (!s->is_16bpp) {
s->pixel_ptr = s->current_frame.data[0] + x
+ y*s->current_frame.linesize[0];
ret = ipvideo_decode_block[opcode](s);
} else {
s->pixel_ptr = s->current_frame.data[0] + x*2
+ y*s->current_frame.linesize[0];
ret = ipvideo_decode_block16[opcode](s);
}
if (ret != 0) {
av_log(s->avctx, AV_LOG_ERROR, " Interplay video: decode problem on frame %d, @ block (%d, %d)\n",
frame, x, y);
return;
}
}
}
if (s->stream_end - s->stream_ptr > 1) {
av_log(s->avctx, AV_LOG_ERROR, " Interplay video: decode finished with %td bytes left over\n",
s->stream_end - s->stream_ptr);
}
}
static av_cold int ipvideo_decode_init(AVCodecContext *avctx)
{
IpvideoContext *s = avctx->priv_data;
s->avctx = avctx;
s->is_16bpp = avctx->bits_per_coded_sample == 16;
avctx->pix_fmt = s->is_16bpp ? PIX_FMT_RGB555 : PIX_FMT_PAL8;
if (!s->is_16bpp && s->avctx->palctrl == NULL) {
av_log(avctx, AV_LOG_ERROR, " Interplay video: palette expected.\n");
return -1;
}
dsputil_init(&s->dsp, avctx);
/* decoding map contains 4 bits of information per 8x8 block */
s->decoding_map_size = avctx->width * avctx->height / (8 * 8 * 2);
s->current_frame.data[0] = s->last_frame.data[0] =
s->second_last_frame.data[0] = NULL;
return 0;
}
static int ipvideo_decode_frame(AVCodecContext *avctx,
void *data, int *data_size,
AVPacket *avpkt)
{
const uint8_t *buf = avpkt->data;
int buf_size = avpkt->size;
IpvideoContext *s = avctx->priv_data;
AVPaletteControl *palette_control = avctx->palctrl;
/* compressed buffer needs to be large enough to at least hold an entire
* decoding map */
if (buf_size < s->decoding_map_size)
return buf_size;
s->decoding_map = buf;
s->buf = buf + s->decoding_map_size;
s->size = buf_size - s->decoding_map_size;
s->current_frame.reference = 3;
if (avctx->get_buffer(avctx, &s->current_frame)) {
av_log(avctx, AV_LOG_ERROR, " Interplay Video: get_buffer() failed\n");
return -1;
}
ipvideo_decode_opcodes(s);
if (!s->is_16bpp && palette_control->palette_changed) {
palette_control->palette_changed = 0;
s->current_frame.palette_has_changed = 1;
}
*data_size = sizeof(AVFrame);
*(AVFrame*)data = s->current_frame;
/* shuffle frames */
if (s->second_last_frame.data[0])
avctx->release_buffer(avctx, &s->second_last_frame);
s->second_last_frame = s->last_frame;
s->last_frame = s->current_frame;
s->current_frame.data[0] = NULL; /* catch any access attempts */
/* report that the buffer was completely consumed */
return buf_size;
}
static av_cold int ipvideo_decode_end(AVCodecContext *avctx)
{
IpvideoContext *s = avctx->priv_data;
/* release the last frame */
if (s->last_frame.data[0])
avctx->release_buffer(avctx, &s->last_frame);
if (s->second_last_frame.data[0])
avctx->release_buffer(avctx, &s->second_last_frame);
return 0;
}
AVCodec interplay_video_decoder = {
"interplayvideo",
AVMEDIA_TYPE_VIDEO,
CODEC_ID_INTERPLAY_VIDEO,
sizeof(IpvideoContext),
ipvideo_decode_init,
NULL,
ipvideo_decode_end,
ipvideo_decode_frame,
CODEC_CAP_DR1,
.long_name = NULL_IF_CONFIG_SMALL("Interplay MVE video"),
};
| 123linslouis-android-video-cutter | jni/libavcodec/interplayvideo.c | C | asf20 | 33,298 |
/*
* RealAudio 2.0 (28.8K)
* Copyright (c) 2003 the ffmpeg project
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "avcodec.h"
#define ALT_BITSTREAM_READER_LE
#include "get_bits.h"
#include "ra288.h"
#include "lpc.h"
#include "celp_math.h"
#include "celp_filters.h"
typedef struct {
float sp_lpc[36]; ///< LPC coefficients for speech data (spec: A)
float gain_lpc[10]; ///< LPC coefficients for gain (spec: GB)
/** speech data history (spec: SB).
* Its first 70 coefficients are updated only at backward filtering.
*/
float sp_hist[111];
/// speech part of the gain autocorrelation (spec: REXP)
float sp_rec[37];
/** log-gain history (spec: SBLG).
* Its first 28 coefficients are updated only at backward filtering.
*/
float gain_hist[38];
/// recursive part of the gain autocorrelation (spec: REXPLG)
float gain_rec[11];
} RA288Context;
static av_cold int ra288_decode_init(AVCodecContext *avctx)
{
avctx->sample_fmt = SAMPLE_FMT_FLT;
return 0;
}
static void apply_window(float *tgt, const float *m1, const float *m2, int n)
{
while (n--)
*tgt++ = *m1++ * *m2++;
}
static void convolve(float *tgt, const float *src, int len, int n)
{
for (; n >= 0; n--)
tgt[n] = ff_dot_productf(src, src - n, len);
}
static void decode(RA288Context *ractx, float gain, int cb_coef)
{
int i;
double sumsum;
float sum, buffer[5];
float *block = ractx->sp_hist + 70 + 36; // current block
float *gain_block = ractx->gain_hist + 28;
memmove(ractx->sp_hist + 70, ractx->sp_hist + 75, 36*sizeof(*block));
/* block 46 of G.728 spec */
sum = 32.;
for (i=0; i < 10; i++)
sum -= gain_block[9-i] * ractx->gain_lpc[i];
/* block 47 of G.728 spec */
sum = av_clipf(sum, 0, 60);
/* block 48 of G.728 spec */
/* exp(sum * 0.1151292546497) == pow(10.0,sum/20) */
sumsum = exp(sum * 0.1151292546497) * gain * (1.0/(1<<23));
for (i=0; i < 5; i++)
buffer[i] = codetable[cb_coef][i] * sumsum;
sum = ff_dot_productf(buffer, buffer, 5) * ((1<<24)/5.);
sum = FFMAX(sum, 1);
/* shift and store */
memmove(gain_block, gain_block + 1, 9 * sizeof(*gain_block));
gain_block[9] = 10 * log10(sum) - 32;
ff_celp_lp_synthesis_filterf(block, ractx->sp_lpc, buffer, 5, 36);
}
/**
* Hybrid window filtering, see blocks 36 and 49 of the G.728 specification.
*
* @param order filter order
* @param n input length
* @param non_rec number of non-recursive samples
* @param out filter output
* @param hist pointer to the input history of the filter
* @param out pointer to the non-recursive part of the output
* @param out2 pointer to the recursive part of the output
* @param window pointer to the windowing function table
*/
static void do_hybrid_window(int order, int n, int non_rec, float *out,
float *hist, float *out2, const float *window)
{
int i;
float buffer1[order + 1];
float buffer2[order + 1];
float work[order + n + non_rec];
apply_window(work, window, hist, order + n + non_rec);
convolve(buffer1, work + order , n , order);
convolve(buffer2, work + order + n, non_rec, order);
for (i=0; i <= order; i++) {
out2[i] = out2[i] * 0.5625 + buffer1[i];
out [i] = out2[i] + buffer2[i];
}
/* Multiply by the white noise correcting factor (WNCF). */
*out *= 257./256.;
}
/**
* Backward synthesis filter, find the LPC coefficients from past speech data.
*/
static void backward_filter(float *hist, float *rec, const float *window,
float *lpc, const float *tab,
int order, int n, int non_rec, int move_size)
{
float temp[order+1];
do_hybrid_window(order, n, non_rec, temp, hist, rec, window);
if (!compute_lpc_coefs(temp, order, lpc, 0, 1, 1))
apply_window(lpc, lpc, tab, order);
memmove(hist, hist + n, move_size*sizeof(*hist));
}
static int ra288_decode_frame(AVCodecContext * avctx, void *data,
int *data_size, AVPacket *avpkt)
{
const uint8_t *buf = avpkt->data;
int buf_size = avpkt->size;
float *out = data;
int i, j;
RA288Context *ractx = avctx->priv_data;
GetBitContext gb;
if (buf_size < avctx->block_align) {
av_log(avctx, AV_LOG_ERROR,
"Error! Input buffer is too small [%d<%d]\n",
buf_size, avctx->block_align);
return 0;
}
if (*data_size < 32*5*4)
return -1;
init_get_bits(&gb, buf, avctx->block_align * 8);
for (i=0; i < 32; i++) {
float gain = amptable[get_bits(&gb, 3)];
int cb_coef = get_bits(&gb, 6 + (i&1));
decode(ractx, gain, cb_coef);
for (j=0; j < 5; j++)
*(out++) = ractx->sp_hist[70 + 36 + j];
if ((i & 7) == 3) {
backward_filter(ractx->sp_hist, ractx->sp_rec, syn_window,
ractx->sp_lpc, syn_bw_tab, 36, 40, 35, 70);
backward_filter(ractx->gain_hist, ractx->gain_rec, gain_window,
ractx->gain_lpc, gain_bw_tab, 10, 8, 20, 28);
}
}
*data_size = (char *)out - (char *)data;
return avctx->block_align;
}
AVCodec ra_288_decoder =
{
"real_288",
AVMEDIA_TYPE_AUDIO,
CODEC_ID_RA_288,
sizeof(RA288Context),
ra288_decode_init,
NULL,
NULL,
ra288_decode_frame,
.long_name = NULL_IF_CONFIG_SMALL("RealAudio 2.0 (28.8K)"),
};
| 123linslouis-android-video-cutter | jni/libavcodec/ra288.c | C | asf20 | 6,409 |
/*
* Copyright (c) 2004 François Revol <revol@free.fr>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
//#define DEBUG
#include "avcodec.h"
#include <OS.h>
typedef struct ThreadContext{
AVCodecContext *avctx;
thread_id thread;
sem_id work_sem;
sem_id done_sem;
int (*func)(AVCodecContext *c, void *arg);
void *arg;
int ret;
}ThreadContext;
// it's odd Be never patented that :D
struct benaphore {
vint32 atom;
sem_id sem;
};
static inline int lock_ben(struct benaphore *ben)
{
if (atomic_add(&ben->atom, 1) > 0)
return acquire_sem(ben->sem);
return B_OK;
}
static inline int unlock_ben(struct benaphore *ben)
{
if (atomic_add(&ben->atom, -1) > 1)
return release_sem(ben->sem);
return B_OK;
}
static struct benaphore av_thread_lib_ben;
static int32 ff_thread_func(void *v){
ThreadContext *c= v;
for(;;){
//printf("thread_func %X enter wait\n", (int)v); fflush(stdout);
acquire_sem(c->work_sem);
//printf("thread_func %X after wait (func=%X)\n", (int)v, (int)c->func); fflush(stdout);
if(c->func)
c->ret= c->func(c->avctx, c->arg);
else
return 0;
//printf("thread_func %X signal complete\n", (int)v); fflush(stdout);
release_sem(c->done_sem);
}
return B_OK;
}
/**
* Free what has been allocated by avcodec_thread_init().
* Must be called after decoding has finished, especially do not call while avcodec_thread_execute() is running.
*/
void avcodec_thread_free(AVCodecContext *s){
ThreadContext *c= s->thread_opaque;
int i;
int32 ret;
for(i=0; i<s->thread_count; i++){
c[i].func= NULL;
release_sem(c[i].work_sem);
wait_for_thread(c[i].thread, &ret);
if(c[i].work_sem > B_OK) delete_sem(c[i].work_sem);
if(c[i].done_sem > B_OK) delete_sem(c[i].done_sem);
}
av_freep(&s->thread_opaque);
}
static int avcodec_thread_execute(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
ThreadContext *c= s->thread_opaque;
int i;
assert(s == c->avctx);
assert(count <= s->thread_count);
/* note, we can be certain that this is not called with the same AVCodecContext by different threads at the same time */
for(i=0; i<count; i++){
c[i].arg= (char*)arg + i*size;
c[i].func= func;
c[i].ret= 12345;
release_sem(c[i].work_sem);
}
for(i=0; i<count; i++){
acquire_sem(c[i].done_sem);
c[i].func= NULL;
if(ret) ret[i]= c[i].ret;
}
return 0;
}
int avcodec_thread_init(AVCodecContext *s, int thread_count){
int i;
ThreadContext *c;
s->thread_count= thread_count;
if (thread_count <= 1)
return 0;
assert(!s->thread_opaque);
c= av_mallocz(sizeof(ThreadContext)*thread_count);
s->thread_opaque= c;
for(i=0; i<thread_count; i++){
//printf("init semaphors %d\n", i); fflush(stdout);
c[i].avctx= s;
if((c[i].work_sem = create_sem(0, "ff work sem")) < B_OK)
goto fail;
if((c[i].done_sem = create_sem(0, "ff done sem")) < B_OK)
goto fail;
//printf("create thread %d\n", i); fflush(stdout);
c[i].thread = spawn_thread(ff_thread_func, "libavcodec thread", B_LOW_PRIORITY, &c[i] );
if( c[i].thread < B_OK ) goto fail;
resume_thread(c[i].thread );
}
//printf("init done\n"); fflush(stdout);
s->execute= avcodec_thread_execute;
return 0;
fail:
avcodec_thread_free(s);
return -1;
}
/* provide a mean to serialize calls to avcodec_*() for thread safety. */
int avcodec_thread_lock_lib(void)
{
return lock_ben(&av_thread_lib_ben);
}
int avcodec_thread_unlock_lib(void)
{
return unlock_ben(&av_thread_lib_ben);
}
/* our versions of _init and _fini (which are called by those actually from crt.o) */
void initialize_after(void)
{
av_thread_lib_ben.atom = 0;
av_thread_lib_ben.sem = create_sem(0, "libavcodec benaphore");
}
void uninitialize_before(void)
{
delete_sem(av_thread_lib_ben.sem);
}
| 123linslouis-android-video-cutter | jni/libavcodec/beosthread.c | C | asf20 | 4,872 |
/*
* common functions for Indeo Video Interactive codecs (Indeo4 and Indeo5)
*
* Copyright (c) 2009 Maxim Poliakovski
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* This file contains structures and macros shared by both Indeo4 and
* Indeo5 decoders.
*/
#ifndef AVCODEC_IVI_COMMON_H
#define AVCODEC_IVI_COMMON_H
#include "avcodec.h"
#include "get_bits.h"
#include <stdint.h>
#define IVI_DEBUG 0
#define IVI_VLC_BITS 13 ///< max number of bits of the ivi's huffman codes
/**
* huffman codebook descriptor
*/
typedef struct {
int32_t num_rows;
uint8_t xbits[16];
} IVIHuffDesc;
/**
* macroblock/block huffman table descriptor
*/
typedef struct {
int32_t tab_sel; /// index of one of the predefined tables
/// or "7" for custom one
VLC *tab; /// pointer to the table associated with tab_sel
//! the following are used only when tab_sel == 7
IVIHuffDesc cust_desc; /// custom Huffman codebook descriptor
VLC cust_tab; /// vlc table for custom codebook
} IVIHuffTab;
enum {
IVI_MB_HUFF = 0, /// Huffman table is used for coding macroblocks
IVI_BLK_HUFF = 1 /// Huffman table is used for coding blocks
};
extern VLC ff_ivi_mb_vlc_tabs [8]; ///< static macroblock Huffman tables
extern VLC ff_ivi_blk_vlc_tabs[8]; ///< static block Huffman tables
/**
* run-value (RLE) table descriptor
*/
typedef struct {
uint8_t eob_sym; ///< end of block symbol
uint8_t esc_sym; ///< escape symbol
uint8_t runtab[256];
int8_t valtab[256];
} RVMapDesc;
extern const RVMapDesc ff_ivi_rvmap_tabs[9];
/**
* information for Indeo macroblock (16x16, 8x8 or 4x4)
*/
typedef struct {
int16_t xpos;
int16_t ypos;
uint32_t buf_offs; ///< address in the output buffer for this mb
uint8_t type; ///< macroblock type: 0 - INTRA, 1 - INTER
uint8_t cbp; ///< coded block pattern
int8_t q_delta; ///< quant delta
int8_t mv_x; ///< motion vector (x component)
int8_t mv_y; ///< motion vector (y component)
} IVIMbInfo;
/**
* information for Indeo tile
*/
typedef struct {
int xpos;
int ypos;
int width;
int height;
int is_empty; ///< = 1 if this tile doesn't contain any data
int data_size; ///< size of the data in bytes
int num_MBs; ///< number of macroblocks in this tile
IVIMbInfo *mbs; ///< array of macroblock descriptors
IVIMbInfo *ref_mbs; ///< ptr to the macroblock descriptors of the reference tile
} IVITile;
/**
* information for Indeo wavelet band
*/
typedef struct {
int plane; ///< plane number this band belongs to
int band_num; ///< band number
int width;
int height;
const uint8_t *data_ptr; ///< ptr to the first byte of the band data
int data_size; ///< size of the band data
int16_t *buf; ///< pointer to the output buffer for this band
int16_t *ref_buf; ///< pointer to the reference frame buffer (for motion compensation)
int16_t *bufs[3]; ///< array of pointers to the band buffers
int pitch; ///< pitch associated with the buffers above
int is_empty; ///< = 1 if this band doesn't contain any data
int mb_size; ///< macroblock size
int blk_size; ///< block size
int is_halfpel; ///< precision of the motion compensation: 0 - fullpel, 1 - halfpel
int inherit_mv; ///< tells if motion vector is inherited from reference macroblock
int inherit_qdelta; ///< tells if quantiser delta is inherited from reference macroblock
int qdelta_present; ///< tells if Qdelta signal is present in the bitstream (Indeo5 only)
int quant_mat; ///< dequant matrix index
int glob_quant; ///< quant base for this band
const uint8_t *scan; ///< ptr to the scan pattern
IVIHuffTab blk_vlc; ///< vlc table for decoding block data
uint16_t *dequant_intra; ///< ptr to dequant tables for intra blocks
uint16_t *dequant_inter; ///< ptr dequant tables for inter blocks
int num_corr; ///< number of correction entries
uint8_t corr[61*2]; ///< rvmap correction pairs
int rvmap_sel; ///< rvmap table selector
RVMapDesc *rv_map; ///< ptr to the RLE table for this band
int num_tiles; ///< number of tiles in this band
IVITile *tiles; ///< array of tile descriptors
void (*inv_transform)(const int32_t *in, int16_t *out, uint32_t pitch, const uint8_t *flags); ///< inverse transform function pointer
void (*dc_transform) (const int32_t *in, int16_t *out, uint32_t pitch, int blk_size); ///< dc transform function pointer, it may be NULL
int is_2d_trans; ///< 1 indicates that the two-dimensional inverse transform is used
int32_t checksum; ///< for debug purposes
int checksum_present;
int bufsize; ///< band buffer size in bytes
const uint8_t *intra_base; ///< quantization matrix for intra blocks
const uint8_t *inter_base; ///< quantization matrix for inter blocks
const uint8_t *intra_scale; ///< quantization coefficient for intra blocks
const uint8_t *inter_scale; ///< quantization coefficient for inter blocks
} IVIBandDesc;
/**
* color plane (luma or chroma) information
*/
typedef struct {
uint16_t width;
uint16_t height;
uint8_t num_bands; ///< number of bands this plane subdivided into
IVIBandDesc *bands; ///< array of band descriptors
} IVIPlaneDesc;
typedef struct {
uint16_t pic_width;
uint16_t pic_height;
uint16_t chroma_width;
uint16_t chroma_height;
uint16_t tile_width;
uint16_t tile_height;
uint8_t luma_bands;
uint8_t chroma_bands;
} IVIPicConfig;
/** compares some properties of two pictures */
static inline int ivi_pic_config_cmp(IVIPicConfig *str1, IVIPicConfig *str2)
{
return (str1->pic_width != str2->pic_width || str1->pic_height != str2->pic_height ||
str1->chroma_width != str2->chroma_width || str1->chroma_height != str2->chroma_height ||
str1->tile_width != str2->tile_width || str1->tile_height != str2->tile_height ||
str1->luma_bands != str2->luma_bands || str1->chroma_bands != str2->chroma_bands);
}
/** calculate number of tiles in a stride */
#define IVI_NUM_TILES(stride, tile_size) (((stride) + (tile_size) - 1) / (tile_size))
/** calculate number of macroblocks in a tile */
#define IVI_MBs_PER_TILE(tile_width, tile_height, mb_size) \
((((tile_width) + (mb_size) - 1) / (mb_size)) * (((tile_height) + (mb_size) - 1) / (mb_size)))
/** convert unsigned values into signed ones (the sign is in the LSB) */
#define IVI_TOSIGNED(val) (-(((val) >> 1) ^ -((val) & 1)))
/** scales motion vector */
static inline int ivi_scale_mv(int mv, int mv_scale)
{
return (mv + (mv > 0) + (mv_scale - 1)) >> mv_scale;
}
/**
* Generates a huffman codebook from the given descriptor
* and converts it into the FFmpeg VLC table.
*
* @param cb [in] pointer to codebook descriptor
* @param vlc [out] where to place the generated VLC table
* @param flag [in] flag: 1 - for static or 0 for dynamic tables
* @return result code: 0 - OK, -1 = error (invalid codebook descriptor)
*/
int ff_ivi_create_huff_from_desc(const IVIHuffDesc *cb, VLC *vlc, int flag);
/**
* Initializes static codes used for macroblock and block decoding.
*/
void ff_ivi_init_static_vlc(void);
/**
* Decodes a huffman codebook descriptor from the bitstream
* and selects specified huffman table.
*
* @param gb [in,out] the GetBit context
* @param desc_coded [in] flag signalling if table descriptor was coded
* @param which_tab [in] codebook purpose (IVI_MB_HUFF or IVI_BLK_HUFF)
* @param huff_tab [out] pointer to the descriptor of the selected table
* @param avctx [in] AVCodecContext pointer
* @return zero on success, negative value otherwise
*/
int ff_ivi_dec_huff_desc(GetBitContext *gb, int desc_coded, int which_tab,
IVIHuffTab *huff_tab, AVCodecContext *avctx);
/**
* Compares two huffman codebook descriptors.
*
* @param desc1 [in] ptr to the 1st descriptor to compare
* @param desc2 [in] ptr to the 2nd descriptor to compare
* @return comparison result: 0 - equal, 1 - not equal
*/
int ff_ivi_huff_desc_cmp(const IVIHuffDesc *desc1, const IVIHuffDesc *desc2);
/**
* Copies huffman codebook descriptors.
*
* @param dst [out] ptr to the destination descriptor
* @param src [in] ptr to the source descriptor
*/
void ff_ivi_huff_desc_copy(IVIHuffDesc *dst, const IVIHuffDesc *src);
/**
* Initializes planes (prepares descriptors, allocates buffers etc).
*
* @param planes [in,out] pointer to the array of the plane descriptors
* @param cfg [in] pointer to the ivi_pic_config structure describing picture layout
* @return result code: 0 - OK
*/
int ff_ivi_init_planes(IVIPlaneDesc *planes, const IVIPicConfig *cfg);
/**
* Frees planes, bands and macroblocks buffers.
*
* @param planes [in] pointer to the array of the plane descriptors
*/
void ff_ivi_free_buffers(IVIPlaneDesc *planes);
/**
* Initializes tile and macroblock descriptors.
*
* @param planes [in,out] pointer to the array of the plane descriptors
* @param tile_width [in] tile width
* @param tile_height [in] tile height
* @return result code: 0 - OK
*/
int ff_ivi_init_tiles(IVIPlaneDesc *planes, int tile_width, int tile_height);
/**
* Decodes size of the tile data.
* The size is stored as a variable-length field having the following format:
* if (tile_data_size < 255) than this field is only one byte long
* if (tile_data_size >= 255) than this field four is byte long: 0xFF X1 X2 X3
* where X1-X3 is size of the tile data
*
* @param gb [in,out] the GetBit context
* @return size of the tile data in bytes
*/
int ff_ivi_dec_tile_data_size(GetBitContext *gb);
/**
* Decodes block data:
* extracts huffman-coded transform coefficients from the bitstream,
* dequantizes them, applies inverse transform and motion compensation
* in order to reconstruct the picture.
*
* @param gb [in,out] the GetBit context
* @param band [in] pointer to the band descriptor
* @param tile [in] pointer to the tile descriptor
* @return result code: 0 - OK, -1 = error (corrupted blocks data)
*/
int ff_ivi_decode_blocks(GetBitContext *gb, IVIBandDesc *band, IVITile *tile);
/**
* Handles empty tiles by performing data copying and motion
* compensation respectively.
*
* @param avctx [in] ptr to the AVCodecContext
* @param band [in] pointer to the band descriptor
* @param tile [in] pointer to the tile descriptor
* @param mv_scale [in] scaling factor for motion vectors
*/
void ff_ivi_process_empty_tile(AVCodecContext *avctx, IVIBandDesc *band,
IVITile *tile, int32_t mv_scale);
/**
* Converts and outputs the current plane.
* This conversion is done by adding back the bias value of 128
* (subtracted in the encoder) and clipping the result.
*
* @param plane [in] pointer to the descriptor of the plane being processed
* @param dst [out] pointer to the buffer receiving converted pixels
* @param dst_pitch [in] pitch for moving to the next y line
*/
void ff_ivi_output_plane(IVIPlaneDesc *plane, uint8_t *dst, int dst_pitch);
#if IVI_DEBUG
/**
* Calculates band checksum from band data.
*/
uint16_t ivi_calc_band_checksum (IVIBandDesc *band);
/**
* Verifies that band data lies in range.
*/
int ivi_check_band (IVIBandDesc *band, const uint8_t *ref, int pitch);
#endif
#endif /* AVCODEC_IVI_COMMON_H */
| 123linslouis-android-video-cutter | jni/libavcodec/ivi_common.h | C | asf20 | 13,066 |
/*
* FLV specific private header.
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef AVCODEC_FLV_H
#define AVCODEC_FLV_H
#include "mpegvideo.h"
#include "get_bits.h"
#include "put_bits.h"
void ff_flv_encode_picture_header(MpegEncContext * s, int picture_number);
void ff_flv2_encode_ac_esc(PutBitContext *pb, int slevel, int level, int run, int last);
int ff_flv_decode_picture_header(MpegEncContext *s);
void ff_flv2_decode_ac_esc(GetBitContext *gb, int *level, int *run, int *last);
#endif
| 123linslouis-android-video-cutter | jni/libavcodec/flv.h | C | asf20 | 1,216 |
/*
* MPEG-4 Parametric Stereo decoding functions
* Copyright (c) 2010 Alex Converse <alex.converse@gmail.com>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdint.h>
#include "libavutil/common.h"
#include "libavutil/mathematics.h"
#include "avcodec.h"
#include "get_bits.h"
#include "aacps.h"
#include "aacps_tablegen.h"
#include "aacpsdata.c"
#define PS_BASELINE 0 //< Operate in Baseline PS mode
//< Baseline implies 10 or 20 stereo bands,
//< mixing mode A, and no ipd/opd
#define numQMFSlots 32 //numTimeSlots * RATE
static const int8_t num_env_tab[2][4] = {
{ 0, 1, 2, 4, },
{ 1, 2, 3, 4, },
};
static const int8_t nr_iidicc_par_tab[] = {
10, 20, 34, 10, 20, 34,
};
static const int8_t nr_iidopd_par_tab[] = {
5, 11, 17, 5, 11, 17,
};
enum {
huff_iid_df1,
huff_iid_dt1,
huff_iid_df0,
huff_iid_dt0,
huff_icc_df,
huff_icc_dt,
huff_ipd_df,
huff_ipd_dt,
huff_opd_df,
huff_opd_dt,
};
static const int huff_iid[] = {
huff_iid_df0,
huff_iid_df1,
huff_iid_dt0,
huff_iid_dt1,
};
static VLC vlc_ps[10];
/**
* Read Inter-channel Intensity Difference/Inter-Channel Coherence/
* Inter-channel Phase Difference/Overall Phase Difference parameters from the
* bitstream.
*
* @param avctx contains the current codec context
* @param gb pointer to the input bitstream
* @param ps pointer to the Parametric Stereo context
* @param par pointer to the parameter to be read
* @param e envelope to decode
* @param dt 1: time delta-coded, 0: frequency delta-coded
*/
#define READ_PAR_DATA(PAR, OFFSET, MASK, ERR_CONDITION) \
static int read_ ## PAR ## _data(AVCodecContext *avctx, GetBitContext *gb, PSContext *ps, \
int8_t (*PAR)[PS_MAX_NR_IIDICC], int table_idx, int e, int dt) \
{ \
int b, num = ps->nr_ ## PAR ## _par; \
VLC_TYPE (*vlc_table)[2] = vlc_ps[table_idx].table; \
if (dt) { \
int e_prev = e ? e - 1 : ps->num_env_old - 1; \
e_prev = FFMAX(e_prev, 0); \
for (b = 0; b < num; b++) { \
int val = PAR[e_prev][b] + get_vlc2(gb, vlc_table, 9, 3) - OFFSET; \
if (MASK) val &= MASK; \
PAR[e][b] = val; \
if (ERR_CONDITION) \
goto err; \
} \
} else { \
int val = 0; \
for (b = 0; b < num; b++) { \
val += get_vlc2(gb, vlc_table, 9, 3) - OFFSET; \
if (MASK) val &= MASK; \
PAR[e][b] = val; \
if (ERR_CONDITION) \
goto err; \
} \
} \
return 0; \
err: \
av_log(avctx, AV_LOG_ERROR, "illegal "#PAR"\n"); \
return -1; \
}
READ_PAR_DATA(iid, huff_offset[table_idx], 0, FFABS(ps->iid_par[e][b]) > 7 + 8 * ps->iid_quant)
READ_PAR_DATA(icc, huff_offset[table_idx], 0, ps->icc_par[e][b] > 7U)
READ_PAR_DATA(ipdopd, 0, 0x07, 0)
static int ps_read_extension_data(GetBitContext *gb, PSContext *ps, int ps_extension_id)
{
int e;
int count = get_bits_count(gb);
if (ps_extension_id)
return 0;
ps->enable_ipdopd = get_bits1(gb);
if (ps->enable_ipdopd) {
for (e = 0; e < ps->num_env; e++) {
int dt = get_bits1(gb);
read_ipdopd_data(NULL, gb, ps, ps->ipd_par, dt ? huff_ipd_dt : huff_ipd_df, e, dt);
dt = get_bits1(gb);
read_ipdopd_data(NULL, gb, ps, ps->opd_par, dt ? huff_opd_dt : huff_opd_df, e, dt);
}
}
skip_bits1(gb); //reserved_ps
return get_bits_count(gb) - count;
}
static void ipdopd_reset(int8_t *opd_hist, int8_t *ipd_hist)
{
int i;
for (i = 0; i < PS_MAX_NR_IPDOPD; i++) {
opd_hist[i] = 0;
ipd_hist[i] = 0;
}
}
int ff_ps_read_data(AVCodecContext *avctx, GetBitContext *gb_host, PSContext *ps, int bits_left)
{
int e;
int bit_count_start = get_bits_count(gb_host);
int header;
int bits_consumed;
GetBitContext gbc = *gb_host, *gb = &gbc;
header = get_bits1(gb);
if (header) { //enable_ps_header
ps->enable_iid = get_bits1(gb);
if (ps->enable_iid) {
int iid_mode = get_bits(gb, 3);
if (iid_mode > 5) {
av_log(avctx, AV_LOG_ERROR, "iid_mode %d is reserved.\n",
iid_mode);
goto err;
}
ps->nr_iid_par = nr_iidicc_par_tab[iid_mode];
ps->iid_quant = iid_mode > 2;
ps->nr_ipdopd_par = nr_iidopd_par_tab[iid_mode];
}
ps->enable_icc = get_bits1(gb);
if (ps->enable_icc) {
ps->icc_mode = get_bits(gb, 3);
if (ps->icc_mode > 5) {
av_log(avctx, AV_LOG_ERROR, "icc_mode %d is reserved.\n",
ps->icc_mode);
goto err;
}
ps->nr_icc_par = nr_iidicc_par_tab[ps->icc_mode];
}
ps->enable_ext = get_bits1(gb);
}
ps->frame_class = get_bits1(gb);
ps->num_env_old = ps->num_env;
ps->num_env = num_env_tab[ps->frame_class][get_bits(gb, 2)];
ps->border_position[0] = -1;
if (ps->frame_class) {
for (e = 1; e <= ps->num_env; e++)
ps->border_position[e] = get_bits(gb, 5);
} else
for (e = 1; e <= ps->num_env; e++)
ps->border_position[e] = (e * numQMFSlots >> ff_log2_tab[ps->num_env]) - 1;
if (ps->enable_iid) {
for (e = 0; e < ps->num_env; e++) {
int dt = get_bits1(gb);
if (read_iid_data(avctx, gb, ps, ps->iid_par, huff_iid[2*dt+ps->iid_quant], e, dt))
goto err;
}
} else
memset(ps->iid_par, 0, sizeof(ps->iid_par));
if (ps->enable_icc)
for (e = 0; e < ps->num_env; e++) {
int dt = get_bits1(gb);
if (read_icc_data(avctx, gb, ps, ps->icc_par, dt ? huff_icc_dt : huff_icc_df, e, dt))
goto err;
}
else
memset(ps->icc_par, 0, sizeof(ps->icc_par));
if (ps->enable_ext) {
int cnt = get_bits(gb, 4);
if (cnt == 15) {
cnt += get_bits(gb, 8);
}
cnt *= 8;
while (cnt > 7) {
int ps_extension_id = get_bits(gb, 2);
cnt -= 2 + ps_read_extension_data(gb, ps, ps_extension_id);
}
if (cnt < 0) {
av_log(avctx, AV_LOG_ERROR, "ps extension overflow %d", cnt);
goto err;
}
skip_bits(gb, cnt);
}
ps->enable_ipdopd &= !PS_BASELINE;
//Fix up envelopes
if (!ps->num_env || ps->border_position[ps->num_env] < numQMFSlots - 1) {
//Create a fake envelope
int source = ps->num_env ? ps->num_env - 1 : ps->num_env_old - 1;
if (source >= 0 && source != ps->num_env) {
if (ps->enable_iid) {
memcpy(ps->iid_par+ps->num_env, ps->iid_par+source, sizeof(ps->iid_par[0]));
}
if (ps->enable_icc) {
memcpy(ps->icc_par+ps->num_env, ps->icc_par+source, sizeof(ps->icc_par[0]));
}
if (ps->enable_ipdopd) {
memcpy(ps->ipd_par+ps->num_env, ps->ipd_par+source, sizeof(ps->ipd_par[0]));
memcpy(ps->opd_par+ps->num_env, ps->opd_par+source, sizeof(ps->opd_par[0]));
}
}
ps->num_env++;
ps->border_position[ps->num_env] = numQMFSlots - 1;
}
ps->is34bands_old = ps->is34bands;
if (!PS_BASELINE && (ps->enable_iid || ps->enable_icc))
ps->is34bands = (ps->enable_iid && ps->nr_iid_par == 34) ||
(ps->enable_icc && ps->nr_icc_par == 34);
//Baseline
if (!ps->enable_ipdopd) {
memset(ps->ipd_par, 0, sizeof(ps->ipd_par));
memset(ps->opd_par, 0, sizeof(ps->opd_par));
}
if (header)
ps->start = 1;
bits_consumed = get_bits_count(gb) - bit_count_start;
if (bits_consumed <= bits_left) {
skip_bits_long(gb_host, bits_consumed);
return bits_consumed;
}
av_log(avctx, AV_LOG_ERROR, "Expected to read %d PS bits actually read %d.\n", bits_left, bits_consumed);
err:
ps->start = 0;
skip_bits_long(gb_host, bits_left);
return bits_left;
}
/** Split one subband into 2 subsubbands with a symmetric real filter.
* The filter must have its non-center even coefficients equal to zero. */
static void hybrid2_re(float (*in)[2], float (*out)[32][2], const float filter[7], int len, int reverse)
{
int i, j;
for (i = 0; i < len; i++, in++) {
float re_in = filter[6] * in[6][0]; //real inphase
float re_op = 0.0f; //real out of phase
float im_in = filter[6] * in[6][1]; //imag inphase
float im_op = 0.0f; //imag out of phase
for (j = 0; j < 6; j += 2) {
re_op += filter[j+1] * (in[j+1][0] + in[12-j-1][0]);
im_op += filter[j+1] * (in[j+1][1] + in[12-j-1][1]);
}
out[ reverse][i][0] = re_in + re_op;
out[ reverse][i][1] = im_in + im_op;
out[!reverse][i][0] = re_in - re_op;
out[!reverse][i][1] = im_in - im_op;
}
}
/** Split one subband into 6 subsubbands with a complex filter */
static void hybrid6_cx(float (*in)[2], float (*out)[32][2], const float (*filter)[7][2], int len)
{
int i, j, ssb;
int N = 8;
float temp[8][2];
for (i = 0; i < len; i++, in++) {
for (ssb = 0; ssb < N; ssb++) {
float sum_re = filter[ssb][6][0] * in[6][0], sum_im = filter[ssb][6][0] * in[6][1];
for (j = 0; j < 6; j++) {
float in0_re = in[j][0];
float in0_im = in[j][1];
float in1_re = in[12-j][0];
float in1_im = in[12-j][1];
sum_re += filter[ssb][j][0] * (in0_re + in1_re) - filter[ssb][j][1] * (in0_im - in1_im);
sum_im += filter[ssb][j][0] * (in0_im + in1_im) + filter[ssb][j][1] * (in0_re - in1_re);
}
temp[ssb][0] = sum_re;
temp[ssb][1] = sum_im;
}
out[0][i][0] = temp[6][0];
out[0][i][1] = temp[6][1];
out[1][i][0] = temp[7][0];
out[1][i][1] = temp[7][1];
out[2][i][0] = temp[0][0];
out[2][i][1] = temp[0][1];
out[3][i][0] = temp[1][0];
out[3][i][1] = temp[1][1];
out[4][i][0] = temp[2][0] + temp[5][0];
out[4][i][1] = temp[2][1] + temp[5][1];
out[5][i][0] = temp[3][0] + temp[4][0];
out[5][i][1] = temp[3][1] + temp[4][1];
}
}
static void hybrid4_8_12_cx(float (*in)[2], float (*out)[32][2], const float (*filter)[7][2], int N, int len)
{
int i, j, ssb;
for (i = 0; i < len; i++, in++) {
for (ssb = 0; ssb < N; ssb++) {
float sum_re = filter[ssb][6][0] * in[6][0], sum_im = filter[ssb][6][0] * in[6][1];
for (j = 0; j < 6; j++) {
float in0_re = in[j][0];
float in0_im = in[j][1];
float in1_re = in[12-j][0];
float in1_im = in[12-j][1];
sum_re += filter[ssb][j][0] * (in0_re + in1_re) - filter[ssb][j][1] * (in0_im - in1_im);
sum_im += filter[ssb][j][0] * (in0_im + in1_im) + filter[ssb][j][1] * (in0_re - in1_re);
}
out[ssb][i][0] = sum_re;
out[ssb][i][1] = sum_im;
}
}
}
static void hybrid_analysis(float out[91][32][2], float in[5][44][2], float L[2][38][64], int is34, int len)
{
int i, j;
for (i = 0; i < 5; i++) {
for (j = 0; j < 38; j++) {
in[i][j+6][0] = L[0][j][i];
in[i][j+6][1] = L[1][j][i];
}
}
if (is34) {
hybrid4_8_12_cx(in[0], out, f34_0_12, 12, len);
hybrid4_8_12_cx(in[1], out+12, f34_1_8, 8, len);
hybrid4_8_12_cx(in[2], out+20, f34_2_4, 4, len);
hybrid4_8_12_cx(in[3], out+24, f34_2_4, 4, len);
hybrid4_8_12_cx(in[4], out+28, f34_2_4, 4, len);
for (i = 0; i < 59; i++) {
for (j = 0; j < len; j++) {
out[i+32][j][0] = L[0][j][i+5];
out[i+32][j][1] = L[1][j][i+5];
}
}
} else {
hybrid6_cx(in[0], out, f20_0_8, len);
hybrid2_re(in[1], out+6, g1_Q2, len, 1);
hybrid2_re(in[2], out+8, g1_Q2, len, 0);
for (i = 0; i < 61; i++) {
for (j = 0; j < len; j++) {
out[i+10][j][0] = L[0][j][i+3];
out[i+10][j][1] = L[1][j][i+3];
}
}
}
//update in_buf
for (i = 0; i < 5; i++) {
memcpy(in[i], in[i]+32, 6 * sizeof(in[i][0]));
}
}
static void hybrid_synthesis(float out[2][38][64], float in[91][32][2], int is34, int len)
{
int i, n;
if (is34) {
for (n = 0; n < len; n++) {
memset(out[0][n], 0, 5*sizeof(out[0][n][0]));
memset(out[1][n], 0, 5*sizeof(out[1][n][0]));
for (i = 0; i < 12; i++) {
out[0][n][0] += in[ i][n][0];
out[1][n][0] += in[ i][n][1];
}
for (i = 0; i < 8; i++) {
out[0][n][1] += in[12+i][n][0];
out[1][n][1] += in[12+i][n][1];
}
for (i = 0; i < 4; i++) {
out[0][n][2] += in[20+i][n][0];
out[1][n][2] += in[20+i][n][1];
out[0][n][3] += in[24+i][n][0];
out[1][n][3] += in[24+i][n][1];
out[0][n][4] += in[28+i][n][0];
out[1][n][4] += in[28+i][n][1];
}
}
for (i = 0; i < 59; i++) {
for (n = 0; n < len; n++) {
out[0][n][i+5] = in[i+32][n][0];
out[1][n][i+5] = in[i+32][n][1];
}
}
} else {
for (n = 0; n < len; n++) {
out[0][n][0] = in[0][n][0] + in[1][n][0] + in[2][n][0] +
in[3][n][0] + in[4][n][0] + in[5][n][0];
out[1][n][0] = in[0][n][1] + in[1][n][1] + in[2][n][1] +
in[3][n][1] + in[4][n][1] + in[5][n][1];
out[0][n][1] = in[6][n][0] + in[7][n][0];
out[1][n][1] = in[6][n][1] + in[7][n][1];
out[0][n][2] = in[8][n][0] + in[9][n][0];
out[1][n][2] = in[8][n][1] + in[9][n][1];
}
for (i = 0; i < 61; i++) {
for (n = 0; n < len; n++) {
out[0][n][i+3] = in[i+10][n][0];
out[1][n][i+3] = in[i+10][n][1];
}
}
}
}
/// All-pass filter decay slope
#define DECAY_SLOPE 0.05f
/// Number of frequency bands that can be addressed by the parameter index, b(k)
static const int NR_PAR_BANDS[] = { 20, 34 };
/// Number of frequency bands that can be addressed by the sub subband index, k
static const int NR_BANDS[] = { 71, 91 };
/// Start frequency band for the all-pass filter decay slope
static const int DECAY_CUTOFF[] = { 10, 32 };
/// Number of all-pass filer bands
static const int NR_ALLPASS_BANDS[] = { 30, 50 };
/// First stereo band using the short one sample delay
static const int SHORT_DELAY_BAND[] = { 42, 62 };
/** Table 8.46 */
static void map_idx_10_to_20(int8_t *par_mapped, const int8_t *par, int full)
{
int b;
if (full)
b = 9;
else {
b = 4;
par_mapped[10] = 0;
}
for (; b >= 0; b--) {
par_mapped[2*b+1] = par_mapped[2*b] = par[b];
}
}
static void map_idx_34_to_20(int8_t *par_mapped, const int8_t *par, int full)
{
par_mapped[ 0] = (2*par[ 0] + par[ 1]) / 3;
par_mapped[ 1] = ( par[ 1] + 2*par[ 2]) / 3;
par_mapped[ 2] = (2*par[ 3] + par[ 4]) / 3;
par_mapped[ 3] = ( par[ 4] + 2*par[ 5]) / 3;
par_mapped[ 4] = ( par[ 6] + par[ 7]) / 2;
par_mapped[ 5] = ( par[ 8] + par[ 9]) / 2;
par_mapped[ 6] = par[10];
par_mapped[ 7] = par[11];
par_mapped[ 8] = ( par[12] + par[13]) / 2;
par_mapped[ 9] = ( par[14] + par[15]) / 2;
par_mapped[10] = par[16];
if (full) {
par_mapped[11] = par[17];
par_mapped[12] = par[18];
par_mapped[13] = par[19];
par_mapped[14] = ( par[20] + par[21]) / 2;
par_mapped[15] = ( par[22] + par[23]) / 2;
par_mapped[16] = ( par[24] + par[25]) / 2;
par_mapped[17] = ( par[26] + par[27]) / 2;
par_mapped[18] = ( par[28] + par[29] + par[30] + par[31]) / 4;
par_mapped[19] = ( par[32] + par[33]) / 2;
}
}
static void map_val_34_to_20(float par[PS_MAX_NR_IIDICC])
{
par[ 0] = (2*par[ 0] + par[ 1]) * 0.33333333f;
par[ 1] = ( par[ 1] + 2*par[ 2]) * 0.33333333f;
par[ 2] = (2*par[ 3] + par[ 4]) * 0.33333333f;
par[ 3] = ( par[ 4] + 2*par[ 5]) * 0.33333333f;
par[ 4] = ( par[ 6] + par[ 7]) * 0.5f;
par[ 5] = ( par[ 8] + par[ 9]) * 0.5f;
par[ 6] = par[10];
par[ 7] = par[11];
par[ 8] = ( par[12] + par[13]) * 0.5f;
par[ 9] = ( par[14] + par[15]) * 0.5f;
par[10] = par[16];
par[11] = par[17];
par[12] = par[18];
par[13] = par[19];
par[14] = ( par[20] + par[21]) * 0.5f;
par[15] = ( par[22] + par[23]) * 0.5f;
par[16] = ( par[24] + par[25]) * 0.5f;
par[17] = ( par[26] + par[27]) * 0.5f;
par[18] = ( par[28] + par[29] + par[30] + par[31]) * 0.25f;
par[19] = ( par[32] + par[33]) * 0.5f;
}
static void map_idx_10_to_34(int8_t *par_mapped, const int8_t *par, int full)
{
if (full) {
par_mapped[33] = par[9];
par_mapped[32] = par[9];
par_mapped[31] = par[9];
par_mapped[30] = par[9];
par_mapped[29] = par[9];
par_mapped[28] = par[9];
par_mapped[27] = par[8];
par_mapped[26] = par[8];
par_mapped[25] = par[8];
par_mapped[24] = par[8];
par_mapped[23] = par[7];
par_mapped[22] = par[7];
par_mapped[21] = par[7];
par_mapped[20] = par[7];
par_mapped[19] = par[6];
par_mapped[18] = par[6];
par_mapped[17] = par[5];
par_mapped[16] = par[5];
} else {
par_mapped[16] = 0;
}
par_mapped[15] = par[4];
par_mapped[14] = par[4];
par_mapped[13] = par[4];
par_mapped[12] = par[4];
par_mapped[11] = par[3];
par_mapped[10] = par[3];
par_mapped[ 9] = par[2];
par_mapped[ 8] = par[2];
par_mapped[ 7] = par[2];
par_mapped[ 6] = par[2];
par_mapped[ 5] = par[1];
par_mapped[ 4] = par[1];
par_mapped[ 3] = par[1];
par_mapped[ 2] = par[0];
par_mapped[ 1] = par[0];
par_mapped[ 0] = par[0];
}
static void map_idx_20_to_34(int8_t *par_mapped, const int8_t *par, int full)
{
if (full) {
par_mapped[33] = par[19];
par_mapped[32] = par[19];
par_mapped[31] = par[18];
par_mapped[30] = par[18];
par_mapped[29] = par[18];
par_mapped[28] = par[18];
par_mapped[27] = par[17];
par_mapped[26] = par[17];
par_mapped[25] = par[16];
par_mapped[24] = par[16];
par_mapped[23] = par[15];
par_mapped[22] = par[15];
par_mapped[21] = par[14];
par_mapped[20] = par[14];
par_mapped[19] = par[13];
par_mapped[18] = par[12];
par_mapped[17] = par[11];
}
par_mapped[16] = par[10];
par_mapped[15] = par[ 9];
par_mapped[14] = par[ 9];
par_mapped[13] = par[ 8];
par_mapped[12] = par[ 8];
par_mapped[11] = par[ 7];
par_mapped[10] = par[ 6];
par_mapped[ 9] = par[ 5];
par_mapped[ 8] = par[ 5];
par_mapped[ 7] = par[ 4];
par_mapped[ 6] = par[ 4];
par_mapped[ 5] = par[ 3];
par_mapped[ 4] = (par[ 2] + par[ 3]) / 2;
par_mapped[ 3] = par[ 2];
par_mapped[ 2] = par[ 1];
par_mapped[ 1] = (par[ 0] + par[ 1]) / 2;
par_mapped[ 0] = par[ 0];
}
static void map_val_20_to_34(float par[PS_MAX_NR_IIDICC])
{
par[33] = par[19];
par[32] = par[19];
par[31] = par[18];
par[30] = par[18];
par[29] = par[18];
par[28] = par[18];
par[27] = par[17];
par[26] = par[17];
par[25] = par[16];
par[24] = par[16];
par[23] = par[15];
par[22] = par[15];
par[21] = par[14];
par[20] = par[14];
par[19] = par[13];
par[18] = par[12];
par[17] = par[11];
par[16] = par[10];
par[15] = par[ 9];
par[14] = par[ 9];
par[13] = par[ 8];
par[12] = par[ 8];
par[11] = par[ 7];
par[10] = par[ 6];
par[ 9] = par[ 5];
par[ 8] = par[ 5];
par[ 7] = par[ 4];
par[ 6] = par[ 4];
par[ 5] = par[ 3];
par[ 4] = (par[ 2] + par[ 3]) * 0.5f;
par[ 3] = par[ 2];
par[ 2] = par[ 1];
par[ 1] = (par[ 0] + par[ 1]) * 0.5f;
par[ 0] = par[ 0];
}
static void decorrelation(PSContext *ps, float (*out)[32][2], const float (*s)[32][2], int is34)
{
float power[34][PS_QMF_TIME_SLOTS] = {{0}};
float transient_gain[34][PS_QMF_TIME_SLOTS];
float *peak_decay_nrg = ps->peak_decay_nrg;
float *power_smooth = ps->power_smooth;
float *peak_decay_diff_smooth = ps->peak_decay_diff_smooth;
float (*delay)[PS_QMF_TIME_SLOTS + PS_MAX_DELAY][2] = ps->delay;
float (*ap_delay)[PS_AP_LINKS][PS_QMF_TIME_SLOTS + PS_MAX_AP_DELAY][2] = ps->ap_delay;
const int8_t *k_to_i = is34 ? k_to_i_34 : k_to_i_20;
const float peak_decay_factor = 0.76592833836465f;
const float transient_impact = 1.5f;
const float a_smooth = 0.25f; //< Smoothing coefficient
int i, k, m, n;
int n0 = 0, nL = 32;
static const int link_delay[] = { 3, 4, 5 };
static const float a[] = { 0.65143905753106f,
0.56471812200776f,
0.48954165955695f };
if (is34 != ps->is34bands_old) {
memset(ps->peak_decay_nrg, 0, sizeof(ps->peak_decay_nrg));
memset(ps->power_smooth, 0, sizeof(ps->power_smooth));
memset(ps->peak_decay_diff_smooth, 0, sizeof(ps->peak_decay_diff_smooth));
memset(ps->delay, 0, sizeof(ps->delay));
memset(ps->ap_delay, 0, sizeof(ps->ap_delay));
}
for (n = n0; n < nL; n++) {
for (k = 0; k < NR_BANDS[is34]; k++) {
int i = k_to_i[k];
power[i][n] += s[k][n][0] * s[k][n][0] + s[k][n][1] * s[k][n][1];
}
}
//Transient detection
for (i = 0; i < NR_PAR_BANDS[is34]; i++) {
for (n = n0; n < nL; n++) {
float decayed_peak = peak_decay_factor * peak_decay_nrg[i];
float denom;
peak_decay_nrg[i] = FFMAX(decayed_peak, power[i][n]);
power_smooth[i] += a_smooth * (power[i][n] - power_smooth[i]);
peak_decay_diff_smooth[i] += a_smooth * (peak_decay_nrg[i] - power[i][n] - peak_decay_diff_smooth[i]);
denom = transient_impact * peak_decay_diff_smooth[i];
transient_gain[i][n] = (denom > power_smooth[i]) ?
power_smooth[i] / denom : 1.0f;
}
}
//Decorrelation and transient reduction
// PS_AP_LINKS - 1
// -----
// | | Q_fract_allpass[k][m]*z^-link_delay[m] - a[m]*g_decay_slope[k]
//H[k][z] = z^-2 * phi_fract[k] * | | ----------------------------------------------------------------
// | | 1 - a[m]*g_decay_slope[k]*Q_fract_allpass[k][m]*z^-link_delay[m]
// m = 0
//d[k][z] (out) = transient_gain_mapped[k][z] * H[k][z] * s[k][z]
for (k = 0; k < NR_ALLPASS_BANDS[is34]; k++) {
int b = k_to_i[k];
float g_decay_slope = 1.f - DECAY_SLOPE * (k - DECAY_CUTOFF[is34]);
float ag[PS_AP_LINKS];
g_decay_slope = av_clipf(g_decay_slope, 0.f, 1.f);
memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0]));
memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0]));
for (m = 0; m < PS_AP_LINKS; m++) {
memcpy(ap_delay[k][m], ap_delay[k][m]+numQMFSlots, 5*sizeof(ap_delay[k][m][0]));
ag[m] = a[m] * g_decay_slope;
}
for (n = n0; n < nL; n++) {
float in_re = delay[k][n+PS_MAX_DELAY-2][0] * phi_fract[is34][k][0] -
delay[k][n+PS_MAX_DELAY-2][1] * phi_fract[is34][k][1];
float in_im = delay[k][n+PS_MAX_DELAY-2][0] * phi_fract[is34][k][1] +
delay[k][n+PS_MAX_DELAY-2][1] * phi_fract[is34][k][0];
for (m = 0; m < PS_AP_LINKS; m++) {
float a_re = ag[m] * in_re;
float a_im = ag[m] * in_im;
float link_delay_re = ap_delay[k][m][n+5-link_delay[m]][0];
float link_delay_im = ap_delay[k][m][n+5-link_delay[m]][1];
float fractional_delay_re = Q_fract_allpass[is34][k][m][0];
float fractional_delay_im = Q_fract_allpass[is34][k][m][1];
ap_delay[k][m][n+5][0] = in_re;
ap_delay[k][m][n+5][1] = in_im;
in_re = link_delay_re * fractional_delay_re - link_delay_im * fractional_delay_im - a_re;
in_im = link_delay_re * fractional_delay_im + link_delay_im * fractional_delay_re - a_im;
ap_delay[k][m][n+5][0] += ag[m] * in_re;
ap_delay[k][m][n+5][1] += ag[m] * in_im;
}
out[k][n][0] = transient_gain[b][n] * in_re;
out[k][n][1] = transient_gain[b][n] * in_im;
}
}
for (; k < SHORT_DELAY_BAND[is34]; k++) {
memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0]));
memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0]));
for (n = n0; n < nL; n++) {
//H = delay 14
out[k][n][0] = transient_gain[k_to_i[k]][n] * delay[k][n+PS_MAX_DELAY-14][0];
out[k][n][1] = transient_gain[k_to_i[k]][n] * delay[k][n+PS_MAX_DELAY-14][1];
}
}
for (; k < NR_BANDS[is34]; k++) {
memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0]));
memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0]));
for (n = n0; n < nL; n++) {
//H = delay 1
out[k][n][0] = transient_gain[k_to_i[k]][n] * delay[k][n+PS_MAX_DELAY-1][0];
out[k][n][1] = transient_gain[k_to_i[k]][n] * delay[k][n+PS_MAX_DELAY-1][1];
}
}
}
static void remap34(int8_t (**p_par_mapped)[PS_MAX_NR_IIDICC],
int8_t (*par)[PS_MAX_NR_IIDICC],
int num_par, int num_env, int full)
{
int8_t (*par_mapped)[PS_MAX_NR_IIDICC] = *p_par_mapped;
int e;
if (num_par == 20 || num_par == 11) {
for (e = 0; e < num_env; e++) {
map_idx_20_to_34(par_mapped[e], par[e], full);
}
} else if (num_par == 10 || num_par == 5) {
for (e = 0; e < num_env; e++) {
map_idx_10_to_34(par_mapped[e], par[e], full);
}
} else {
*p_par_mapped = par;
}
}
static void remap20(int8_t (**p_par_mapped)[PS_MAX_NR_IIDICC],
int8_t (*par)[PS_MAX_NR_IIDICC],
int num_par, int num_env, int full)
{
int8_t (*par_mapped)[PS_MAX_NR_IIDICC] = *p_par_mapped;
int e;
if (num_par == 34 || num_par == 17) {
for (e = 0; e < num_env; e++) {
map_idx_34_to_20(par_mapped[e], par[e], full);
}
} else if (num_par == 10 || num_par == 5) {
for (e = 0; e < num_env; e++) {
map_idx_10_to_20(par_mapped[e], par[e], full);
}
} else {
*p_par_mapped = par;
}
}
static void stereo_processing(PSContext *ps, float (*l)[32][2], float (*r)[32][2], int is34)
{
int e, b, k, n;
float (*H11)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H11;
float (*H12)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H12;
float (*H21)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H21;
float (*H22)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H22;
int8_t *opd_hist = ps->opd_hist;
int8_t *ipd_hist = ps->ipd_hist;
int8_t iid_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
int8_t icc_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
int8_t ipd_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
int8_t opd_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
int8_t (*iid_mapped)[PS_MAX_NR_IIDICC] = iid_mapped_buf;
int8_t (*icc_mapped)[PS_MAX_NR_IIDICC] = icc_mapped_buf;
int8_t (*ipd_mapped)[PS_MAX_NR_IIDICC] = ipd_mapped_buf;
int8_t (*opd_mapped)[PS_MAX_NR_IIDICC] = opd_mapped_buf;
const int8_t *k_to_i = is34 ? k_to_i_34 : k_to_i_20;
const float (*H_LUT)[8][4] = (PS_BASELINE || ps->icc_mode < 3) ? HA : HB;
//Remapping
memcpy(H11[0][0], H11[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H11[0][0][0]));
memcpy(H11[1][0], H11[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H11[1][0][0]));
memcpy(H12[0][0], H12[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H12[0][0][0]));
memcpy(H12[1][0], H12[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H12[1][0][0]));
memcpy(H21[0][0], H21[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H21[0][0][0]));
memcpy(H21[1][0], H21[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H21[1][0][0]));
memcpy(H22[0][0], H22[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H22[0][0][0]));
memcpy(H22[1][0], H22[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H22[1][0][0]));
if (is34) {
remap34(&iid_mapped, ps->iid_par, ps->nr_iid_par, ps->num_env, 1);
remap34(&icc_mapped, ps->icc_par, ps->nr_icc_par, ps->num_env, 1);
if (ps->enable_ipdopd) {
remap34(&ipd_mapped, ps->ipd_par, ps->nr_ipdopd_par, ps->num_env, 0);
remap34(&opd_mapped, ps->opd_par, ps->nr_ipdopd_par, ps->num_env, 0);
}
if (!ps->is34bands_old) {
map_val_20_to_34(H11[0][0]);
map_val_20_to_34(H11[1][0]);
map_val_20_to_34(H12[0][0]);
map_val_20_to_34(H12[1][0]);
map_val_20_to_34(H21[0][0]);
map_val_20_to_34(H21[1][0]);
map_val_20_to_34(H22[0][0]);
map_val_20_to_34(H22[1][0]);
ipdopd_reset(ipd_hist, opd_hist);
}
} else {
remap20(&iid_mapped, ps->iid_par, ps->nr_iid_par, ps->num_env, 1);
remap20(&icc_mapped, ps->icc_par, ps->nr_icc_par, ps->num_env, 1);
if (ps->enable_ipdopd) {
remap20(&ipd_mapped, ps->ipd_par, ps->nr_ipdopd_par, ps->num_env, 0);
remap20(&opd_mapped, ps->opd_par, ps->nr_ipdopd_par, ps->num_env, 0);
}
if (ps->is34bands_old) {
map_val_34_to_20(H11[0][0]);
map_val_34_to_20(H11[1][0]);
map_val_34_to_20(H12[0][0]);
map_val_34_to_20(H12[1][0]);
map_val_34_to_20(H21[0][0]);
map_val_34_to_20(H21[1][0]);
map_val_34_to_20(H22[0][0]);
map_val_34_to_20(H22[1][0]);
ipdopd_reset(ipd_hist, opd_hist);
}
}
//Mixing
for (e = 0; e < ps->num_env; e++) {
for (b = 0; b < NR_PAR_BANDS[is34]; b++) {
float h11, h12, h21, h22;
h11 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][0];
h12 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][1];
h21 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][2];
h22 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][3];
if (!PS_BASELINE && ps->enable_ipdopd && b < ps->nr_ipdopd_par) {
//The spec say says to only run this smoother when enable_ipdopd
//is set but the reference decoder appears to run it constantly
float h11i, h12i, h21i, h22i;
float ipd_adj_re, ipd_adj_im;
int opd_idx = opd_hist[b] * 8 + opd_mapped[e][b];
int ipd_idx = ipd_hist[b] * 8 + ipd_mapped[e][b];
float opd_re = pd_re_smooth[opd_idx];
float opd_im = pd_im_smooth[opd_idx];
float ipd_re = pd_re_smooth[ipd_idx];
float ipd_im = pd_im_smooth[ipd_idx];
opd_hist[b] = opd_idx & 0x3F;
ipd_hist[b] = ipd_idx & 0x3F;
ipd_adj_re = opd_re*ipd_re + opd_im*ipd_im;
ipd_adj_im = opd_im*ipd_re - opd_re*ipd_im;
h11i = h11 * opd_im;
h11 = h11 * opd_re;
h12i = h12 * ipd_adj_im;
h12 = h12 * ipd_adj_re;
h21i = h21 * opd_im;
h21 = h21 * opd_re;
h22i = h22 * ipd_adj_im;
h22 = h22 * ipd_adj_re;
H11[1][e+1][b] = h11i;
H12[1][e+1][b] = h12i;
H21[1][e+1][b] = h21i;
H22[1][e+1][b] = h22i;
}
H11[0][e+1][b] = h11;
H12[0][e+1][b] = h12;
H21[0][e+1][b] = h21;
H22[0][e+1][b] = h22;
}
for (k = 0; k < NR_BANDS[is34]; k++) {
float h11r, h12r, h21r, h22r;
float h11i, h12i, h21i, h22i;
float h11r_step, h12r_step, h21r_step, h22r_step;
float h11i_step, h12i_step, h21i_step, h22i_step;
int start = ps->border_position[e];
int stop = ps->border_position[e+1];
float width = 1.f / (stop - start);
b = k_to_i[k];
h11r = H11[0][e][b];
h12r = H12[0][e][b];
h21r = H21[0][e][b];
h22r = H22[0][e][b];
if (!PS_BASELINE && ps->enable_ipdopd) {
//Is this necessary? ps_04_new seems unchanged
if ((is34 && k <= 13 && k >= 9) || (!is34 && k <= 1)) {
h11i = -H11[1][e][b];
h12i = -H12[1][e][b];
h21i = -H21[1][e][b];
h22i = -H22[1][e][b];
} else {
h11i = H11[1][e][b];
h12i = H12[1][e][b];
h21i = H21[1][e][b];
h22i = H22[1][e][b];
}
}
//Interpolation
h11r_step = (H11[0][e+1][b] - h11r) * width;
h12r_step = (H12[0][e+1][b] - h12r) * width;
h21r_step = (H21[0][e+1][b] - h21r) * width;
h22r_step = (H22[0][e+1][b] - h22r) * width;
if (!PS_BASELINE && ps->enable_ipdopd) {
h11i_step = (H11[1][e+1][b] - h11i) * width;
h12i_step = (H12[1][e+1][b] - h12i) * width;
h21i_step = (H21[1][e+1][b] - h21i) * width;
h22i_step = (H22[1][e+1][b] - h22i) * width;
}
for (n = start + 1; n <= stop; n++) {
//l is s, r is d
float l_re = l[k][n][0];
float l_im = l[k][n][1];
float r_re = r[k][n][0];
float r_im = r[k][n][1];
h11r += h11r_step;
h12r += h12r_step;
h21r += h21r_step;
h22r += h22r_step;
if (!PS_BASELINE && ps->enable_ipdopd) {
h11i += h11i_step;
h12i += h12i_step;
h21i += h21i_step;
h22i += h22i_step;
l[k][n][0] = h11r*l_re + h21r*r_re - h11i*l_im - h21i*r_im;
l[k][n][1] = h11r*l_im + h21r*r_im + h11i*l_re + h21i*r_re;
r[k][n][0] = h12r*l_re + h22r*r_re - h12i*l_im - h22i*r_im;
r[k][n][1] = h12r*l_im + h22r*r_im + h12i*l_re + h22i*r_re;
} else {
l[k][n][0] = h11r*l_re + h21r*r_re;
l[k][n][1] = h11r*l_im + h21r*r_im;
r[k][n][0] = h12r*l_re + h22r*r_re;
r[k][n][1] = h12r*l_im + h22r*r_im;
}
}
}
}
}
int ff_ps_apply(AVCodecContext *avctx, PSContext *ps, float L[2][38][64], float R[2][38][64], int top)
{
float Lbuf[91][32][2];
float Rbuf[91][32][2];
const int len = 32;
int is34 = ps->is34bands;
top += NR_BANDS[is34] - 64;
memset(ps->delay+top, 0, (NR_BANDS[is34] - top)*sizeof(ps->delay[0]));
if (top < NR_ALLPASS_BANDS[is34])
memset(ps->ap_delay + top, 0, (NR_ALLPASS_BANDS[is34] - top)*sizeof(ps->ap_delay[0]));
hybrid_analysis(Lbuf, ps->in_buf, L, is34, len);
decorrelation(ps, Rbuf, Lbuf, is34);
stereo_processing(ps, Lbuf, Rbuf, is34);
hybrid_synthesis(L, Lbuf, is34, len);
hybrid_synthesis(R, Rbuf, is34, len);
return 0;
}
#define PS_INIT_VLC_STATIC(num, size) \
INIT_VLC_STATIC(&vlc_ps[num], 9, ps_tmp[num].table_size / ps_tmp[num].elem_size, \
ps_tmp[num].ps_bits, 1, 1, \
ps_tmp[num].ps_codes, ps_tmp[num].elem_size, ps_tmp[num].elem_size, \
size);
#define PS_VLC_ROW(name) \
{ name ## _codes, name ## _bits, sizeof(name ## _codes), sizeof(name ## _codes[0]) }
av_cold void ff_ps_init(void) {
// Syntax initialization
static const struct {
const void *ps_codes, *ps_bits;
const unsigned int table_size, elem_size;
} ps_tmp[] = {
PS_VLC_ROW(huff_iid_df1),
PS_VLC_ROW(huff_iid_dt1),
PS_VLC_ROW(huff_iid_df0),
PS_VLC_ROW(huff_iid_dt0),
PS_VLC_ROW(huff_icc_df),
PS_VLC_ROW(huff_icc_dt),
PS_VLC_ROW(huff_ipd_df),
PS_VLC_ROW(huff_ipd_dt),
PS_VLC_ROW(huff_opd_df),
PS_VLC_ROW(huff_opd_dt),
};
PS_INIT_VLC_STATIC(0, 1544);
PS_INIT_VLC_STATIC(1, 832);
PS_INIT_VLC_STATIC(2, 1024);
PS_INIT_VLC_STATIC(3, 1036);
PS_INIT_VLC_STATIC(4, 544);
PS_INIT_VLC_STATIC(5, 544);
PS_INIT_VLC_STATIC(6, 512);
PS_INIT_VLC_STATIC(7, 512);
PS_INIT_VLC_STATIC(8, 512);
PS_INIT_VLC_STATIC(9, 512);
ps_tableinit();
}
av_cold void ff_ps_ctx_init(PSContext *ps)
{
}
| 123linslouis-android-video-cutter | jni/libavcodec/aacps.c | C | asf20 | 38,891 |
/*
* MPEG1/2 tables
* copyright (c) 2000,2001 Fabrice Bellard
* copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* MPEG1/2 tables.
*/
#ifndef AVCODEC_MPEG12DATA_H
#define AVCODEC_MPEG12DATA_H
#include <stdint.h>
#include "libavutil/rational.h"
#include "rl.h"
extern const uint16_t ff_mpeg1_default_intra_matrix[64];
extern const uint16_t ff_mpeg1_default_non_intra_matrix[64];
extern const uint16_t ff_mpeg12_vlc_dc_lum_code[12];
extern const unsigned char ff_mpeg12_vlc_dc_lum_bits[12];
extern const uint16_t ff_mpeg12_vlc_dc_chroma_code[12];
extern const unsigned char ff_mpeg12_vlc_dc_chroma_bits[12];
extern RLTable ff_rl_mpeg1;
extern RLTable ff_rl_mpeg2;
extern const uint8_t ff_mpeg12_mbAddrIncrTable[36][2];
extern const uint8_t ff_mpeg12_mbPatTable[64][2];
extern const uint8_t ff_mpeg12_mbMotionVectorTable[17][2];
extern const AVRational ff_frame_rate_tab[];
extern const float ff_mpeg1_aspect[16];
extern const AVRational ff_mpeg2_aspect[16];
#endif /* AVCODEC_MPEG12DATA_H */
| 123linslouis-android-video-cutter | jni/libavcodec/mpeg12data.h | C | asf20 | 1,796 |
/*
* Indeo Video Interactive v5 compatible decoder
* Copyright (c) 2009 Maxim Poliakovski
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* Indeo Video Interactive version 5 decoder
*
* Indeo5 data is usually transported within .avi or .mov files.
* Known FOURCCs: 'IV50'
*/
#define ALT_BITSTREAM_READER_LE
#include "avcodec.h"
#include "get_bits.h"
#include "dsputil.h"
#include "ivi_dsp.h"
#include "ivi_common.h"
#include "indeo5data.h"
/**
* Indeo5 frame types.
*/
enum {
FRAMETYPE_INTRA = 0,
FRAMETYPE_INTER = 1, ///< non-droppable P-frame
FRAMETYPE_INTER_SCAL = 2, ///< droppable P-frame used in the scalability mode
FRAMETYPE_INTER_NOREF = 3, ///< droppable P-frame
FRAMETYPE_NULL = 4 ///< empty frame with no data
};
#define IVI5_PIC_SIZE_ESC 15
#define IVI5_IS_PROTECTED 0x20
typedef struct {
GetBitContext gb;
AVFrame frame;
RVMapDesc rvmap_tabs[9]; ///< local corrected copy of the static rvmap tables
IVIPlaneDesc planes[3]; ///< color planes
const uint8_t *frame_data; ///< input frame data pointer
int buf_switch; ///< used to switch between three buffers
int inter_scal; ///< signals a sequence of scalable inter frames
int dst_buf; ///< buffer index for the currently decoded frame
int ref_buf; ///< inter frame reference buffer index
int ref2_buf; ///< temporal storage for switching buffers
uint32_t frame_size; ///< frame size in bytes
int frame_type;
int prev_frame_type; ///< frame type of the previous frame
int frame_num;
uint32_t pic_hdr_size; ///< picture header size in bytes
uint8_t frame_flags;
uint16_t checksum; ///< frame checksum
IVIHuffTab mb_vlc; ///< vlc table for decoding macroblock data
uint16_t gop_hdr_size;
uint8_t gop_flags;
int is_scalable;
uint32_t lock_word;
IVIPicConfig pic_conf;
} IVI5DecContext;
/**
* Decodes Indeo5 GOP (Group of pictures) header.
* This header is present in key frames only.
* It defines parameters for all frames in a GOP.
*
* @param ctx [in,out] ptr to the decoder context
* @param avctx [in] ptr to the AVCodecContext
* @return result code: 0 = OK, -1 = error
*/
static int decode_gop_header(IVI5DecContext *ctx, AVCodecContext *avctx)
{
int result, i, p, tile_size, pic_size_indx, mb_size, blk_size, blk_size_changed = 0;
IVIBandDesc *band, *band1, *band2;
IVIPicConfig pic_conf;
ctx->gop_flags = get_bits(&ctx->gb, 8);
ctx->gop_hdr_size = (ctx->gop_flags & 1) ? get_bits(&ctx->gb, 16) : 0;
if (ctx->gop_flags & IVI5_IS_PROTECTED)
ctx->lock_word = get_bits_long(&ctx->gb, 32);
tile_size = (ctx->gop_flags & 0x40) ? 64 << get_bits(&ctx->gb, 2) : 0;
if (tile_size > 256) {
av_log(avctx, AV_LOG_ERROR, "Invalid tile size: %d\n", tile_size);
return -1;
}
/* decode number of wavelet bands */
/* num_levels * 3 + 1 */
pic_conf.luma_bands = get_bits(&ctx->gb, 2) * 3 + 1;
pic_conf.chroma_bands = get_bits1(&ctx->gb) * 3 + 1;
ctx->is_scalable = pic_conf.luma_bands != 1 || pic_conf.chroma_bands != 1;
if (ctx->is_scalable && (pic_conf.luma_bands != 4 || pic_conf.chroma_bands != 1)) {
av_log(avctx, AV_LOG_ERROR, "Scalability: unsupported subdivision! Luma bands: %d, chroma bands: %d\n",
pic_conf.luma_bands, pic_conf.chroma_bands);
return -1;
}
pic_size_indx = get_bits(&ctx->gb, 4);
if (pic_size_indx == IVI5_PIC_SIZE_ESC) {
pic_conf.pic_height = get_bits(&ctx->gb, 13);
pic_conf.pic_width = get_bits(&ctx->gb, 13);
} else {
pic_conf.pic_height = ivi5_common_pic_sizes[pic_size_indx * 2 + 1] << 2;
pic_conf.pic_width = ivi5_common_pic_sizes[pic_size_indx * 2 ] << 2;
}
if (ctx->gop_flags & 2) {
av_log(avctx, AV_LOG_ERROR, "YV12 picture format not supported!\n");
return -1;
}
pic_conf.chroma_height = (pic_conf.pic_height + 3) >> 2;
pic_conf.chroma_width = (pic_conf.pic_width + 3) >> 2;
if (!tile_size) {
pic_conf.tile_height = pic_conf.pic_height;
pic_conf.tile_width = pic_conf.pic_width;
} else {
pic_conf.tile_height = pic_conf.tile_width = tile_size;
}
/* check if picture layout was changed and reallocate buffers */
if (ivi_pic_config_cmp(&pic_conf, &ctx->pic_conf)) {
result = ff_ivi_init_planes(ctx->planes, &pic_conf);
if (result) {
av_log(avctx, AV_LOG_ERROR, "Couldn't reallocate color planes!\n");
return -1;
}
ctx->pic_conf = pic_conf;
blk_size_changed = 1; /* force reallocation of the internal structures */
}
for (p = 0; p <= 1; p++) {
for (i = 0; i < (!p ? pic_conf.luma_bands : pic_conf.chroma_bands); i++) {
band = &ctx->planes[p].bands[i];
band->is_halfpel = get_bits1(&ctx->gb);
mb_size = get_bits1(&ctx->gb);
blk_size = 8 >> get_bits1(&ctx->gb);
mb_size = blk_size << !mb_size;
blk_size_changed = mb_size != band->mb_size || blk_size != band->blk_size;
if (blk_size_changed) {
band->mb_size = mb_size;
band->blk_size = blk_size;
}
if (get_bits1(&ctx->gb)) {
av_log(avctx, AV_LOG_ERROR, "Extended transform info encountered!\n");
return -1;
}
/* select transform function and scan pattern according to plane and band number */
switch ((p << 2) + i) {
case 0:
band->inv_transform = ff_ivi_inverse_slant_8x8;
band->dc_transform = ff_ivi_dc_slant_2d;
band->scan = ff_zigzag_direct;
break;
case 1:
band->inv_transform = ff_ivi_row_slant8;
band->dc_transform = ff_ivi_dc_row_slant;
band->scan = ivi5_scans8x8[0];
break;
case 2:
band->inv_transform = ff_ivi_col_slant8;
band->dc_transform = ff_ivi_dc_col_slant;
band->scan = ivi5_scans8x8[1];
break;
case 3:
band->inv_transform = ff_ivi_put_pixels_8x8;
band->dc_transform = ff_ivi_put_dc_pixel_8x8;
band->scan = ivi5_scans8x8[1];
break;
case 4:
band->inv_transform = ff_ivi_inverse_slant_4x4;
band->dc_transform = ff_ivi_dc_slant_2d;
band->scan = ivi5_scan4x4;
break;
}
band->is_2d_trans = band->inv_transform == ff_ivi_inverse_slant_8x8 ||
band->inv_transform == ff_ivi_inverse_slant_4x4;
/* select dequant matrix according to plane and band number */
if (!p) {
band->quant_mat = (pic_conf.luma_bands > 1) ? i+1 : 0;
} else {
band->quant_mat = 5;
}
if (get_bits(&ctx->gb, 2)) {
av_log(avctx, AV_LOG_ERROR, "End marker missing!\n");
return -1;
}
}
}
/* copy chroma parameters into the 2nd chroma plane */
for (i = 0; i < pic_conf.chroma_bands; i++) {
band1 = &ctx->planes[1].bands[i];
band2 = &ctx->planes[2].bands[i];
band2->width = band1->width;
band2->height = band1->height;
band2->mb_size = band1->mb_size;
band2->blk_size = band1->blk_size;
band2->is_halfpel = band1->is_halfpel;
band2->quant_mat = band1->quant_mat;
band2->scan = band1->scan;
band2->inv_transform = band1->inv_transform;
band2->dc_transform = band1->dc_transform;
band2->is_2d_trans = band1->is_2d_trans;
}
/* reallocate internal structures if needed */
if (blk_size_changed) {
result = ff_ivi_init_tiles(ctx->planes, pic_conf.tile_width,
pic_conf.tile_height);
if (result) {
av_log(avctx, AV_LOG_ERROR,
"Couldn't reallocate internal structures!\n");
return -1;
}
}
if (ctx->gop_flags & 8) {
if (get_bits(&ctx->gb, 3)) {
av_log(avctx, AV_LOG_ERROR, "Alignment bits are not zero!\n");
return -1;
}
if (get_bits1(&ctx->gb))
skip_bits_long(&ctx->gb, 24); /* skip transparency fill color */
}
align_get_bits(&ctx->gb);
skip_bits(&ctx->gb, 23); /* FIXME: unknown meaning */
/* skip GOP extension if any */
if (get_bits1(&ctx->gb)) {
do {
i = get_bits(&ctx->gb, 16);
} while (i & 0x8000);
}
align_get_bits(&ctx->gb);
return 0;
}
/**
* Skips a header extension.
*
* @param gb [in,out] the GetBit context
*/
static inline void skip_hdr_extension(GetBitContext *gb)
{
int i, len;
do {
len = get_bits(gb, 8);
for (i = 0; i < len; i++) skip_bits(gb, 8);
} while(len);
}
/**
* Decodes Indeo5 picture header.
*
* @param ctx [in,out] ptr to the decoder context
* @param avctx [in] ptr to the AVCodecContext
* @return result code: 0 = OK, -1 = error
*/
static int decode_pic_hdr(IVI5DecContext *ctx, AVCodecContext *avctx)
{
if (get_bits(&ctx->gb, 5) != 0x1F) {
av_log(avctx, AV_LOG_ERROR, "Invalid picture start code!\n");
return -1;
}
ctx->prev_frame_type = ctx->frame_type;
ctx->frame_type = get_bits(&ctx->gb, 3);
if (ctx->frame_type >= 5) {
av_log(avctx, AV_LOG_ERROR, "Invalid frame type: %d \n", ctx->frame_type);
return -1;
}
ctx->frame_num = get_bits(&ctx->gb, 8);
if (ctx->frame_type == FRAMETYPE_INTRA) {
if (decode_gop_header(ctx, avctx))
return -1;
}
if (ctx->frame_type != FRAMETYPE_NULL) {
ctx->frame_flags = get_bits(&ctx->gb, 8);
ctx->pic_hdr_size = (ctx->frame_flags & 1) ? get_bits_long(&ctx->gb, 24) : 0;
ctx->checksum = (ctx->frame_flags & 0x10) ? get_bits(&ctx->gb, 16) : 0;
/* skip unknown extension if any */
if (ctx->frame_flags & 0x20)
skip_hdr_extension(&ctx->gb); /* XXX: untested */
/* decode macroblock huffman codebook */
if (ff_ivi_dec_huff_desc(&ctx->gb, ctx->frame_flags & 0x40, IVI_MB_HUFF, &ctx->mb_vlc, avctx))
return -1;
skip_bits(&ctx->gb, 3); /* FIXME: unknown meaning! */
}
align_get_bits(&ctx->gb);
return 0;
}
/**
* Decodes Indeo5 band header.
*
* @param ctx [in,out] ptr to the decoder context
* @param band [in,out] ptr to the band descriptor
* @param avctx [in] ptr to the AVCodecContext
* @return result code: 0 = OK, -1 = error
*/
static int decode_band_hdr(IVI5DecContext *ctx, IVIBandDesc *band,
AVCodecContext *avctx)
{
int i;
uint8_t band_flags;
band_flags = get_bits(&ctx->gb, 8);
if (band_flags & 1) {
band->is_empty = 1;
return 0;
}
band->data_size = (ctx->frame_flags & 0x80) ? get_bits_long(&ctx->gb, 24) : 0;
band->inherit_mv = band_flags & 2;
band->inherit_qdelta = band_flags & 8;
band->qdelta_present = band_flags & 4;
if (!band->qdelta_present) band->inherit_qdelta = 1;
/* decode rvmap probability corrections if any */
band->num_corr = 0; /* there are no corrections */
if (band_flags & 0x10) {
band->num_corr = get_bits(&ctx->gb, 8); /* get number of correction pairs */
if (band->num_corr > 61) {
av_log(avctx, AV_LOG_ERROR, "Too many corrections: %d\n",
band->num_corr);
return -1;
}
/* read correction pairs */
for (i = 0; i < band->num_corr * 2; i++)
band->corr[i] = get_bits(&ctx->gb, 8);
}
/* select appropriate rvmap table for this band */
band->rvmap_sel = (band_flags & 0x40) ? get_bits(&ctx->gb, 3) : 8;
/* decode block huffman codebook */
if (ff_ivi_dec_huff_desc(&ctx->gb, band_flags & 0x80, IVI_BLK_HUFF, &band->blk_vlc, avctx))
return -1;
band->checksum_present = get_bits1(&ctx->gb);
if (band->checksum_present)
band->checksum = get_bits(&ctx->gb, 16);
band->glob_quant = get_bits(&ctx->gb, 5);
/* skip unknown extension if any */
if (band_flags & 0x20) { /* XXX: untested */
align_get_bits(&ctx->gb);
skip_hdr_extension(&ctx->gb);
}
align_get_bits(&ctx->gb);
return 0;
}
/**
* Decodes info (block type, cbp, quant delta, motion vector)
* for all macroblocks in the current tile.
*
* @param ctx [in,out] ptr to the decoder context
* @param band [in,out] ptr to the band descriptor
* @param tile [in,out] ptr to the tile descriptor
* @param avctx [in] ptr to the AVCodecContext
* @return result code: 0 = OK, -1 = error
*/
static int decode_mb_info(IVI5DecContext *ctx, IVIBandDesc *band,
IVITile *tile, AVCodecContext *avctx)
{
int x, y, mv_x, mv_y, mv_delta, offs, mb_offset,
mv_scale, blks_per_mb;
IVIMbInfo *mb, *ref_mb;
int row_offset = band->mb_size * band->pitch;
mb = tile->mbs;
ref_mb = tile->ref_mbs;
offs = tile->ypos * band->pitch + tile->xpos;
/* scale factor for motion vectors */
mv_scale = (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3);
mv_x = mv_y = 0;
for (y = tile->ypos; y < (tile->ypos + tile->height); y += band->mb_size) {
mb_offset = offs;
for (x = tile->xpos; x < (tile->xpos + tile->width); x += band->mb_size) {
mb->xpos = x;
mb->ypos = y;
mb->buf_offs = mb_offset;
if (get_bits1(&ctx->gb)) {
if (ctx->frame_type == FRAMETYPE_INTRA) {
av_log(avctx, AV_LOG_ERROR, "Empty macroblock in an INTRA picture!\n");
return -1;
}
mb->type = 1; /* empty macroblocks are always INTER */
mb->cbp = 0; /* all blocks are empty */
mb->q_delta = 0;
if (!band->plane && !band->band_num && (ctx->frame_flags & 8)) {
mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
IVI_VLC_BITS, 1);
mb->q_delta = IVI_TOSIGNED(mb->q_delta);
}
mb->mv_x = mb->mv_y = 0; /* no motion vector coded */
if (band->inherit_mv){
/* motion vector inheritance */
if (mv_scale) {
mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
} else {
mb->mv_x = ref_mb->mv_x;
mb->mv_y = ref_mb->mv_y;
}
}
} else {
if (band->inherit_mv) {
mb->type = ref_mb->type; /* copy mb_type from corresponding reference mb */
} else if (ctx->frame_type == FRAMETYPE_INTRA) {
mb->type = 0; /* mb_type is always INTRA for intra-frames */
} else {
mb->type = get_bits1(&ctx->gb);
}
blks_per_mb = band->mb_size != band->blk_size ? 4 : 1;
mb->cbp = get_bits(&ctx->gb, blks_per_mb);
mb->q_delta = 0;
if (band->qdelta_present) {
if (band->inherit_qdelta) {
if (ref_mb) mb->q_delta = ref_mb->q_delta;
} else if (mb->cbp || (!band->plane && !band->band_num &&
(ctx->frame_flags & 8))) {
mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
IVI_VLC_BITS, 1);
mb->q_delta = IVI_TOSIGNED(mb->q_delta);
}
}
if (!mb->type) {
mb->mv_x = mb->mv_y = 0; /* there is no motion vector in intra-macroblocks */
} else {
if (band->inherit_mv){
/* motion vector inheritance */
if (mv_scale) {
mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
} else {
mb->mv_x = ref_mb->mv_x;
mb->mv_y = ref_mb->mv_y;
}
} else {
/* decode motion vector deltas */
mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
IVI_VLC_BITS, 1);
mv_y += IVI_TOSIGNED(mv_delta);
mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
IVI_VLC_BITS, 1);
mv_x += IVI_TOSIGNED(mv_delta);
mb->mv_x = mv_x;
mb->mv_y = mv_y;
}
}
}
mb++;
if (ref_mb)
ref_mb++;
mb_offset += band->mb_size;
}
offs += row_offset;
}
align_get_bits(&ctx->gb);
return 0;
}
/**
* Decodes an Indeo5 band.
*
* @param ctx [in,out] ptr to the decoder context
* @param band [in,out] ptr to the band descriptor
* @param avctx [in] ptr to the AVCodecContext
* @return result code: 0 = OK, -1 = error
*/
static int decode_band(IVI5DecContext *ctx, int plane_num,
IVIBandDesc *band, AVCodecContext *avctx)
{
int result, i, t, idx1, idx2, pos;
IVITile *tile;
band->buf = band->bufs[ctx->dst_buf];
band->ref_buf = band->bufs[ctx->ref_buf];
band->data_ptr = ctx->frame_data + (get_bits_count(&ctx->gb) >> 3);
result = decode_band_hdr(ctx, band, avctx);
if (result) {
av_log(avctx, AV_LOG_ERROR, "Error while decoding band header: %d\n",
result);
return -1;
}
if (band->is_empty) {
av_log(avctx, AV_LOG_ERROR, "Empty band encountered!\n");
return -1;
}
if (band->blk_size == 8) {
band->intra_base = &ivi5_base_quant_8x8_intra[band->quant_mat][0];
band->inter_base = &ivi5_base_quant_8x8_inter[band->quant_mat][0];
band->intra_scale = &ivi5_scale_quant_8x8_intra[band->quant_mat][0];
band->inter_scale = &ivi5_scale_quant_8x8_inter[band->quant_mat][0];
} else {
band->intra_base = ivi5_base_quant_4x4_intra;
band->inter_base = ivi5_base_quant_4x4_inter;
band->intra_scale = ivi5_scale_quant_4x4_intra;
band->inter_scale = ivi5_scale_quant_4x4_inter;
}
band->rv_map = &ctx->rvmap_tabs[band->rvmap_sel];
/* apply corrections to the selected rvmap table if present */
for (i = 0; i < band->num_corr; i++) {
idx1 = band->corr[i*2];
idx2 = band->corr[i*2+1];
FFSWAP(uint8_t, band->rv_map->runtab[idx1], band->rv_map->runtab[idx2]);
FFSWAP(int16_t, band->rv_map->valtab[idx1], band->rv_map->valtab[idx2]);
}
pos = get_bits_count(&ctx->gb);
for (t = 0; t < band->num_tiles; t++) {
tile = &band->tiles[t];
tile->is_empty = get_bits1(&ctx->gb);
if (tile->is_empty) {
ff_ivi_process_empty_tile(avctx, band, tile,
(ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3));
} else {
tile->data_size = ff_ivi_dec_tile_data_size(&ctx->gb);
result = decode_mb_info(ctx, band, tile, avctx);
if (result < 0)
break;
result = ff_ivi_decode_blocks(&ctx->gb, band, tile);
if (result < 0 || (get_bits_count(&ctx->gb) - pos) >> 3 != tile->data_size) {
av_log(avctx, AV_LOG_ERROR, "Corrupted tile data encountered!\n");
break;
}
pos += tile->data_size << 3; // skip to next tile
}
}
/* restore the selected rvmap table by applying its corrections in reverse order */
for (i = band->num_corr-1; i >= 0; i--) {
idx1 = band->corr[i*2];
idx2 = band->corr[i*2+1];
FFSWAP(uint8_t, band->rv_map->runtab[idx1], band->rv_map->runtab[idx2]);
FFSWAP(int16_t, band->rv_map->valtab[idx1], band->rv_map->valtab[idx2]);
}
#if IVI_DEBUG
if (band->checksum_present) {
uint16_t chksum = ivi_calc_band_checksum(band);
if (chksum != band->checksum) {
av_log(avctx, AV_LOG_ERROR,
"Band checksum mismatch! Plane %d, band %d, received: %x, calculated: %x\n",
band->plane, band->band_num, band->checksum, chksum);
}
}
#endif
align_get_bits(&ctx->gb);
return result;
}
/**
* Switches buffers.
*
* @param ctx [in,out] ptr to the decoder context
* @param avctx [in] ptr to the AVCodecContext
*/
static void switch_buffers(IVI5DecContext *ctx, AVCodecContext *avctx)
{
switch (ctx->prev_frame_type) {
case FRAMETYPE_INTRA:
case FRAMETYPE_INTER:
ctx->buf_switch ^= 1;
ctx->dst_buf = ctx->buf_switch;
ctx->ref_buf = ctx->buf_switch ^ 1;
break;
case FRAMETYPE_INTER_SCAL:
if (!ctx->inter_scal) {
ctx->ref2_buf = 2;
ctx->inter_scal = 1;
}
FFSWAP(int, ctx->dst_buf, ctx->ref2_buf);
ctx->ref_buf = ctx->ref2_buf;
break;
case FRAMETYPE_INTER_NOREF:
break;
}
switch (ctx->frame_type) {
case FRAMETYPE_INTRA:
ctx->buf_switch = 0;
/* FALLTHROUGH */
case FRAMETYPE_INTER:
ctx->inter_scal = 0;
ctx->dst_buf = ctx->buf_switch;
ctx->ref_buf = ctx->buf_switch ^ 1;
break;
case FRAMETYPE_INTER_SCAL:
case FRAMETYPE_INTER_NOREF:
case FRAMETYPE_NULL:
break;
}
}
/**
* Initializes Indeo5 decoder.
*/
static av_cold int decode_init(AVCodecContext *avctx)
{
IVI5DecContext *ctx = avctx->priv_data;
int result;
ff_ivi_init_static_vlc();
/* copy rvmap tables in our context so we can apply changes to them */
memcpy(ctx->rvmap_tabs, ff_ivi_rvmap_tabs, sizeof(ff_ivi_rvmap_tabs));
/* set the initial picture layout according to the basic profile:
there is only one band per plane (no scalability), only one tile (no local decoding)
and picture format = YVU9 */
ctx->pic_conf.pic_width = avctx->width;
ctx->pic_conf.pic_height = avctx->height;
ctx->pic_conf.chroma_width = (avctx->width + 3) >> 2;
ctx->pic_conf.chroma_height = (avctx->height + 3) >> 2;
ctx->pic_conf.tile_width = avctx->width;
ctx->pic_conf.tile_height = avctx->height;
ctx->pic_conf.luma_bands = ctx->pic_conf.chroma_bands = 1;
result = ff_ivi_init_planes(ctx->planes, &ctx->pic_conf);
if (result) {
av_log(avctx, AV_LOG_ERROR, "Couldn't allocate color planes!\n");
return -1;
}
ctx->buf_switch = 0;
ctx->inter_scal = 0;
avctx->pix_fmt = PIX_FMT_YUV410P;
return 0;
}
/**
* main decoder function
*/
static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
AVPacket *avpkt)
{
IVI5DecContext *ctx = avctx->priv_data;
const uint8_t *buf = avpkt->data;
int buf_size = avpkt->size;
int result, p, b;
init_get_bits(&ctx->gb, buf, buf_size * 8);
ctx->frame_data = buf;
ctx->frame_size = buf_size;
result = decode_pic_hdr(ctx, avctx);
if (result) {
av_log(avctx, AV_LOG_ERROR,
"Error while decoding picture header: %d\n", result);
return -1;
}
if (ctx->gop_flags & IVI5_IS_PROTECTED) {
av_log(avctx, AV_LOG_ERROR, "Password-protected clip!\n");
return -1;
}
switch_buffers(ctx, avctx);
//START_TIMER;
if (ctx->frame_type != FRAMETYPE_NULL) {
for (p = 0; p < 3; p++) {
for (b = 0; b < ctx->planes[p].num_bands; b++) {
result = decode_band(ctx, p, &ctx->planes[p].bands[b], avctx);
if (result) {
av_log(avctx, AV_LOG_ERROR,
"Error while decoding band: %d, plane: %d\n", b, p);
return -1;
}
}
}
}
//STOP_TIMER("decode_planes");
if (ctx->frame.data[0])
avctx->release_buffer(avctx, &ctx->frame);
ctx->frame.reference = 0;
if (avctx->get_buffer(avctx, &ctx->frame) < 0) {
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
return -1;
}
if (ctx->is_scalable) {
ff_ivi_recompose53 (&ctx->planes[0], ctx->frame.data[0], ctx->frame.linesize[0], 4);
} else {
ff_ivi_output_plane(&ctx->planes[0], ctx->frame.data[0], ctx->frame.linesize[0]);
}
ff_ivi_output_plane(&ctx->planes[2], ctx->frame.data[1], ctx->frame.linesize[1]);
ff_ivi_output_plane(&ctx->planes[1], ctx->frame.data[2], ctx->frame.linesize[2]);
*data_size = sizeof(AVFrame);
*(AVFrame*)data = ctx->frame;
return buf_size;
}
/**
* Closes Indeo5 decoder and cleans up its context.
*/
static av_cold int decode_close(AVCodecContext *avctx)
{
IVI5DecContext *ctx = avctx->priv_data;
ff_ivi_free_buffers(&ctx->planes[0]);
if (ctx->mb_vlc.cust_tab.table)
free_vlc(&ctx->mb_vlc.cust_tab);
if (ctx->frame.data[0])
avctx->release_buffer(avctx, &ctx->frame);
return 0;
}
AVCodec indeo5_decoder = {
.name = "indeo5",
.type = AVMEDIA_TYPE_VIDEO,
.id = CODEC_ID_INDEO5,
.priv_data_size = sizeof(IVI5DecContext),
.init = decode_init,
.close = decode_close,
.decode = decode_frame,
.long_name = NULL_IF_CONFIG_SMALL("Intel Indeo Video Interactive 5"),
};
| 123linslouis-android-video-cutter | jni/libavcodec/indeo5.c | C | asf20 | 27,516 |
/*
* American Laser Games MM Video Decoder
* Copyright (c) 2006,2008 Peter Ross
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* American Laser Games MM Video Decoder
* by Peter Ross (pross@xvid.org)
*
* The MM format was used by IBM-PC ports of ALG's "arcade shooter" games,
* including Mad Dog McCree and Crime Patrol.
*
* Technical details here:
* http://wiki.multimedia.cx/index.php?title=American_Laser_Games_MM
*/
#include "libavutil/intreadwrite.h"
#include "avcodec.h"
#define MM_PREAMBLE_SIZE 6
#define MM_TYPE_INTER 0x5
#define MM_TYPE_INTRA 0x8
#define MM_TYPE_INTRA_HH 0xc
#define MM_TYPE_INTER_HH 0xd
#define MM_TYPE_INTRA_HHV 0xe
#define MM_TYPE_INTER_HHV 0xf
#define MM_TYPE_PALETTE 0x31
typedef struct MmContext {
AVCodecContext *avctx;
AVFrame frame;
int palette[AVPALETTE_COUNT];
} MmContext;
static av_cold int mm_decode_init(AVCodecContext *avctx)
{
MmContext *s = avctx->priv_data;
s->avctx = avctx;
avctx->pix_fmt = PIX_FMT_PAL8;
s->frame.reference = 1;
if (avctx->get_buffer(avctx, &s->frame)) {
av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
return -1;
}
return 0;
}
static void mm_decode_pal(MmContext *s, const uint8_t *buf, const uint8_t *buf_end)
{
int i;
buf += 4;
for (i=0; i<128 && buf+2<buf_end; i++) {
s->palette[i] = AV_RB24(buf);
s->palette[i+128] = s->palette[i]<<2;
buf += 3;
}
}
static void mm_decode_intra(MmContext * s, int half_horiz, int half_vert, const uint8_t *buf, int buf_size)
{
int i, x, y;
i=0; x=0; y=0;
while(i<buf_size) {
int run_length, color;
if (buf[i] & 0x80) {
run_length = 1;
color = buf[i];
i++;
}else{
run_length = (buf[i] & 0x7f) + 2;
color = buf[i+1];
i+=2;
}
if (half_horiz)
run_length *=2;
if (color) {
memset(s->frame.data[0] + y*s->frame.linesize[0] + x, color, run_length);
if (half_vert)
memset(s->frame.data[0] + (y+1)*s->frame.linesize[0] + x, color, run_length);
}
x+= run_length;
if (x >= s->avctx->width) {
x=0;
y += half_vert ? 2 : 1;
}
}
}
static void mm_decode_inter(MmContext * s, int half_horiz, int half_vert, const uint8_t *buf, int buf_size)
{
const int data_ptr = 2 + AV_RL16(&buf[0]);
int d, r, y;
d = data_ptr; r = 2; y = 0;
while(r < data_ptr) {
int i, j;
int length = buf[r] & 0x7f;
int x = buf[r+1] + ((buf[r] & 0x80) << 1);
r += 2;
if (length==0) {
y += x;
continue;
}
for(i=0; i<length; i++) {
for(j=0; j<8; j++) {
int replace = (buf[r+i] >> (7-j)) & 1;
if (replace) {
int color = buf[d];
s->frame.data[0][y*s->frame.linesize[0] + x] = color;
if (half_horiz)
s->frame.data[0][y*s->frame.linesize[0] + x + 1] = color;
if (half_vert) {
s->frame.data[0][(y+1)*s->frame.linesize[0] + x] = color;
if (half_horiz)
s->frame.data[0][(y+1)*s->frame.linesize[0] + x + 1] = color;
}
d++;
}
x += half_horiz ? 2 : 1;
}
}
r += length;
y += half_vert ? 2 : 1;
}
}
static int mm_decode_frame(AVCodecContext *avctx,
void *data, int *data_size,
AVPacket *avpkt)
{
const uint8_t *buf = avpkt->data;
int buf_size = avpkt->size;
MmContext *s = avctx->priv_data;
const uint8_t *buf_end = buf+buf_size;
int type;
type = AV_RL16(&buf[0]);
buf += MM_PREAMBLE_SIZE;
buf_size -= MM_PREAMBLE_SIZE;
switch(type) {
case MM_TYPE_PALETTE : mm_decode_pal(s, buf, buf_end); return buf_size;
case MM_TYPE_INTRA : mm_decode_intra(s, 0, 0, buf, buf_size); break;
case MM_TYPE_INTRA_HH : mm_decode_intra(s, 1, 0, buf, buf_size); break;
case MM_TYPE_INTRA_HHV : mm_decode_intra(s, 1, 1, buf, buf_size); break;
case MM_TYPE_INTER : mm_decode_inter(s, 0, 0, buf, buf_size); break;
case MM_TYPE_INTER_HH : mm_decode_inter(s, 1, 0, buf, buf_size); break;
case MM_TYPE_INTER_HHV : mm_decode_inter(s, 1, 1, buf, buf_size); break;
default :
return -1;
}
memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
*data_size = sizeof(AVFrame);
*(AVFrame*)data = s->frame;
return buf_size;
}
static av_cold int mm_decode_end(AVCodecContext *avctx)
{
MmContext *s = avctx->priv_data;
if(s->frame.data[0])
avctx->release_buffer(avctx, &s->frame);
return 0;
}
AVCodec mmvideo_decoder = {
"mmvideo",
AVMEDIA_TYPE_VIDEO,
CODEC_ID_MMVIDEO,
sizeof(MmContext),
mm_decode_init,
NULL,
mm_decode_end,
mm_decode_frame,
CODEC_CAP_DR1,
.long_name = NULL_IF_CONFIG_SMALL("American Laser Games MM Video"),
};
| 123linslouis-android-video-cutter | jni/libavcodec/mmvideo.c | C | asf20 | 5,938 |
/*
* R210 decoder
*
* Copyright (c) 2009 Reimar Doeffinger <Reimar.Doeffinger@gmx.de>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "avcodec.h"
#include "libavutil/bswap.h"
static av_cold int decode_init(AVCodecContext *avctx)
{
avctx->pix_fmt = PIX_FMT_RGB48;
avctx->bits_per_raw_sample = 10;
avctx->coded_frame = avcodec_alloc_frame();
return 0;
}
static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
AVPacket *avpkt)
{
int h, w;
AVFrame *pic = avctx->coded_frame;
const uint32_t *src = (const uint32_t *)avpkt->data;
int aligned_width = FFALIGN(avctx->width, 64);
uint8_t *dst_line;
if (pic->data[0])
avctx->release_buffer(avctx, pic);
if (avpkt->size < 4 * aligned_width * avctx->height) {
av_log(avctx, AV_LOG_ERROR, "packet too small\n");
return -1;
}
pic->reference = 0;
if (avctx->get_buffer(avctx, pic) < 0)
return -1;
pic->pict_type = FF_I_TYPE;
pic->key_frame = 1;
dst_line = pic->data[0];
for (h = 0; h < avctx->height; h++) {
uint16_t *dst = (uint16_t *)dst_line;
for (w = 0; w < avctx->width; w++) {
uint32_t pixel = be2me_32(*src++);
uint16_t r, g, b;
b = pixel << 6;
g = (pixel >> 4) & 0xffc0;
r = (pixel >> 14) & 0xffc0;
*dst++ = r | (r >> 10);
*dst++ = g | (g >> 10);
*dst++ = b | (b >> 10);
}
src += aligned_width - avctx->width;
dst_line += pic->linesize[0];
}
*data_size = sizeof(AVFrame);
*(AVFrame*)data = *avctx->coded_frame;
return avpkt->size;
}
static av_cold int decode_close(AVCodecContext *avctx)
{
AVFrame *pic = avctx->coded_frame;
if (pic->data[0])
avctx->release_buffer(avctx, pic);
av_freep(&avctx->coded_frame);
return 0;
}
AVCodec r210_decoder = {
"r210",
AVMEDIA_TYPE_VIDEO,
CODEC_ID_R210,
0,
decode_init,
NULL,
decode_close,
decode_frame,
CODEC_CAP_DR1,
.long_name = NULL_IF_CONFIG_SMALL("Uncompressed RGB 10-bit"),
};
| 123linslouis-android-video-cutter | jni/libavcodec/r210dec.c | C | asf20 | 2,891 |
/*
* copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "avcodec.h"
#include "mpegaudio.h"
#include "mpegaudiodata.h"
static int mp3_header_decompress(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,
uint8_t **poutbuf, int *poutbuf_size,
const uint8_t *buf, int buf_size, int keyframe){
uint32_t header;
int sample_rate= avctx->sample_rate;
int sample_rate_index=0;
int lsf, mpeg25, bitrate_index, frame_size;
header = AV_RB32(buf);
if(ff_mpa_check_header(header) >= 0){
*poutbuf= (uint8_t *) buf;
*poutbuf_size= buf_size;
return 0;
}
if(avctx->extradata_size != 15 || strcmp(avctx->extradata, "FFCMP3 0.0")){
av_log(avctx, AV_LOG_ERROR, "Extradata invalid %d\n", avctx->extradata_size);
return -1;
}
header= AV_RB32(avctx->extradata+11) & MP3_MASK;
lsf = sample_rate < (24000+32000)/2;
mpeg25 = sample_rate < (12000+16000)/2;
sample_rate_index= (header>>10)&3;
sample_rate= ff_mpa_freq_tab[sample_rate_index] >> (lsf + mpeg25); //in case sample rate is a little off
for(bitrate_index=2; bitrate_index<30; bitrate_index++){
frame_size = ff_mpa_bitrate_tab[lsf][2][bitrate_index>>1];
frame_size = (frame_size * 144000) / (sample_rate << lsf) + (bitrate_index&1);
if(frame_size == buf_size + 4)
break;
if(frame_size == buf_size + 6)
break;
}
if(bitrate_index == 30){
av_log(avctx, AV_LOG_ERROR, "Could not find bitrate_index.\n");
return -1;
}
header |= (bitrate_index&1)<<9;
header |= (bitrate_index>>1)<<12;
header |= (frame_size == buf_size + 4)<<16; //FIXME actually set a correct crc instead of 0
*poutbuf_size= frame_size;
*poutbuf= av_malloc(frame_size + FF_INPUT_BUFFER_PADDING_SIZE);
memcpy(*poutbuf + frame_size - buf_size, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
if(avctx->channels==2){
uint8_t *p= *poutbuf + frame_size - buf_size;
if(lsf){
FFSWAP(int, p[1], p[2]);
header |= (p[1] & 0xC0)>>2;
p[1] &= 0x3F;
}else{
header |= p[1] & 0x30;
p[1] &= 0xCF;
}
}
AV_WB32(*poutbuf, header);
return 1;
}
AVBitStreamFilter mp3_header_decompress_bsf={
"mp3decomp",
0,
mp3_header_decompress,
};
| 123linslouis-android-video-cutter | jni/libavcodec/mp3_header_decompress_bsf.c | C | asf20 | 3,194 |
/*
* LCL (LossLess Codec Library) Codec
* Copyright (c) 2002-2004 Roberto Togni
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef AVCODEC_LCL_H
#define AVCODEC_LCL_H
#define BMPTYPE_YUV 1
#define BMPTYPE_RGB 2
#define IMGTYPE_YUV111 0
#define IMGTYPE_YUV422 1
#define IMGTYPE_RGB24 2
#define IMGTYPE_YUV411 3
#define IMGTYPE_YUV211 4
#define IMGTYPE_YUV420 5
#define COMP_MSZH 0
#define COMP_MSZH_NOCOMP 1
#define COMP_ZLIB_HISPEED 1
#define COMP_ZLIB_HICOMP 9
#define COMP_ZLIB_NORMAL -1
#define FLAG_MULTITHREAD 1
#define FLAG_NULLFRAME 2
#define FLAG_PNGFILTER 4
#define FLAGMASK_UNUSED 0xf8
#define CODEC_MSZH 1
#define CODEC_ZLIB 3
#endif /* AVCODEC_LCL_H */
| 123linslouis-android-video-cutter | jni/libavcodec/lcl.h | C | asf20 | 1,394 |
/*
* VC-1 and WMV3 parser
* Copyright (c) 2006-2007 Konstantin Shishkov
* Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* VC-1 and WMV3 parser
*/
#include "parser.h"
#include "vc1.h"
#include "get_bits.h"
typedef struct {
ParseContext pc;
VC1Context v;
} VC1ParseContext;
static void vc1_extract_headers(AVCodecParserContext *s, AVCodecContext *avctx,
const uint8_t *buf, int buf_size)
{
VC1ParseContext *vpc = s->priv_data;
GetBitContext gb;
const uint8_t *start, *end, *next;
uint8_t *buf2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
vpc->v.s.avctx = avctx;
vpc->v.parse_only = 1;
next = buf;
for(start = buf, end = buf + buf_size; next < end; start = next){
int buf2_size, size;
next = find_next_marker(start + 4, end);
size = next - start - 4;
buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
init_get_bits(&gb, buf2, buf2_size * 8);
if(size <= 0) continue;
switch(AV_RB32(start)){
case VC1_CODE_SEQHDR:
vc1_decode_sequence_header(avctx, &vpc->v, &gb);
break;
case VC1_CODE_ENTRYPOINT:
vc1_decode_entry_point(avctx, &vpc->v, &gb);
break;
case VC1_CODE_FRAME:
if(vpc->v.profile < PROFILE_ADVANCED)
vc1_parse_frame_header (&vpc->v, &gb);
else
vc1_parse_frame_header_adv(&vpc->v, &gb);
/* keep FF_BI_TYPE internal to VC1 */
if (vpc->v.s.pict_type == FF_BI_TYPE)
s->pict_type = FF_B_TYPE;
else
s->pict_type = vpc->v.s.pict_type;
break;
}
}
av_free(buf2);
}
/**
* finds the end of the current frame in the bitstream.
* @return the position of the first byte of the next frame, or -1
*/
static int vc1_find_frame_end(ParseContext *pc, const uint8_t *buf,
int buf_size) {
int pic_found, i;
uint32_t state;
pic_found= pc->frame_start_found;
state= pc->state;
i=0;
if(!pic_found){
for(i=0; i<buf_size; i++){
state= (state<<8) | buf[i];
if(state == VC1_CODE_FRAME || state == VC1_CODE_FIELD){
i++;
pic_found=1;
break;
}
}
}
if(pic_found){
/* EOF considered as end of frame */
if (buf_size == 0)
return 0;
for(; i<buf_size; i++){
state= (state<<8) | buf[i];
if(IS_MARKER(state) && state != VC1_CODE_FIELD && state != VC1_CODE_SLICE){
pc->frame_start_found=0;
pc->state=-1;
return i-3;
}
}
}
pc->frame_start_found= pic_found;
pc->state= state;
return END_NOT_FOUND;
}
static int vc1_parse(AVCodecParserContext *s,
AVCodecContext *avctx,
const uint8_t **poutbuf, int *poutbuf_size,
const uint8_t *buf, int buf_size)
{
VC1ParseContext *vpc = s->priv_data;
int next;
if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
next= buf_size;
}else{
next= vc1_find_frame_end(&vpc->pc, buf, buf_size);
if (ff_combine_frame(&vpc->pc, next, &buf, &buf_size) < 0) {
*poutbuf = NULL;
*poutbuf_size = 0;
return buf_size;
}
}
vc1_extract_headers(s, avctx, buf, buf_size);
*poutbuf = buf;
*poutbuf_size = buf_size;
return next;
}
static int vc1_split(AVCodecContext *avctx,
const uint8_t *buf, int buf_size)
{
int i;
uint32_t state= -1;
int charged=0;
for(i=0; i<buf_size; i++){
state= (state<<8) | buf[i];
if(IS_MARKER(state)){
if(state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT){
charged=1;
}else if(charged){
return i-3;
}
}
}
return 0;
}
AVCodecParser vc1_parser = {
{ CODEC_ID_VC1 },
sizeof(VC1ParseContext),
NULL,
vc1_parse,
ff_parse1_close,
vc1_split,
};
| 123linslouis-android-video-cutter | jni/libavcodec/vc1_parser.c | C | asf20 | 5,008 |
/*
* Feeble Files/ScummVM DXA decoder
* Copyright (c) 2007 Konstantin Shishkov
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* DXA Video decoder
*/
#include <stdio.h>
#include <stdlib.h>
#include "libavutil/intreadwrite.h"
#include "avcodec.h"
#include <zlib.h>
/*
* Decoder context
*/
typedef struct DxaDecContext {
AVCodecContext *avctx;
AVFrame pic, prev;
int dsize;
uint8_t *decomp_buf;
uint32_t pal[256];
} DxaDecContext;
static const int shift1[6] = { 0, 8, 8, 8, 4, 4 };
static const int shift2[6] = { 0, 0, 8, 4, 0, 4 };
static int decode_13(AVCodecContext *avctx, DxaDecContext *c, uint8_t* dst, uint8_t *src, uint8_t *ref)
{
uint8_t *code, *data, *mv, *msk, *tmp, *tmp2;
int i, j, k;
int type, x, y, d, d2;
int stride = c->pic.linesize[0];
uint32_t mask;
code = src + 12;
data = code + ((avctx->width * avctx->height) >> 4);
mv = data + AV_RB32(src + 0);
msk = mv + AV_RB32(src + 4);
for(j = 0; j < avctx->height; j += 4){
for(i = 0; i < avctx->width; i += 4){
tmp = dst + i;
tmp2 = ref + i;
type = *code++;
switch(type){
case 4: // motion compensation
x = (*mv) >> 4; if(x & 8) x = 8 - x;
y = (*mv++) & 0xF; if(y & 8) y = 8 - y;
tmp2 += x + y*stride;
case 0: // skip
case 5: // skip in method 12
for(y = 0; y < 4; y++){
memcpy(tmp, tmp2, 4);
tmp += stride;
tmp2 += stride;
}
break;
case 1: // masked change
case 10: // masked change with only half of pixels changed
case 11: // cases 10-15 are for method 12 only
case 12:
case 13:
case 14:
case 15:
if(type == 1){
mask = AV_RB16(msk);
msk += 2;
}else{
type -= 10;
mask = ((msk[0] & 0xF0) << shift1[type]) | ((msk[0] & 0xF) << shift2[type]);
msk++;
}
for(y = 0; y < 4; y++){
for(x = 0; x < 4; x++){
tmp[x] = (mask & 0x8000) ? *data++ : tmp2[x];
mask <<= 1;
}
tmp += stride;
tmp2 += stride;
}
break;
case 2: // fill block
for(y = 0; y < 4; y++){
memset(tmp, data[0], 4);
tmp += stride;
}
data++;
break;
case 3: // raw block
for(y = 0; y < 4; y++){
memcpy(tmp, data, 4);
data += 4;
tmp += stride;
}
break;
case 8: // subblocks - method 13 only
mask = *msk++;
for(k = 0; k < 4; k++){
d = ((k & 1) << 1) + ((k & 2) * stride);
d2 = ((k & 1) << 1) + ((k & 2) * stride);
tmp2 = ref + i + d2;
switch(mask & 0xC0){
case 0x80: // motion compensation
x = (*mv) >> 4; if(x & 8) x = 8 - x;
y = (*mv++) & 0xF; if(y & 8) y = 8 - y;
tmp2 += x + y*stride;
case 0x00: // skip
tmp[d + 0 ] = tmp2[0];
tmp[d + 1 ] = tmp2[1];
tmp[d + 0 + stride] = tmp2[0 + stride];
tmp[d + 1 + stride] = tmp2[1 + stride];
break;
case 0x40: // fill
tmp[d + 0 ] = data[0];
tmp[d + 1 ] = data[0];
tmp[d + 0 + stride] = data[0];
tmp[d + 1 + stride] = data[0];
data++;
break;
case 0xC0: // raw
tmp[d + 0 ] = *data++;
tmp[d + 1 ] = *data++;
tmp[d + 0 + stride] = *data++;
tmp[d + 1 + stride] = *data++;
break;
}
mask <<= 2;
}
break;
case 32: // vector quantization - 2 colors
mask = AV_RB16(msk);
msk += 2;
for(y = 0; y < 4; y++){
for(x = 0; x < 4; x++){
tmp[x] = data[mask & 1];
mask >>= 1;
}
tmp += stride;
tmp2 += stride;
}
data += 2;
break;
case 33: // vector quantization - 3 or 4 colors
case 34:
mask = AV_RB32(msk);
msk += 4;
for(y = 0; y < 4; y++){
for(x = 0; x < 4; x++){
tmp[x] = data[mask & 3];
mask >>= 2;
}
tmp += stride;
tmp2 += stride;
}
data += type - 30;
break;
default:
av_log(avctx, AV_LOG_ERROR, "Unknown opcode %d\n", type);
return -1;
}
}
dst += stride * 4;
ref += stride * 4;
}
return 0;
}
static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
{
const uint8_t *buf = avpkt->data;
int buf_size = avpkt->size;
DxaDecContext * const c = avctx->priv_data;
uint8_t *outptr, *srcptr, *tmpptr;
unsigned long dsize;
int i, j, compr;
int stride;
int orig_buf_size = buf_size;
int pc = 0;
/* make the palette available on the way out */
if(buf[0]=='C' && buf[1]=='M' && buf[2]=='A' && buf[3]=='P'){
int r, g, b;
buf += 4;
for(i = 0; i < 256; i++){
r = *buf++;
g = *buf++;
b = *buf++;
c->pal[i] = (r << 16) | (g << 8) | b;
}
pc = 1;
buf_size -= 768+4;
}
if(avctx->get_buffer(avctx, &c->pic) < 0){
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
return -1;
}
memcpy(c->pic.data[1], c->pal, AVPALETTE_SIZE);
c->pic.palette_has_changed = pc;
outptr = c->pic.data[0];
srcptr = c->decomp_buf;
tmpptr = c->prev.data[0];
stride = c->pic.linesize[0];
if(buf[0]=='N' && buf[1]=='U' && buf[2]=='L' && buf[3]=='L')
compr = -1;
else
compr = buf[4];
dsize = c->dsize;
if((compr != 4 && compr != -1) && uncompress(c->decomp_buf, &dsize, buf + 9, buf_size - 9) != Z_OK){
av_log(avctx, AV_LOG_ERROR, "Uncompress failed!\n");
return -1;
}
switch(compr){
case -1:
c->pic.key_frame = 0;
c->pic.pict_type = FF_P_TYPE;
if(c->prev.data[0])
memcpy(c->pic.data[0], c->prev.data[0], c->pic.linesize[0] * avctx->height);
else{ // Should happen only when first frame is 'NULL'
memset(c->pic.data[0], 0, c->pic.linesize[0] * avctx->height);
c->pic.key_frame = 1;
c->pic.pict_type = FF_I_TYPE;
}
break;
case 2:
case 3:
case 4:
case 5:
c->pic.key_frame = !(compr & 1);
c->pic.pict_type = (compr & 1) ? FF_P_TYPE : FF_I_TYPE;
for(j = 0; j < avctx->height; j++){
if(compr & 1){
for(i = 0; i < avctx->width; i++)
outptr[i] = srcptr[i] ^ tmpptr[i];
tmpptr += stride;
}else
memcpy(outptr, srcptr, avctx->width);
outptr += stride;
srcptr += avctx->width;
}
break;
case 12: // ScummVM coding
case 13:
c->pic.key_frame = 0;
c->pic.pict_type = FF_P_TYPE;
decode_13(avctx, c, c->pic.data[0], srcptr, c->prev.data[0]);
break;
default:
av_log(avctx, AV_LOG_ERROR, "Unknown/unsupported compression type %d\n", buf[4]);
return -1;
}
FFSWAP(AVFrame, c->pic, c->prev);
if(c->pic.data[0])
avctx->release_buffer(avctx, &c->pic);
*data_size = sizeof(AVFrame);
*(AVFrame*)data = c->prev;
/* always report that the buffer was completely consumed */
return orig_buf_size;
}
static av_cold int decode_init(AVCodecContext *avctx)
{
DxaDecContext * const c = avctx->priv_data;
c->avctx = avctx;
avctx->pix_fmt = PIX_FMT_PAL8;
c->dsize = avctx->width * avctx->height * 2;
if((c->decomp_buf = av_malloc(c->dsize)) == NULL) {
av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
return -1;
}
return 0;
}
static av_cold int decode_end(AVCodecContext *avctx)
{
DxaDecContext * const c = avctx->priv_data;
av_freep(&c->decomp_buf);
if(c->prev.data[0])
avctx->release_buffer(avctx, &c->prev);
if(c->pic.data[0])
avctx->release_buffer(avctx, &c->pic);
return 0;
}
AVCodec dxa_decoder = {
"dxa",
AVMEDIA_TYPE_VIDEO,
CODEC_ID_DXA,
sizeof(DxaDecContext),
decode_init,
NULL,
decode_end,
decode_frame,
CODEC_CAP_DR1,
.long_name = NULL_IF_CONFIG_SMALL("Feeble Files/ScummVM DXA"),
};
| 123linslouis-android-video-cutter | jni/libavcodec/dxa.c | C | asf20 | 10,330 |
/*
* PNG image format
* Copyright (c) 2003 Fabrice Bellard
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "avcodec.h"
#include "bytestream.h"
#include "png.h"
const uint8_t ff_pngsig[8] = {137, 80, 78, 71, 13, 10, 26, 10};
const uint8_t ff_mngsig[8] = {138, 77, 78, 71, 13, 10, 26, 10};
/* Mask to determine which y pixels are valid in a pass */
const uint8_t ff_png_pass_ymask[NB_PASSES] = {
0x80, 0x80, 0x08, 0x88, 0x22, 0xaa, 0x55,
};
/* minimum x value */
const uint8_t ff_png_pass_xmin[NB_PASSES] = {
0, 4, 0, 2, 0, 1, 0
};
/* x shift to get row width */
const uint8_t ff_png_pass_xshift[NB_PASSES] = {
3, 3, 2, 2, 1, 1, 0
};
/* Mask to determine which pixels are valid in a pass */
const uint8_t ff_png_pass_mask[NB_PASSES] = {
0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff
};
void *ff_png_zalloc(void *opaque, unsigned int items, unsigned int size)
{
if(items >= UINT_MAX / size)
return NULL;
return av_malloc(items * size);
}
void ff_png_zfree(void *opaque, void *ptr)
{
av_free(ptr);
}
int ff_png_get_nb_channels(int color_type)
{
int channels;
channels = 1;
if ((color_type & (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_PALETTE)) ==
PNG_COLOR_MASK_COLOR)
channels = 3;
if (color_type & PNG_COLOR_MASK_ALPHA)
channels++;
return channels;
}
/* compute the row size of an interleaved pass */
int ff_png_pass_row_size(int pass, int bits_per_pixel, int width)
{
int shift, xmin, pass_width;
xmin = ff_png_pass_xmin[pass];
if (width <= xmin)
return 0;
shift = ff_png_pass_xshift[pass];
pass_width = (width - xmin + (1 << shift) - 1) >> shift;
return (pass_width * bits_per_pixel + 7) >> 3;
}
| 123linslouis-android-video-cutter | jni/libavcodec/png.c | C | asf20 | 2,435 |
/*
* Copyright (c) 2002-2006 Michael Niedermayer <michaelni@gmx.at>
* Copyright (c) 2006 Oded Shimon <ods15@ods15.dyndns.org>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* simple arithmetic expression evaluator.
*
* see http://joe.hotchkiss.com/programming/eval/eval.html
*/
#include "libavutil/avutil.h"
#include "eval.h"
typedef struct Parser{
int stack_index;
char *s;
const double *const_value;
const char * const *const_name; // NULL terminated
double (* const *func1)(void *, double a); // NULL terminated
const char * const *func1_name; // NULL terminated
double (* const *func2)(void *, double a, double b); // NULL terminated
const char * const *func2_name; // NULL terminated
void *opaque;
const char **error;
#define VARS 10
double var[VARS];
} Parser;
static const int8_t si_prefixes['z' - 'E' + 1]={
['y'-'E']= -24,
['z'-'E']= -21,
['a'-'E']= -18,
['f'-'E']= -15,
['p'-'E']= -12,
['n'-'E']= - 9,
['u'-'E']= - 6,
['m'-'E']= - 3,
['c'-'E']= - 2,
['d'-'E']= - 1,
['h'-'E']= 2,
['k'-'E']= 3,
['K'-'E']= 3,
['M'-'E']= 6,
['G'-'E']= 9,
['T'-'E']= 12,
['P'-'E']= 15,
['E'-'E']= 18,
['Z'-'E']= 21,
['Y'-'E']= 24,
};
double av_strtod(const char *numstr, char **tail) {
double d;
char *next;
d = strtod(numstr, &next);
/* if parsing succeeded, check for and interpret postfixes */
if (next!=numstr) {
if(*next >= 'E' && *next <= 'z'){
int e= si_prefixes[*next - 'E'];
if(e){
if(next[1] == 'i'){
d*= pow( 2, e/0.3);
next+=2;
}else{
d*= pow(10, e);
next++;
}
}
}
if(*next=='B') {
d*=8;
next++;
}
}
/* if requested, fill in tail with the position after the last parsed
character */
if (tail)
*tail = next;
return d;
}
static int strmatch(const char *s, const char *prefix){
int i;
for(i=0; prefix[i]; i++){
if(prefix[i] != s[i]) return 0;
}
return 1;
}
struct AVExpr {
enum {
e_value, e_const, e_func0, e_func1, e_func2,
e_squish, e_gauss, e_ld,
e_mod, e_max, e_min, e_eq, e_gt, e_gte,
e_pow, e_mul, e_div, e_add,
e_last, e_st, e_while,
} type;
double value; // is sign in other types
union {
int const_index;
double (*func0)(double);
double (*func1)(void *, double);
double (*func2)(void *, double, double);
} a;
struct AVExpr *param[2];
};
static double eval_expr(Parser * p, AVExpr * e) {
switch (e->type) {
case e_value: return e->value;
case e_const: return e->value * p->const_value[e->a.const_index];
case e_func0: return e->value * e->a.func0(eval_expr(p, e->param[0]));
case e_func1: return e->value * e->a.func1(p->opaque, eval_expr(p, e->param[0]));
case e_func2: return e->value * e->a.func2(p->opaque, eval_expr(p, e->param[0]), eval_expr(p, e->param[1]));
case e_squish: return 1/(1+exp(4*eval_expr(p, e->param[0])));
case e_gauss: { double d = eval_expr(p, e->param[0]); return exp(-d*d/2)/sqrt(2*M_PI); }
case e_ld: return e->value * p->var[av_clip(eval_expr(p, e->param[0]), 0, VARS-1)];
case e_while: {
double d = NAN;
while(eval_expr(p, e->param[0]))
d=eval_expr(p, e->param[1]);
return d;
}
default: {
double d = eval_expr(p, e->param[0]);
double d2 = eval_expr(p, e->param[1]);
switch (e->type) {
case e_mod: return e->value * (d - floor(d/d2)*d2);
case e_max: return e->value * (d > d2 ? d : d2);
case e_min: return e->value * (d < d2 ? d : d2);
case e_eq: return e->value * (d == d2 ? 1.0 : 0.0);
case e_gt: return e->value * (d > d2 ? 1.0 : 0.0);
case e_gte: return e->value * (d >= d2 ? 1.0 : 0.0);
case e_pow: return e->value * pow(d, d2);
case e_mul: return e->value * (d * d2);
case e_div: return e->value * (d / d2);
case e_add: return e->value * (d + d2);
case e_last:return e->value * d2;
case e_st : return e->value * (p->var[av_clip(d, 0, VARS-1)]= d2);
}
}
}
return NAN;
}
static AVExpr * parse_expr(Parser *p);
void ff_free_expr(AVExpr * e) {
if (!e) return;
ff_free_expr(e->param[0]);
ff_free_expr(e->param[1]);
av_freep(&e);
}
static AVExpr * parse_primary(Parser *p) {
AVExpr * d = av_mallocz(sizeof(AVExpr));
char *next= p->s;
int i;
if (!d)
return NULL;
/* number */
d->value = av_strtod(p->s, &next);
if(next != p->s){
d->type = e_value;
p->s= next;
return d;
}
d->value = 1;
/* named constants */
for(i=0; p->const_name && p->const_name[i]; i++){
if(strmatch(p->s, p->const_name[i])){
p->s+= strlen(p->const_name[i]);
d->type = e_const;
d->a.const_index = i;
return d;
}
}
p->s= strchr(p->s, '(');
if(p->s==NULL){
*p->error = "undefined constant or missing (";
p->s= next;
ff_free_expr(d);
return NULL;
}
p->s++; // "("
if (*next == '(') { // special case do-nothing
av_freep(&d);
d = parse_expr(p);
if(p->s[0] != ')'){
*p->error = "missing )";
ff_free_expr(d);
return NULL;
}
p->s++; // ")"
return d;
}
d->param[0] = parse_expr(p);
if(p->s[0]== ','){
p->s++; // ","
d->param[1] = parse_expr(p);
}
if(p->s[0] != ')'){
*p->error = "missing )";
ff_free_expr(d);
return NULL;
}
p->s++; // ")"
d->type = e_func0;
if( strmatch(next, "sinh" ) ) d->a.func0 = sinh;
else if( strmatch(next, "cosh" ) ) d->a.func0 = cosh;
else if( strmatch(next, "tanh" ) ) d->a.func0 = tanh;
else if( strmatch(next, "sin" ) ) d->a.func0 = sin;
else if( strmatch(next, "cos" ) ) d->a.func0 = cos;
else if( strmatch(next, "tan" ) ) d->a.func0 = tan;
else if( strmatch(next, "atan" ) ) d->a.func0 = atan;
else if( strmatch(next, "asin" ) ) d->a.func0 = asin;
else if( strmatch(next, "acos" ) ) d->a.func0 = acos;
else if( strmatch(next, "exp" ) ) d->a.func0 = exp;
else if( strmatch(next, "log" ) ) d->a.func0 = log;
else if( strmatch(next, "abs" ) ) d->a.func0 = fabs;
else if( strmatch(next, "squish") ) d->type = e_squish;
else if( strmatch(next, "gauss" ) ) d->type = e_gauss;
else if( strmatch(next, "mod" ) ) d->type = e_mod;
else if( strmatch(next, "max" ) ) d->type = e_max;
else if( strmatch(next, "min" ) ) d->type = e_min;
else if( strmatch(next, "eq" ) ) d->type = e_eq;
else if( strmatch(next, "gte" ) ) d->type = e_gte;
else if( strmatch(next, "gt" ) ) d->type = e_gt;
else if( strmatch(next, "lte" ) ) { AVExpr * tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gt; }
else if( strmatch(next, "lt" ) ) { AVExpr * tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gte; }
else if( strmatch(next, "ld" ) ) d->type = e_ld;
else if( strmatch(next, "st" ) ) d->type = e_st;
else if( strmatch(next, "while" ) ) d->type = e_while;
else {
for(i=0; p->func1_name && p->func1_name[i]; i++){
if(strmatch(next, p->func1_name[i])){
d->a.func1 = p->func1[i];
d->type = e_func1;
return d;
}
}
for(i=0; p->func2_name && p->func2_name[i]; i++){
if(strmatch(next, p->func2_name[i])){
d->a.func2 = p->func2[i];
d->type = e_func2;
return d;
}
}
*p->error = "unknown function";
ff_free_expr(d);
return NULL;
}
return d;
}
static AVExpr * new_eval_expr(int type, int value, AVExpr *p0, AVExpr *p1){
AVExpr * e = av_mallocz(sizeof(AVExpr));
if (!e)
return NULL;
e->type =type ;
e->value =value ;
e->param[0] =p0 ;
e->param[1] =p1 ;
return e;
}
static AVExpr * parse_pow(Parser *p, int *sign){
*sign= (*p->s == '+') - (*p->s == '-');
p->s += *sign&1;
return parse_primary(p);
}
static AVExpr * parse_factor(Parser *p){
int sign, sign2;
AVExpr * e = parse_pow(p, &sign);
while(p->s[0]=='^'){
p->s++;
e= new_eval_expr(e_pow, 1, e, parse_pow(p, &sign2));
if (!e)
return NULL;
if (e->param[1]) e->param[1]->value *= (sign2|1);
}
if (e) e->value *= (sign|1);
return e;
}
static AVExpr * parse_term(Parser *p){
AVExpr * e = parse_factor(p);
while(p->s[0]=='*' || p->s[0]=='/'){
int c= *p->s++;
e= new_eval_expr(c == '*' ? e_mul : e_div, 1, e, parse_factor(p));
if (!e)
return NULL;
}
return e;
}
static AVExpr * parse_subexpr(Parser *p) {
AVExpr * e = parse_term(p);
while(*p->s == '+' || *p->s == '-') {
e= new_eval_expr(e_add, 1, e, parse_term(p));
if (!e)
return NULL;
};
return e;
}
static AVExpr * parse_expr(Parser *p) {
AVExpr * e;
if(p->stack_index <= 0) //protect against stack overflows
return NULL;
p->stack_index--;
e = parse_subexpr(p);
while(*p->s == ';') {
p->s++;
e= new_eval_expr(e_last, 1, e, parse_subexpr(p));
if (!e)
return NULL;
};
p->stack_index++;
return e;
}
static int verify_expr(AVExpr * e) {
if (!e) return 0;
switch (e->type) {
case e_value:
case e_const: return 1;
case e_func0:
case e_func1:
case e_squish:
case e_ld:
case e_gauss: return verify_expr(e->param[0]);
default: return verify_expr(e->param[0]) && verify_expr(e->param[1]);
}
}
AVExpr *ff_parse_expr(const char *s, const char * const *const_name,
double (* const *func1)(void *, double), const char * const *func1_name,
double (* const *func2)(void *, double, double), const char * const *func2_name,
const char **error){
Parser p;
AVExpr *e = NULL;
char *w = av_malloc(strlen(s) + 1);
char *wp = w;
if (!w)
goto end;
while (*s)
if (!isspace(*s++)) *wp++ = s[-1];
*wp++ = 0;
p.stack_index=100;
p.s= w;
p.const_name = const_name;
p.func1 = func1;
p.func1_name = func1_name;
p.func2 = func2;
p.func2_name = func2_name;
p.error= error;
e = parse_expr(&p);
if (!verify_expr(e)) {
ff_free_expr(e);
e = NULL;
}
end:
av_free(w);
return e;
}
double ff_eval_expr(AVExpr * e, const double *const_value, void *opaque) {
Parser p;
p.const_value= const_value;
p.opaque = opaque;
return eval_expr(&p, e);
}
double ff_parse_and_eval_expr(const char *s, const double *const_value, const char * const *const_name,
double (* const *func1)(void *, double), const char * const *func1_name,
double (* const *func2)(void *, double, double), const char * const *func2_name,
void *opaque, const char **error){
AVExpr * e = ff_parse_expr(s, const_name, func1, func1_name, func2, func2_name, error);
double d;
if (!e) return NAN;
d = ff_eval_expr(e, const_value, opaque);
ff_free_expr(e);
return d;
}
#ifdef TEST
#undef printf
static double const_values[]={
M_PI,
M_E,
0
};
static const char *const_names[]={
"PI",
"E",
0
};
int main(void){
int i;
printf("%f == 12.7\n", ff_parse_and_eval_expr("1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", const_values, const_names, NULL, NULL, NULL, NULL, NULL, NULL));
printf("%f == 0.931322575\n", ff_parse_and_eval_expr("80G/80Gi", const_values, const_names, NULL, NULL, NULL, NULL, NULL, NULL));
for(i=0; i<1050; i++){
START_TIMER
ff_parse_and_eval_expr("1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", const_values, const_names, NULL, NULL, NULL, NULL, NULL, NULL);
STOP_TIMER("ff_parse_and_eval_expr")
}
return 0;
}
#endif
| 123linslouis-android-video-cutter | jni/libavcodec/eval.c | C | asf20 | 13,384 |
/*
* JPEG-LS decoder
* Copyright (c) 2003 Michael Niedermayer
* Copyright (c) 2006 Konstantin Shishkov
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* JPEG-LS decoder.
*/
#include "avcodec.h"
#include "get_bits.h"
#include "golomb.h"
#include "mathops.h"
#include "mjpeg.h"
#include "mjpegdec.h"
#include "jpegls.h"
#include "jpeglsdec.h"
/*
* Uncomment this to significantly speed up decoding of broken JPEG-LS
* (or test broken JPEG-LS decoder) and slow down ordinary decoding a bit.
*
* There is no Golomb code with length >= 32 bits possible, so check and
* avoid situation of 32 zeros, FFmpeg Golomb decoder is painfully slow
* on this errors.
*/
//#define JLS_BROKEN
/**
* Decode LSE block with initialization parameters
*/
int ff_jpegls_decode_lse(MJpegDecodeContext *s)
{
int len, id;
/* XXX: verify len field validity */
len = get_bits(&s->gb, 16);
id = get_bits(&s->gb, 8);
switch(id){
case 1:
s->maxval= get_bits(&s->gb, 16);
s->t1= get_bits(&s->gb, 16);
s->t2= get_bits(&s->gb, 16);
s->t3= get_bits(&s->gb, 16);
s->reset= get_bits(&s->gb, 16);
// ff_jpegls_reset_coding_parameters(s, 0);
//FIXME quant table?
break;
case 2:
case 3:
av_log(s->avctx, AV_LOG_ERROR, "palette not supported\n");
return -1;
case 4:
av_log(s->avctx, AV_LOG_ERROR, "oversize image not supported\n");
return -1;
default:
av_log(s->avctx, AV_LOG_ERROR, "invalid id %d\n", id);
return -1;
}
// av_log(s->avctx, AV_LOG_DEBUG, "ID=%i, T=%i,%i,%i\n", id, s->t1, s->t2, s->t3);
return 0;
}
/**
* Get context-dependent Golomb code, decode it and update context
*/
static inline int ls_get_code_regular(GetBitContext *gb, JLSState *state, int Q){
int k, ret;
for(k = 0; (state->N[Q] << k) < state->A[Q]; k++);
#ifdef JLS_BROKEN
if(!show_bits_long(gb, 32))return -1;
#endif
ret = get_ur_golomb_jpegls(gb, k, state->limit, state->qbpp);
/* decode mapped error */
if(ret & 1)
ret = -((ret + 1) >> 1);
else
ret >>= 1;
/* for NEAR=0, k=0 and 2*B[Q] <= - N[Q] mapping is reversed */
if(!state->near && !k && (2 * state->B[Q] <= -state->N[Q]))
ret = -(ret + 1);
ret= ff_jpegls_update_state_regular(state, Q, ret);
return ret;
}
/**
* Get Golomb code, decode it and update state for run termination
*/
static inline int ls_get_code_runterm(GetBitContext *gb, JLSState *state, int RItype, int limit_add){
int k, ret, temp, map;
int Q = 365 + RItype;
temp= state->A[Q];
if(RItype)
temp += state->N[Q] >> 1;
for(k = 0; (state->N[Q] << k) < temp; k++);
#ifdef JLS_BROKEN
if(!show_bits_long(gb, 32))return -1;
#endif
ret = get_ur_golomb_jpegls(gb, k, state->limit - limit_add - 1, state->qbpp);
/* decode mapped error */
map = 0;
if(!k && (RItype || ret) && (2 * state->B[Q] < state->N[Q]))
map = 1;
ret += RItype + map;
if(ret & 1){
ret = map - ((ret + 1) >> 1);
state->B[Q]++;
} else {
ret = ret >> 1;
}
/* update state */
state->A[Q] += FFABS(ret) - RItype;
ret *= state->twonear;
ff_jpegls_downscale_state(state, Q);
return ret;
}
/**
* Decode one line of image
*/
static inline void ls_decode_line(JLSState *state, MJpegDecodeContext *s, void *last, void *dst, int last2, int w, int stride, int comp, int bits){
int i, x = 0;
int Ra, Rb, Rc, Rd;
int D0, D1, D2;
while(x < w) {
int err, pred;
/* compute gradients */
Ra = x ? R(dst, x - stride) : R(last, x);
Rb = R(last, x);
Rc = x ? R(last, x - stride) : last2;
Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride);
D0 = Rd - Rb;
D1 = Rb - Rc;
D2 = Rc - Ra;
/* run mode */
if((FFABS(D0) <= state->near) && (FFABS(D1) <= state->near) && (FFABS(D2) <= state->near)) {
int r;
int RItype;
/* decode full runs while available */
while(get_bits1(&s->gb)) {
int r;
r = 1 << ff_log2_run[state->run_index[comp]];
if(x + r * stride > w) {
r = (w - x) / stride;
}
for(i = 0; i < r; i++) {
W(dst, x, Ra);
x += stride;
}
/* if EOL reached, we stop decoding */
if(r != (1 << ff_log2_run[state->run_index[comp]]))
return;
if(state->run_index[comp] < 31)
state->run_index[comp]++;
if(x + stride > w)
return;
}
/* decode aborted run */
r = ff_log2_run[state->run_index[comp]];
if(r)
r = get_bits_long(&s->gb, r);
for(i = 0; i < r; i++) {
W(dst, x, Ra);
x += stride;
}
/* decode run termination value */
Rb = R(last, x);
RItype = (FFABS(Ra - Rb) <= state->near) ? 1 : 0;
err = ls_get_code_runterm(&s->gb, state, RItype, ff_log2_run[state->run_index[comp]]);
if(state->run_index[comp])
state->run_index[comp]--;
if(state->near && RItype){
pred = Ra + err;
} else {
if(Rb < Ra)
pred = Rb - err;
else
pred = Rb + err;
}
} else { /* regular mode */
int context, sign;
context = ff_jpegls_quantize(state, D0) * 81 + ff_jpegls_quantize(state, D1) * 9 + ff_jpegls_quantize(state, D2);
pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
if(context < 0){
context = -context;
sign = 1;
}else{
sign = 0;
}
if(sign){
pred = av_clip(pred - state->C[context], 0, state->maxval);
err = -ls_get_code_regular(&s->gb, state, context);
} else {
pred = av_clip(pred + state->C[context], 0, state->maxval);
err = ls_get_code_regular(&s->gb, state, context);
}
/* we have to do something more for near-lossless coding */
pred += err;
}
if(state->near){
if(pred < -state->near)
pred += state->range * state->twonear;
else if(pred > state->maxval + state->near)
pred -= state->range * state->twonear;
pred = av_clip(pred, 0, state->maxval);
}
pred &= state->maxval;
W(dst, x, pred);
x += stride;
}
}
int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near, int point_transform, int ilv){
int i, t = 0;
uint8_t *zero, *last, *cur;
JLSState *state;
int off = 0, stride = 1, width, shift;
zero = av_mallocz(s->picture.linesize[0]);
last = zero;
cur = s->picture.data[0];
state = av_mallocz(sizeof(JLSState));
/* initialize JPEG-LS state from JPEG parameters */
state->near = near;
state->bpp = (s->bits < 2) ? 2 : s->bits;
state->maxval = s->maxval;
state->T1 = s->t1;
state->T2 = s->t2;
state->T3 = s->t3;
state->reset = s->reset;
ff_jpegls_reset_coding_parameters(state, 0);
ff_jpegls_init_state(state);
if(s->bits <= 8)
shift = point_transform + (8 - s->bits);
else
shift = point_transform + (16 - s->bits);
// av_log(s->avctx, AV_LOG_DEBUG, "JPEG-LS params: %ix%i NEAR=%i MV=%i T(%i,%i,%i) RESET=%i, LIMIT=%i, qbpp=%i, RANGE=%i\n",s->width,s->height,state->near,state->maxval,state->T1,state->T2,state->T3,state->reset,state->limit,state->qbpp, state->range);
// av_log(s->avctx, AV_LOG_DEBUG, "JPEG params: ILV=%i Pt=%i BPP=%i, scan = %i\n", ilv, point_transform, s->bits, s->cur_scan);
if(ilv == 0) { /* separate planes */
off = s->cur_scan - 1;
stride = (s->nb_components > 1) ? 3 : 1;
width = s->width * stride;
cur += off;
for(i = 0; i < s->height; i++) {
if(s->bits <= 8){
ls_decode_line(state, s, last, cur, t, width, stride, off, 8);
t = last[0];
}else{
ls_decode_line(state, s, last, cur, t, width, stride, off, 16);
t = *((uint16_t*)last);
}
last = cur;
cur += s->picture.linesize[0];
if (s->restart_interval && !--s->restart_count) {
align_get_bits(&s->gb);
skip_bits(&s->gb, 16); /* skip RSTn */
}
}
} else if(ilv == 1) { /* line interleaving */
int j;
int Rc[3] = {0, 0, 0};
memset(cur, 0, s->picture.linesize[0]);
width = s->width * 3;
for(i = 0; i < s->height; i++) {
for(j = 0; j < 3; j++) {
ls_decode_line(state, s, last + j, cur + j, Rc[j], width, 3, j, 8);
Rc[j] = last[j];
if (s->restart_interval && !--s->restart_count) {
align_get_bits(&s->gb);
skip_bits(&s->gb, 16); /* skip RSTn */
}
}
last = cur;
cur += s->picture.linesize[0];
}
} else if(ilv == 2) { /* sample interleaving */
av_log(s->avctx, AV_LOG_ERROR, "Sample interleaved images are not supported.\n");
av_free(state);
av_free(zero);
return -1;
}
if(shift){ /* we need to do point transform or normalize samples */
int x, w;
w = s->width * s->nb_components;
if(s->bits <= 8){
uint8_t *src = s->picture.data[0];
for(i = 0; i < s->height; i++){
for(x = off; x < w; x+= stride){
src[x] <<= shift;
}
src += s->picture.linesize[0];
}
}else{
uint16_t *src = (uint16_t*) s->picture.data[0];
for(i = 0; i < s->height; i++){
for(x = 0; x < w; x++){
src[x] <<= shift;
}
src += s->picture.linesize[0]/2;
}
}
}
av_free(state);
av_free(zero);
return 0;
}
AVCodec jpegls_decoder = {
"jpegls",
AVMEDIA_TYPE_VIDEO,
CODEC_ID_JPEGLS,
sizeof(MJpegDecodeContext),
ff_mjpeg_decode_init,
NULL,
ff_mjpeg_decode_end,
ff_mjpeg_decode_frame,
CODEC_CAP_DR1,
.long_name = NULL_IF_CONFIG_SMALL("JPEG-LS"),
};
| 123linslouis-android-video-cutter | jni/libavcodec/jpeglsdec.c | C | asf20 | 11,393 |
/*
* ITU H263 bitstream decoder
* Copyright (c) 2000,2001 Fabrice Bellard
* H263+ support.
* Copyright (c) 2001 Juan J. Sierralta P
* Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* h263 decoder.
*/
//#define DEBUG
#include <limits.h>
#include "dsputil.h"
#include "avcodec.h"
#include "mpegvideo.h"
#include "h263.h"
#include "mathops.h"
#include "unary.h"
#include "flv.h"
#include "mpeg4video.h"
//#undef NDEBUG
//#include <assert.h>
// The defines below define the number of bits that are read at once for
// reading vlc values. Changing these may improve speed and data cache needs
// be aware though that decreasing them may need the number of stages that is
// passed to get_vlc* to be increased.
#define MV_VLC_BITS 9
#define H263_MBTYPE_B_VLC_BITS 6
#define CBPC_B_VLC_BITS 3
static const int h263_mb_type_b_map[15]= {
MB_TYPE_DIRECT2 | MB_TYPE_L0L1,
MB_TYPE_DIRECT2 | MB_TYPE_L0L1 | MB_TYPE_CBP,
MB_TYPE_DIRECT2 | MB_TYPE_L0L1 | MB_TYPE_CBP | MB_TYPE_QUANT,
MB_TYPE_L0 | MB_TYPE_16x16,
MB_TYPE_L0 | MB_TYPE_CBP | MB_TYPE_16x16,
MB_TYPE_L0 | MB_TYPE_CBP | MB_TYPE_QUANT | MB_TYPE_16x16,
MB_TYPE_L1 | MB_TYPE_16x16,
MB_TYPE_L1 | MB_TYPE_CBP | MB_TYPE_16x16,
MB_TYPE_L1 | MB_TYPE_CBP | MB_TYPE_QUANT | MB_TYPE_16x16,
MB_TYPE_L0L1 | MB_TYPE_16x16,
MB_TYPE_L0L1 | MB_TYPE_CBP | MB_TYPE_16x16,
MB_TYPE_L0L1 | MB_TYPE_CBP | MB_TYPE_QUANT | MB_TYPE_16x16,
0, //stuffing
MB_TYPE_INTRA4x4 | MB_TYPE_CBP,
MB_TYPE_INTRA4x4 | MB_TYPE_CBP | MB_TYPE_QUANT,
};
void ff_h263_show_pict_info(MpegEncContext *s){
if(s->avctx->debug&FF_DEBUG_PICT_INFO){
av_log(s->avctx, AV_LOG_DEBUG, "qp:%d %c size:%d rnd:%d%s%s%s%s%s%s%s%s%s %d/%d\n",
s->qscale, av_get_pict_type_char(s->pict_type),
s->gb.size_in_bits, 1-s->no_rounding,
s->obmc ? " AP" : "",
s->umvplus ? " UMV" : "",
s->h263_long_vectors ? " LONG" : "",
s->h263_plus ? " +" : "",
s->h263_aic ? " AIC" : "",
s->alt_inter_vlc ? " AIV" : "",
s->modified_quant ? " MQ" : "",
s->loop_filter ? " LOOP" : "",
s->h263_slice_structured ? " SS" : "",
s->avctx->time_base.den, s->avctx->time_base.num
);
}
}
/***********************************************/
/* decoding */
VLC ff_h263_intra_MCBPC_vlc;
VLC ff_h263_inter_MCBPC_vlc;
VLC ff_h263_cbpy_vlc;
static VLC mv_vlc;
static VLC h263_mbtype_b_vlc;
static VLC cbpc_b_vlc;
/* init vlcs */
/* XXX: find a better solution to handle static init */
void h263_decode_init_vlc(MpegEncContext *s)
{
static int done = 0;
if (!done) {
done = 1;
INIT_VLC_STATIC(&ff_h263_intra_MCBPC_vlc, INTRA_MCBPC_VLC_BITS, 9,
ff_h263_intra_MCBPC_bits, 1, 1,
ff_h263_intra_MCBPC_code, 1, 1, 72);
INIT_VLC_STATIC(&ff_h263_inter_MCBPC_vlc, INTER_MCBPC_VLC_BITS, 28,
ff_h263_inter_MCBPC_bits, 1, 1,
ff_h263_inter_MCBPC_code, 1, 1, 198);
INIT_VLC_STATIC(&ff_h263_cbpy_vlc, CBPY_VLC_BITS, 16,
&ff_h263_cbpy_tab[0][1], 2, 1,
&ff_h263_cbpy_tab[0][0], 2, 1, 64);
INIT_VLC_STATIC(&mv_vlc, MV_VLC_BITS, 33,
&mvtab[0][1], 2, 1,
&mvtab[0][0], 2, 1, 538);
init_rl(&ff_h263_rl_inter, ff_h263_static_rl_table_store[0]);
init_rl(&rl_intra_aic, ff_h263_static_rl_table_store[1]);
INIT_VLC_RL(ff_h263_rl_inter, 554);
INIT_VLC_RL(rl_intra_aic, 554);
INIT_VLC_STATIC(&h263_mbtype_b_vlc, H263_MBTYPE_B_VLC_BITS, 15,
&h263_mbtype_b_tab[0][1], 2, 1,
&h263_mbtype_b_tab[0][0], 2, 1, 80);
INIT_VLC_STATIC(&cbpc_b_vlc, CBPC_B_VLC_BITS, 4,
&cbpc_b_tab[0][1], 2, 1,
&cbpc_b_tab[0][0], 2, 1, 8);
}
}
int ff_h263_decode_mba(MpegEncContext *s)
{
int i, mb_pos;
for(i=0; i<6; i++){
if(s->mb_num-1 <= ff_mba_max[i]) break;
}
mb_pos= get_bits(&s->gb, ff_mba_length[i]);
s->mb_x= mb_pos % s->mb_width;
s->mb_y= mb_pos / s->mb_width;
return mb_pos;
}
/**
* decodes the group of blocks header or slice header.
* @return <0 if an error occurred
*/
static int h263_decode_gob_header(MpegEncContext *s)
{
unsigned int val, gfid, gob_number;
int left;
/* Check for GOB Start Code */
val = show_bits(&s->gb, 16);
if(val)
return -1;
/* We have a GBSC probably with GSTUFF */
skip_bits(&s->gb, 16); /* Drop the zeros */
left= get_bits_left(&s->gb);
//MN: we must check the bits left or we might end in a infinite loop (or segfault)
for(;left>13; left--){
if(get_bits1(&s->gb)) break; /* Seek the '1' bit */
}
if(left<=13)
return -1;
if(s->h263_slice_structured){
if(get_bits1(&s->gb)==0)
return -1;
ff_h263_decode_mba(s);
if(s->mb_num > 1583)
if(get_bits1(&s->gb)==0)
return -1;
s->qscale = get_bits(&s->gb, 5); /* SQUANT */
if(get_bits1(&s->gb)==0)
return -1;
gfid = get_bits(&s->gb, 2); /* GFID */
}else{
gob_number = get_bits(&s->gb, 5); /* GN */
s->mb_x= 0;
s->mb_y= s->gob_index* gob_number;
gfid = get_bits(&s->gb, 2); /* GFID */
s->qscale = get_bits(&s->gb, 5); /* GQUANT */
}
if(s->mb_y >= s->mb_height)
return -1;
if(s->qscale==0)
return -1;
return 0;
}
/**
* finds the next resync_marker
* @param p pointer to buffer to scan
* @param end pointer to the end of the buffer
* @return pointer to the next resync_marker, or end if none was found
*/
const uint8_t *ff_h263_find_resync_marker(const uint8_t *restrict p, const uint8_t * restrict end)
{
assert(p < end);
end-=2;
p++;
for(;p<end; p+=2){
if(!*p){
if (!p[-1] && p[1]) return p - 1;
else if(!p[ 1] && p[2]) return p;
}
}
return end+2;
}
/**
* decodes the group of blocks / video packet header.
* @return bit position of the resync_marker, or <0 if none was found
*/
int ff_h263_resync(MpegEncContext *s){
int left, pos, ret;
if(s->codec_id==CODEC_ID_MPEG4){
skip_bits1(&s->gb);
align_get_bits(&s->gb);
}
if(show_bits(&s->gb, 16)==0){
pos= get_bits_count(&s->gb);
if(CONFIG_MPEG4_DECODER && s->codec_id==CODEC_ID_MPEG4)
ret= mpeg4_decode_video_packet_header(s);
else
ret= h263_decode_gob_header(s);
if(ret>=0)
return pos;
}
//OK, it's not where it is supposed to be ...
s->gb= s->last_resync_gb;
align_get_bits(&s->gb);
left= get_bits_left(&s->gb);
for(;left>16+1+5+5; left-=8){
if(show_bits(&s->gb, 16)==0){
GetBitContext bak= s->gb;
pos= get_bits_count(&s->gb);
if(CONFIG_MPEG4_DECODER && s->codec_id==CODEC_ID_MPEG4)
ret= mpeg4_decode_video_packet_header(s);
else
ret= h263_decode_gob_header(s);
if(ret>=0)
return pos;
s->gb= bak;
}
skip_bits(&s->gb, 8);
}
return -1;
}
int h263_decode_motion(MpegEncContext * s, int pred, int f_code)
{
int code, val, sign, shift, l;
code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
if (code == 0)
return pred;
if (code < 0)
return 0xffff;
sign = get_bits1(&s->gb);
shift = f_code - 1;
val = code;
if (shift) {
val = (val - 1) << shift;
val |= get_bits(&s->gb, shift);
val++;
}
if (sign)
val = -val;
val += pred;
/* modulo decoding */
if (!s->h263_long_vectors) {
l = INT_BIT - 5 - f_code;
val = (val<<l)>>l;
} else {
/* horrible h263 long vector mode */
if (pred < -31 && val < -63)
val += 64;
if (pred > 32 && val > 63)
val -= 64;
}
return val;
}
/* Decodes RVLC of H.263+ UMV */
static int h263p_decode_umotion(MpegEncContext * s, int pred)
{
int code = 0, sign;
if (get_bits1(&s->gb)) /* Motion difference = 0 */
return pred;
code = 2 + get_bits1(&s->gb);
while (get_bits1(&s->gb))
{
code <<= 1;
code += get_bits1(&s->gb);
}
sign = code & 1;
code >>= 1;
code = (sign) ? (pred - code) : (pred + code);
dprintf(s->avctx,"H.263+ UMV Motion = %d\n", code);
return code;
}
/**
* read the next MVs for OBMC. yes this is a ugly hack, feel free to send a patch :)
*/
static void preview_obmc(MpegEncContext *s){
GetBitContext gb= s->gb;
int cbpc, i, pred_x, pred_y, mx, my;
int16_t *mot_val;
const int xy= s->mb_x + 1 + s->mb_y * s->mb_stride;
const int stride= s->b8_stride*2;
for(i=0; i<4; i++)
s->block_index[i]+= 2;
for(i=4; i<6; i++)
s->block_index[i]+= 1;
s->mb_x++;
assert(s->pict_type == FF_P_TYPE);
do{
if (get_bits1(&s->gb)) {
/* skip mb */
mot_val = s->current_picture.motion_val[0][ s->block_index[0] ];
mot_val[0 ]= mot_val[2 ]=
mot_val[0+stride]= mot_val[2+stride]= 0;
mot_val[1 ]= mot_val[3 ]=
mot_val[1+stride]= mot_val[3+stride]= 0;
s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
goto end;
}
cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
}while(cbpc == 20);
if(cbpc & 4){
s->current_picture.mb_type[xy]= MB_TYPE_INTRA;
}else{
get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
if (cbpc & 8) {
if(s->modified_quant){
if(get_bits1(&s->gb)) skip_bits(&s->gb, 1);
else skip_bits(&s->gb, 5);
}else
skip_bits(&s->gb, 2);
}
if ((cbpc & 16) == 0) {
s->current_picture.mb_type[xy]= MB_TYPE_16x16 | MB_TYPE_L0;
/* 16x16 motion prediction */
mot_val= h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
if (s->umvplus)
mx = h263p_decode_umotion(s, pred_x);
else
mx = h263_decode_motion(s, pred_x, 1);
if (s->umvplus)
my = h263p_decode_umotion(s, pred_y);
else
my = h263_decode_motion(s, pred_y, 1);
mot_val[0 ]= mot_val[2 ]=
mot_val[0+stride]= mot_val[2+stride]= mx;
mot_val[1 ]= mot_val[3 ]=
mot_val[1+stride]= mot_val[3+stride]= my;
} else {
s->current_picture.mb_type[xy]= MB_TYPE_8x8 | MB_TYPE_L0;
for(i=0;i<4;i++) {
mot_val = h263_pred_motion(s, i, 0, &pred_x, &pred_y);
if (s->umvplus)
mx = h263p_decode_umotion(s, pred_x);
else
mx = h263_decode_motion(s, pred_x, 1);
if (s->umvplus)
my = h263p_decode_umotion(s, pred_y);
else
my = h263_decode_motion(s, pred_y, 1);
if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
mot_val[0] = mx;
mot_val[1] = my;
}
}
}
end:
for(i=0; i<4; i++)
s->block_index[i]-= 2;
for(i=4; i<6; i++)
s->block_index[i]-= 1;
s->mb_x--;
s->gb= gb;
}
static void h263_decode_dquant(MpegEncContext *s){
static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
if(s->modified_quant){
if(get_bits1(&s->gb))
s->qscale= modified_quant_tab[get_bits1(&s->gb)][ s->qscale ];
else
s->qscale= get_bits(&s->gb, 5);
}else
s->qscale += quant_tab[get_bits(&s->gb, 2)];
ff_set_qscale(s, s->qscale);
}
static int h263_decode_block(MpegEncContext * s, DCTELEM * block,
int n, int coded)
{
int code, level, i, j, last, run;
RLTable *rl = &ff_h263_rl_inter;
const uint8_t *scan_table;
GetBitContext gb= s->gb;
scan_table = s->intra_scantable.permutated;
if (s->h263_aic && s->mb_intra) {
rl = &rl_intra_aic;
i = 0;
if (s->ac_pred) {
if (s->h263_aic_dir)
scan_table = s->intra_v_scantable.permutated; /* left */
else
scan_table = s->intra_h_scantable.permutated; /* top */
}
} else if (s->mb_intra) {
/* DC coef */
if(s->codec_id == CODEC_ID_RV10){
#if CONFIG_RV10_DECODER
if (s->rv10_version == 3 && s->pict_type == FF_I_TYPE) {
int component, diff;
component = (n <= 3 ? 0 : n - 4 + 1);
level = s->last_dc[component];
if (s->rv10_first_dc_coded[component]) {
diff = rv_decode_dc(s, n);
if (diff == 0xffff)
return -1;
level += diff;
level = level & 0xff; /* handle wrap round */
s->last_dc[component] = level;
} else {
s->rv10_first_dc_coded[component] = 1;
}
} else {
level = get_bits(&s->gb, 8);
if (level == 255)
level = 128;
}
#endif
}else{
level = get_bits(&s->gb, 8);
if((level&0x7F) == 0){
av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", level, s->mb_x, s->mb_y);
if(s->error_recognition >= FF_ER_COMPLIANT)
return -1;
}
if (level == 255)
level = 128;
}
block[0] = level;
i = 1;
} else {
i = 0;
}
if (!coded) {
if (s->mb_intra && s->h263_aic)
goto not_coded;
s->block_last_index[n] = i - 1;
return 0;
}
retry:
for(;;) {
code = get_vlc2(&s->gb, rl->vlc.table, TEX_VLC_BITS, 2);
if (code < 0){
av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y);
return -1;
}
if (code == rl->n) {
/* escape */
if (CONFIG_FLV_DECODER && s->h263_flv > 1) {
ff_flv2_decode_ac_esc(&s->gb, &level, &run, &last);
} else {
last = get_bits1(&s->gb);
run = get_bits(&s->gb, 6);
level = (int8_t)get_bits(&s->gb, 8);
if(level == -128){
if (s->codec_id == CODEC_ID_RV10) {
/* XXX: should patch encoder too */
level = get_sbits(&s->gb, 12);
}else{
level = get_bits(&s->gb, 5);
level |= get_sbits(&s->gb, 6)<<5;
}
}
}
} else {
run = rl->table_run[code];
level = rl->table_level[code];
last = code >= rl->last;
if (get_bits1(&s->gb))
level = -level;
}
i += run;
if (i >= 64){
if(s->alt_inter_vlc && rl == &ff_h263_rl_inter && !s->mb_intra){
//Looks like a hack but no, it's the way it is supposed to work ...
rl = &rl_intra_aic;
i = 0;
s->gb= gb;
s->dsp.clear_block(block);
goto retry;
}
av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d i:%d\n", s->mb_x, s->mb_y, s->mb_intra);
return -1;
}
j = scan_table[i];
block[j] = level;
if (last)
break;
i++;
}
not_coded:
if (s->mb_intra && s->h263_aic) {
h263_pred_acdc(s, block, n);
i = 63;
}
s->block_last_index[n] = i;
return 0;
}
static int h263_skip_b_part(MpegEncContext *s, int cbp)
{
LOCAL_ALIGNED_16(DCTELEM, dblock, [64]);
int i, mbi;
/* we have to set s->mb_intra to zero to decode B-part of PB-frame correctly
* but real value should be restored in order to be used later (in OBMC condition)
*/
mbi = s->mb_intra;
s->mb_intra = 0;
for (i = 0; i < 6; i++) {
if (h263_decode_block(s, dblock, i, cbp&32) < 0)
return -1;
cbp+=cbp;
}
s->mb_intra = mbi;
return 0;
}
static int h263_get_modb(GetBitContext *gb, int pb_frame, int *cbpb)
{
int c, mv = 1;
if (pb_frame < 3) { // h.263 Annex G and i263 PB-frame
c = get_bits1(gb);
if (pb_frame == 2 && c)
mv = !get_bits1(gb);
} else { // h.263 Annex M improved PB-frame
mv = get_unary(gb, 0, 4) + 1;
c = mv & 1;
mv = !!(mv & 2);
}
if(c)
*cbpb = get_bits(gb, 6);
return mv;
}
int ff_h263_decode_mb(MpegEncContext *s,
DCTELEM block[6][64])
{
int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
int16_t *mot_val;
const int xy= s->mb_x + s->mb_y * s->mb_stride;
int cbpb = 0, pb_mv_count = 0;
assert(!s->h263_pred);
if (s->pict_type == FF_P_TYPE) {
do{
if (get_bits1(&s->gb)) {
/* skip mb */
s->mb_intra = 0;
for(i=0;i<6;i++)
s->block_last_index[i] = -1;
s->mv_dir = MV_DIR_FORWARD;
s->mv_type = MV_TYPE_16X16;
s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
s->mv[0][0][0] = 0;
s->mv[0][0][1] = 0;
s->mb_skipped = !(s->obmc | s->loop_filter);
goto end;
}
cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
if (cbpc < 0){
av_log(s->avctx, AV_LOG_ERROR, "cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
return -1;
}
}while(cbpc == 20);
s->dsp.clear_blocks(s->block[0]);
dquant = cbpc & 8;
s->mb_intra = ((cbpc & 4) != 0);
if (s->mb_intra) goto intra;
if(s->pb_frame && get_bits1(&s->gb))
pb_mv_count = h263_get_modb(&s->gb, s->pb_frame, &cbpb);
cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
if(s->alt_inter_vlc==0 || (cbpc & 3)!=3)
cbpy ^= 0xF;
cbp = (cbpc & 3) | (cbpy << 2);
if (dquant) {
h263_decode_dquant(s);
}
s->mv_dir = MV_DIR_FORWARD;
if ((cbpc & 16) == 0) {
s->current_picture.mb_type[xy]= MB_TYPE_16x16 | MB_TYPE_L0;
/* 16x16 motion prediction */
s->mv_type = MV_TYPE_16X16;
h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
if (s->umvplus)
mx = h263p_decode_umotion(s, pred_x);
else
mx = h263_decode_motion(s, pred_x, 1);
if (mx >= 0xffff)
return -1;
if (s->umvplus)
my = h263p_decode_umotion(s, pred_y);
else
my = h263_decode_motion(s, pred_y, 1);
if (my >= 0xffff)
return -1;
s->mv[0][0][0] = mx;
s->mv[0][0][1] = my;
if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
} else {
s->current_picture.mb_type[xy]= MB_TYPE_8x8 | MB_TYPE_L0;
s->mv_type = MV_TYPE_8X8;
for(i=0;i<4;i++) {
mot_val = h263_pred_motion(s, i, 0, &pred_x, &pred_y);
if (s->umvplus)
mx = h263p_decode_umotion(s, pred_x);
else
mx = h263_decode_motion(s, pred_x, 1);
if (mx >= 0xffff)
return -1;
if (s->umvplus)
my = h263p_decode_umotion(s, pred_y);
else
my = h263_decode_motion(s, pred_y, 1);
if (my >= 0xffff)
return -1;
s->mv[0][i][0] = mx;
s->mv[0][i][1] = my;
if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
mot_val[0] = mx;
mot_val[1] = my;
}
}
} else if(s->pict_type==FF_B_TYPE) {
int mb_type;
const int stride= s->b8_stride;
int16_t *mot_val0 = s->current_picture.motion_val[0][ 2*(s->mb_x + s->mb_y*stride) ];
int16_t *mot_val1 = s->current_picture.motion_val[1][ 2*(s->mb_x + s->mb_y*stride) ];
// const int mv_xy= s->mb_x + 1 + s->mb_y * s->mb_stride;
//FIXME ugly
mot_val0[0 ]= mot_val0[2 ]= mot_val0[0+2*stride]= mot_val0[2+2*stride]=
mot_val0[1 ]= mot_val0[3 ]= mot_val0[1+2*stride]= mot_val0[3+2*stride]=
mot_val1[0 ]= mot_val1[2 ]= mot_val1[0+2*stride]= mot_val1[2+2*stride]=
mot_val1[1 ]= mot_val1[3 ]= mot_val1[1+2*stride]= mot_val1[3+2*stride]= 0;
do{
mb_type= get_vlc2(&s->gb, h263_mbtype_b_vlc.table, H263_MBTYPE_B_VLC_BITS, 2);
if (mb_type < 0){
av_log(s->avctx, AV_LOG_ERROR, "b mb_type damaged at %d %d\n", s->mb_x, s->mb_y);
return -1;
}
mb_type= h263_mb_type_b_map[ mb_type ];
}while(!mb_type);
s->mb_intra = IS_INTRA(mb_type);
if(HAS_CBP(mb_type)){
s->dsp.clear_blocks(s->block[0]);
cbpc = get_vlc2(&s->gb, cbpc_b_vlc.table, CBPC_B_VLC_BITS, 1);
if(s->mb_intra){
dquant = IS_QUANT(mb_type);
goto intra;
}
cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
if (cbpy < 0){
av_log(s->avctx, AV_LOG_ERROR, "b cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
return -1;
}
if(s->alt_inter_vlc==0 || (cbpc & 3)!=3)
cbpy ^= 0xF;
cbp = (cbpc & 3) | (cbpy << 2);
}else
cbp=0;
assert(!s->mb_intra);
if(IS_QUANT(mb_type)){
h263_decode_dquant(s);
}
if(IS_DIRECT(mb_type)){
s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
mb_type |= ff_mpeg4_set_direct_mv(s, 0, 0);
}else{
s->mv_dir = 0;
s->mv_type= MV_TYPE_16X16;
//FIXME UMV
if(USES_LIST(mb_type, 0)){
int16_t *mot_val= h263_pred_motion(s, 0, 0, &mx, &my);
s->mv_dir = MV_DIR_FORWARD;
mx = h263_decode_motion(s, mx, 1);
my = h263_decode_motion(s, my, 1);
s->mv[0][0][0] = mx;
s->mv[0][0][1] = my;
mot_val[0 ]= mot_val[2 ]= mot_val[0+2*stride]= mot_val[2+2*stride]= mx;
mot_val[1 ]= mot_val[3 ]= mot_val[1+2*stride]= mot_val[3+2*stride]= my;
}
if(USES_LIST(mb_type, 1)){
int16_t *mot_val= h263_pred_motion(s, 0, 1, &mx, &my);
s->mv_dir |= MV_DIR_BACKWARD;
mx = h263_decode_motion(s, mx, 1);
my = h263_decode_motion(s, my, 1);
s->mv[1][0][0] = mx;
s->mv[1][0][1] = my;
mot_val[0 ]= mot_val[2 ]= mot_val[0+2*stride]= mot_val[2+2*stride]= mx;
mot_val[1 ]= mot_val[3 ]= mot_val[1+2*stride]= mot_val[3+2*stride]= my;
}
}
s->current_picture.mb_type[xy]= mb_type;
} else { /* I-Frame */
do{
cbpc = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
if (cbpc < 0){
av_log(s->avctx, AV_LOG_ERROR, "I cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
return -1;
}
}while(cbpc == 8);
s->dsp.clear_blocks(s->block[0]);
dquant = cbpc & 4;
s->mb_intra = 1;
intra:
s->current_picture.mb_type[xy]= MB_TYPE_INTRA;
if (s->h263_aic) {
s->ac_pred = get_bits1(&s->gb);
if(s->ac_pred){
s->current_picture.mb_type[xy]= MB_TYPE_INTRA | MB_TYPE_ACPRED;
s->h263_aic_dir = get_bits1(&s->gb);
}
}else
s->ac_pred = 0;
if(s->pb_frame && get_bits1(&s->gb))
pb_mv_count = h263_get_modb(&s->gb, s->pb_frame, &cbpb);
cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
if(cbpy<0){
av_log(s->avctx, AV_LOG_ERROR, "I cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
return -1;
}
cbp = (cbpc & 3) | (cbpy << 2);
if (dquant) {
h263_decode_dquant(s);
}
pb_mv_count += !!s->pb_frame;
}
while(pb_mv_count--){
h263_decode_motion(s, 0, 1);
h263_decode_motion(s, 0, 1);
}
/* decode each block */
for (i = 0; i < 6; i++) {
if (h263_decode_block(s, block[i], i, cbp&32) < 0)
return -1;
cbp+=cbp;
}
if(s->pb_frame && h263_skip_b_part(s, cbpb) < 0)
return -1;
if(s->obmc && !s->mb_intra){
if(s->pict_type == FF_P_TYPE && s->mb_x+1<s->mb_width && s->mb_num_left != 1)
preview_obmc(s);
}
end:
/* per-MB end of slice check */
{
int v= show_bits(&s->gb, 16);
if(get_bits_count(&s->gb) + 16 > s->gb.size_in_bits){
v>>= get_bits_count(&s->gb) + 16 - s->gb.size_in_bits;
}
if(v==0)
return SLICE_END;
}
return SLICE_OK;
}
/* most is hardcoded. should extend to handle all h263 streams */
int h263_decode_picture_header(MpegEncContext *s)
{
int format, width, height, i;
uint32_t startcode;
align_get_bits(&s->gb);
startcode= get_bits(&s->gb, 22-8);
for(i= get_bits_left(&s->gb); i>24; i-=8) {
startcode = ((startcode << 8) | get_bits(&s->gb, 8)) & 0x003FFFFF;
if(startcode == 0x20)
break;
}
if (startcode != 0x20) {
av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
return -1;
}
/* temporal reference */
i = get_bits(&s->gb, 8); /* picture timestamp */
if( (s->picture_number&~0xFF)+i < s->picture_number)
i+= 256;
s->current_picture_ptr->pts=
s->picture_number= (s->picture_number&~0xFF) + i;
/* PTYPE starts here */
if (get_bits1(&s->gb) != 1) {
/* marker */
av_log(s->avctx, AV_LOG_ERROR, "Bad marker\n");
return -1;
}
if (get_bits1(&s->gb) != 0) {
av_log(s->avctx, AV_LOG_ERROR, "Bad H263 id\n");
return -1; /* h263 id */
}
skip_bits1(&s->gb); /* split screen off */
skip_bits1(&s->gb); /* camera off */
skip_bits1(&s->gb); /* freeze picture release off */
format = get_bits(&s->gb, 3);
/*
0 forbidden
1 sub-QCIF
10 QCIF
7 extended PTYPE (PLUSPTYPE)
*/
if (format != 7 && format != 6) {
s->h263_plus = 0;
/* H.263v1 */
width = h263_format[format][0];
height = h263_format[format][1];
if (!width)
return -1;
s->pict_type = FF_I_TYPE + get_bits1(&s->gb);
s->h263_long_vectors = get_bits1(&s->gb);
if (get_bits1(&s->gb) != 0) {
av_log(s->avctx, AV_LOG_ERROR, "H263 SAC not supported\n");
return -1; /* SAC: off */
}
s->obmc= get_bits1(&s->gb); /* Advanced prediction mode */
s->unrestricted_mv = s->h263_long_vectors || s->obmc;
s->pb_frame = get_bits1(&s->gb);
s->chroma_qscale= s->qscale = get_bits(&s->gb, 5);
skip_bits1(&s->gb); /* Continuous Presence Multipoint mode: off */
s->width = width;
s->height = height;
s->avctx->sample_aspect_ratio= (AVRational){12,11};
s->avctx->time_base= (AVRational){1001, 30000};
} else {
int ufep;
/* H.263v2 */
s->h263_plus = 1;
ufep = get_bits(&s->gb, 3); /* Update Full Extended PTYPE */
/* ufep other than 0 and 1 are reserved */
if (ufep == 1) {
/* OPPTYPE */
format = get_bits(&s->gb, 3);
dprintf(s->avctx, "ufep=1, format: %d\n", format);
s->custom_pcf= get_bits1(&s->gb);
s->umvplus = get_bits1(&s->gb); /* Unrestricted Motion Vector */
if (get_bits1(&s->gb) != 0) {
av_log(s->avctx, AV_LOG_ERROR, "Syntax-based Arithmetic Coding (SAC) not supported\n");
}
s->obmc= get_bits1(&s->gb); /* Advanced prediction mode */
s->h263_aic = get_bits1(&s->gb); /* Advanced Intra Coding (AIC) */
s->loop_filter= get_bits1(&s->gb);
s->unrestricted_mv = s->umvplus || s->obmc || s->loop_filter;
s->h263_slice_structured= get_bits1(&s->gb);
if (get_bits1(&s->gb) != 0) {
av_log(s->avctx, AV_LOG_ERROR, "Reference Picture Selection not supported\n");
}
if (get_bits1(&s->gb) != 0) {
av_log(s->avctx, AV_LOG_ERROR, "Independent Segment Decoding not supported\n");
}
s->alt_inter_vlc= get_bits1(&s->gb);
s->modified_quant= get_bits1(&s->gb);
if(s->modified_quant)
s->chroma_qscale_table= ff_h263_chroma_qscale_table;
skip_bits(&s->gb, 1); /* Prevent start code emulation */
skip_bits(&s->gb, 3); /* Reserved */
} else if (ufep != 0) {
av_log(s->avctx, AV_LOG_ERROR, "Bad UFEP type (%d)\n", ufep);
return -1;
}
/* MPPTYPE */
s->pict_type = get_bits(&s->gb, 3);
switch(s->pict_type){
case 0: s->pict_type= FF_I_TYPE;break;
case 1: s->pict_type= FF_P_TYPE;break;
case 2: s->pict_type= FF_P_TYPE;s->pb_frame = 3;break;
case 3: s->pict_type= FF_B_TYPE;break;
case 7: s->pict_type= FF_I_TYPE;break; //ZYGO
default:
return -1;
}
skip_bits(&s->gb, 2);
s->no_rounding = get_bits1(&s->gb);
skip_bits(&s->gb, 4);
/* Get the picture dimensions */
if (ufep) {
if (format == 6) {
/* Custom Picture Format (CPFMT) */
s->aspect_ratio_info = get_bits(&s->gb, 4);
dprintf(s->avctx, "aspect: %d\n", s->aspect_ratio_info);
/* aspect ratios:
0 - forbidden
1 - 1:1
2 - 12:11 (CIF 4:3)
3 - 10:11 (525-type 4:3)
4 - 16:11 (CIF 16:9)
5 - 40:33 (525-type 16:9)
6-14 - reserved
*/
width = (get_bits(&s->gb, 9) + 1) * 4;
skip_bits1(&s->gb);
height = get_bits(&s->gb, 9) * 4;
dprintf(s->avctx, "\nH.263+ Custom picture: %dx%d\n",width,height);
if (s->aspect_ratio_info == FF_ASPECT_EXTENDED) {
/* aspected dimensions */
s->avctx->sample_aspect_ratio.num= get_bits(&s->gb, 8);
s->avctx->sample_aspect_ratio.den= get_bits(&s->gb, 8);
}else{
s->avctx->sample_aspect_ratio= ff_h263_pixel_aspect[s->aspect_ratio_info];
}
} else {
width = h263_format[format][0];
height = h263_format[format][1];
s->avctx->sample_aspect_ratio= (AVRational){12,11};
}
if ((width == 0) || (height == 0))
return -1;
s->width = width;
s->height = height;
if(s->custom_pcf){
int gcd;
s->avctx->time_base.den= 1800000;
s->avctx->time_base.num= 1000 + get_bits1(&s->gb);
s->avctx->time_base.num*= get_bits(&s->gb, 7);
if(s->avctx->time_base.num == 0){
av_log(s, AV_LOG_ERROR, "zero framerate\n");
return -1;
}
gcd= av_gcd(s->avctx->time_base.den, s->avctx->time_base.num);
s->avctx->time_base.den /= gcd;
s->avctx->time_base.num /= gcd;
}else{
s->avctx->time_base= (AVRational){1001, 30000};
}
}
if(s->custom_pcf){
skip_bits(&s->gb, 2); //extended Temporal reference
}
if (ufep) {
if (s->umvplus) {
if(get_bits1(&s->gb)==0) /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
skip_bits1(&s->gb);
}
if(s->h263_slice_structured){
if (get_bits1(&s->gb) != 0) {
av_log(s->avctx, AV_LOG_ERROR, "rectangular slices not supported\n");
}
if (get_bits1(&s->gb) != 0) {
av_log(s->avctx, AV_LOG_ERROR, "unordered slices not supported\n");
}
}
}
s->qscale = get_bits(&s->gb, 5);
}
s->mb_width = (s->width + 15) / 16;
s->mb_height = (s->height + 15) / 16;
s->mb_num = s->mb_width * s->mb_height;
if (s->pb_frame) {
skip_bits(&s->gb, 3); /* Temporal reference for B-pictures */
if (s->custom_pcf)
skip_bits(&s->gb, 2); //extended Temporal reference
skip_bits(&s->gb, 2); /* Quantization information for B-pictures */
}
/* PEI */
while (get_bits1(&s->gb) != 0) {
skip_bits(&s->gb, 8);
}
if(s->h263_slice_structured){
if (get_bits1(&s->gb) != 1) {
av_log(s->avctx, AV_LOG_ERROR, "SEPB1 marker missing\n");
return -1;
}
ff_h263_decode_mba(s);
if (get_bits1(&s->gb) != 1) {
av_log(s->avctx, AV_LOG_ERROR, "SEPB2 marker missing\n");
return -1;
}
}
s->f_code = 1;
if(s->h263_aic){
s->y_dc_scale_table=
s->c_dc_scale_table= ff_aic_dc_scale_table;
}else{
s->y_dc_scale_table=
s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
}
ff_h263_show_pict_info(s);
if (s->pict_type == FF_I_TYPE && s->codec_tag == AV_RL32("ZYGO")){
int i,j;
for(i=0; i<85; i++) av_log(s->avctx, AV_LOG_DEBUG, "%d", get_bits1(&s->gb));
av_log(s->avctx, AV_LOG_DEBUG, "\n");
for(i=0; i<13; i++){
for(j=0; j<3; j++){
int v= get_bits(&s->gb, 8);
v |= get_sbits(&s->gb, 8)<<8;
av_log(s->avctx, AV_LOG_DEBUG, " %5d", v);
}
av_log(s->avctx, AV_LOG_DEBUG, "\n");
}
for(i=0; i<50; i++) av_log(s->avctx, AV_LOG_DEBUG, "%d", get_bits1(&s->gb));
}
return 0;
}
| 123linslouis-android-video-cutter | jni/libavcodec/ituh263dec.c | C | asf20 | 36,432 |
/*
* MDCT/IMDCT transforms
* Copyright (c) 2002 Fabrice Bellard
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <string.h>
#include "libavutil/common.h"
#include "libavutil/mathematics.h"
#include "fft.h"
/**
* @file
* MDCT/IMDCT transforms.
*/
// Generate a Kaiser-Bessel Derived Window.
#define BESSEL_I0_ITER 50 // default: 50 iterations of Bessel I0 approximation
av_cold void ff_kbd_window_init(float *window, float alpha, int n)
{
int i, j;
double sum = 0.0, bessel, tmp;
double local_window[n];
double alpha2 = (alpha * M_PI / n) * (alpha * M_PI / n);
for (i = 0; i < n; i++) {
tmp = i * (n - i) * alpha2;
bessel = 1.0;
for (j = BESSEL_I0_ITER; j > 0; j--)
bessel = bessel * tmp / (j * j) + 1;
sum += bessel;
local_window[i] = sum;
}
sum++;
for (i = 0; i < n; i++)
window[i] = sqrt(local_window[i] / sum);
}
#include "mdct_tablegen.h"
/**
* init MDCT or IMDCT computation.
*/
av_cold int ff_mdct_init(FFTContext *s, int nbits, int inverse, double scale)
{
int n, n4, i;
double alpha, theta;
int tstep;
memset(s, 0, sizeof(*s));
n = 1 << nbits;
s->mdct_bits = nbits;
s->mdct_size = n;
n4 = n >> 2;
s->permutation = FF_MDCT_PERM_NONE;
if (ff_fft_init(s, s->mdct_bits - 2, inverse) < 0)
goto fail;
s->tcos = av_malloc(n/2 * sizeof(FFTSample));
if (!s->tcos)
goto fail;
switch (s->permutation) {
case FF_MDCT_PERM_NONE:
s->tsin = s->tcos + n4;
tstep = 1;
break;
case FF_MDCT_PERM_INTERLEAVE:
s->tsin = s->tcos + 1;
tstep = 2;
break;
default:
goto fail;
}
theta = 1.0 / 8.0 + (scale < 0 ? n4 : 0);
scale = sqrt(fabs(scale));
for(i=0;i<n4;i++) {
alpha = 2 * M_PI * (i + theta) / n;
s->tcos[i*tstep] = -cos(alpha) * scale;
s->tsin[i*tstep] = -sin(alpha) * scale;
}
return 0;
fail:
ff_mdct_end(s);
return -1;
}
/* complex multiplication: p = a * b */
#define CMUL(pre, pim, are, aim, bre, bim) \
{\
FFTSample _are = (are);\
FFTSample _aim = (aim);\
FFTSample _bre = (bre);\
FFTSample _bim = (bim);\
(pre) = _are * _bre - _aim * _bim;\
(pim) = _are * _bim + _aim * _bre;\
}
/**
* Compute the middle half of the inverse MDCT of size N = 2^nbits,
* thus excluding the parts that can be derived by symmetry
* @param output N/2 samples
* @param input N/2 samples
*/
void ff_imdct_half_c(FFTContext *s, FFTSample *output, const FFTSample *input)
{
int k, n8, n4, n2, n, j;
const uint16_t *revtab = s->revtab;
const FFTSample *tcos = s->tcos;
const FFTSample *tsin = s->tsin;
const FFTSample *in1, *in2;
FFTComplex *z = (FFTComplex *)output;
n = 1 << s->mdct_bits;
n2 = n >> 1;
n4 = n >> 2;
n8 = n >> 3;
/* pre rotation */
in1 = input;
in2 = input + n2 - 1;
for(k = 0; k < n4; k++) {
j=revtab[k];
CMUL(z[j].re, z[j].im, *in2, *in1, tcos[k], tsin[k]);
in1 += 2;
in2 -= 2;
}
ff_fft_calc(s, z);
/* post rotation + reordering */
for(k = 0; k < n8; k++) {
FFTSample r0, i0, r1, i1;
CMUL(r0, i1, z[n8-k-1].im, z[n8-k-1].re, tsin[n8-k-1], tcos[n8-k-1]);
CMUL(r1, i0, z[n8+k ].im, z[n8+k ].re, tsin[n8+k ], tcos[n8+k ]);
z[n8-k-1].re = r0;
z[n8-k-1].im = i0;
z[n8+k ].re = r1;
z[n8+k ].im = i1;
}
}
/**
* Compute inverse MDCT of size N = 2^nbits
* @param output N samples
* @param input N/2 samples
*/
void ff_imdct_calc_c(FFTContext *s, FFTSample *output, const FFTSample *input)
{
int k;
int n = 1 << s->mdct_bits;
int n2 = n >> 1;
int n4 = n >> 2;
ff_imdct_half_c(s, output+n4, input);
for(k = 0; k < n4; k++) {
output[k] = -output[n2-k-1];
output[n-k-1] = output[n2+k];
}
}
/**
* Compute MDCT of size N = 2^nbits
* @param input N samples
* @param out N/2 samples
*/
void ff_mdct_calc_c(FFTContext *s, FFTSample *out, const FFTSample *input)
{
int i, j, n, n8, n4, n2, n3;
FFTSample re, im;
const uint16_t *revtab = s->revtab;
const FFTSample *tcos = s->tcos;
const FFTSample *tsin = s->tsin;
FFTComplex *x = (FFTComplex *)out;
n = 1 << s->mdct_bits;
n2 = n >> 1;
n4 = n >> 2;
n8 = n >> 3;
n3 = 3 * n4;
/* pre rotation */
for(i=0;i<n8;i++) {
re = -input[2*i+3*n4] - input[n3-1-2*i];
im = -input[n4+2*i] + input[n4-1-2*i];
j = revtab[i];
CMUL(x[j].re, x[j].im, re, im, -tcos[i], tsin[i]);
re = input[2*i] - input[n2-1-2*i];
im = -(input[n2+2*i] + input[n-1-2*i]);
j = revtab[n8 + i];
CMUL(x[j].re, x[j].im, re, im, -tcos[n8 + i], tsin[n8 + i]);
}
ff_fft_calc(s, x);
/* post rotation */
for(i=0;i<n8;i++) {
FFTSample r0, i0, r1, i1;
CMUL(i1, r0, x[n8-i-1].re, x[n8-i-1].im, -tsin[n8-i-1], -tcos[n8-i-1]);
CMUL(i0, r1, x[n8+i ].re, x[n8+i ].im, -tsin[n8+i ], -tcos[n8+i ]);
x[n8-i-1].re = r0;
x[n8-i-1].im = i0;
x[n8+i ].re = r1;
x[n8+i ].im = i1;
}
}
av_cold void ff_mdct_end(FFTContext *s)
{
av_freep(&s->tcos);
ff_fft_end(s);
}
| 123linslouis-android-video-cutter | jni/libavcodec/mdct.c | C | asf20 | 6,020 |
/*
* TwinVQ decoder
* Copyright (c) 2009 Vitor Sessak
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "avcodec.h"
#include "get_bits.h"
#include "dsputil.h"
#include "fft.h"
#include "lsp.h"
#include <math.h>
#include <stdint.h>
#include "twinvq_data.h"
enum FrameType {
FT_SHORT = 0, ///< Short frame (divided in n sub-blocks)
FT_MEDIUM, ///< Medium frame (divided in m<n sub-blocks)
FT_LONG, ///< Long frame (single sub-block + PPC)
FT_PPC, ///< Periodic Peak Component (part of the long frame)
};
/**
* Parameters and tables that are different for each frame type
*/
struct FrameMode {
uint8_t sub; ///< Number subblocks in each frame
const uint16_t *bark_tab;
/** number of distinct bark scale envelope values */
uint8_t bark_env_size;
const int16_t *bark_cb; ///< codebook for the bark scale envelope (BSE)
uint8_t bark_n_coef;///< number of BSE CB coefficients to read
uint8_t bark_n_bit; ///< number of bits of the BSE coefs
//@{
/** main codebooks for spectrum data */
const int16_t *cb0;
const int16_t *cb1;
//@}
uint8_t cb_len_read; ///< number of spectrum coefficients to read
};
/**
* Parameters and tables that are different for every combination of
* bitrate/sample rate
*/
typedef struct {
struct FrameMode fmode[3]; ///< frame type-dependant parameters
uint16_t size; ///< frame size in samples
uint8_t n_lsp; ///< number of lsp coefficients
const float *lspcodebook;
/* number of bits of the different LSP CB coefficients */
uint8_t lsp_bit0;
uint8_t lsp_bit1;
uint8_t lsp_bit2;
uint8_t lsp_split; ///< number of CB entries for the LSP decoding
const int16_t *ppc_shape_cb; ///< PPC shape CB
/** number of the bits for the PPC period value */
uint8_t ppc_period_bit;
uint8_t ppc_shape_bit; ///< number of bits of the PPC shape CB coeffs
uint8_t ppc_shape_len; ///< size of PPC shape CB
uint8_t pgain_bit; ///< bits for PPC gain
/** constant for peak period to peak width conversion */
uint16_t peak_per2wid;
} ModeTab;
static const ModeTab mode_08_08 = {
{
{ 8, bark_tab_s08_64, 10, tab.fcb08s , 1, 5, tab.cb0808s0, tab.cb0808s1, 18},
{ 2, bark_tab_m08_256, 20, tab.fcb08m , 2, 5, tab.cb0808m0, tab.cb0808m1, 16},
{ 1, bark_tab_l08_512, 30, tab.fcb08l , 3, 6, tab.cb0808l0, tab.cb0808l1, 17}
},
512 , 12, tab.lsp08, 1, 5, 3, 3, tab.shape08 , 8, 28, 20, 6, 40
};
static const ModeTab mode_11_08 = {
{
{ 8, bark_tab_s11_64, 10, tab.fcb11s , 1, 5, tab.cb1108s0, tab.cb1108s1, 29},
{ 2, bark_tab_m11_256, 20, tab.fcb11m , 2, 5, tab.cb1108m0, tab.cb1108m1, 24},
{ 1, bark_tab_l11_512, 30, tab.fcb11l , 3, 6, tab.cb1108l0, tab.cb1108l1, 27}
},
512 , 16, tab.lsp11, 1, 6, 4, 3, tab.shape11 , 9, 36, 30, 7, 90
};
static const ModeTab mode_11_10 = {
{
{ 8, bark_tab_s11_64, 10, tab.fcb11s , 1, 5, tab.cb1110s0, tab.cb1110s1, 21},
{ 2, bark_tab_m11_256, 20, tab.fcb11m , 2, 5, tab.cb1110m0, tab.cb1110m1, 18},
{ 1, bark_tab_l11_512, 30, tab.fcb11l , 3, 6, tab.cb1110l0, tab.cb1110l1, 20}
},
512 , 16, tab.lsp11, 1, 6, 4, 3, tab.shape11 , 9, 36, 30, 7, 90
};
static const ModeTab mode_16_16 = {
{
{ 8, bark_tab_s16_128, 10, tab.fcb16s , 1, 5, tab.cb1616s0, tab.cb1616s1, 16},
{ 2, bark_tab_m16_512, 20, tab.fcb16m , 2, 5, tab.cb1616m0, tab.cb1616m1, 15},
{ 1, bark_tab_l16_1024,30, tab.fcb16l , 3, 6, tab.cb1616l0, tab.cb1616l1, 16}
},
1024, 16, tab.lsp16, 1, 6, 4, 3, tab.shape16 , 9, 56, 60, 7, 180
};
static const ModeTab mode_22_20 = {
{
{ 8, bark_tab_s22_128, 10, tab.fcb22s_1, 1, 6, tab.cb2220s0, tab.cb2220s1, 18},
{ 2, bark_tab_m22_512, 20, tab.fcb22m_1, 2, 6, tab.cb2220m0, tab.cb2220m1, 17},
{ 1, bark_tab_l22_1024,32, tab.fcb22l_1, 4, 6, tab.cb2220l0, tab.cb2220l1, 18}
},
1024, 16, tab.lsp22_1, 1, 6, 4, 3, tab.shape22_1, 9, 56, 36, 7, 144
};
static const ModeTab mode_22_24 = {
{
{ 8, bark_tab_s22_128, 10, tab.fcb22s_1, 1, 6, tab.cb2224s0, tab.cb2224s1, 15},
{ 2, bark_tab_m22_512, 20, tab.fcb22m_1, 2, 6, tab.cb2224m0, tab.cb2224m1, 14},
{ 1, bark_tab_l22_1024,32, tab.fcb22l_1, 4, 6, tab.cb2224l0, tab.cb2224l1, 15}
},
1024, 16, tab.lsp22_1, 1, 6, 4, 3, tab.shape22_1, 9, 56, 36, 7, 144
};
static const ModeTab mode_22_32 = {
{
{ 4, bark_tab_s22_128, 10, tab.fcb22s_2, 1, 6, tab.cb2232s0, tab.cb2232s1, 11},
{ 2, bark_tab_m22_256, 20, tab.fcb22m_2, 2, 6, tab.cb2232m0, tab.cb2232m1, 11},
{ 1, bark_tab_l22_512, 32, tab.fcb22l_2, 4, 6, tab.cb2232l0, tab.cb2232l1, 12}
},
512 , 16, tab.lsp22_2, 1, 6, 4, 4, tab.shape22_2, 9, 56, 36, 7, 72
};
static const ModeTab mode_44_40 = {
{
{16, bark_tab_s44_128, 10, tab.fcb44s , 1, 6, tab.cb4440s0, tab.cb4440s1, 18},
{ 4, bark_tab_m44_512, 20, tab.fcb44m , 2, 6, tab.cb4440m0, tab.cb4440m1, 17},
{ 1, bark_tab_l44_2048,40, tab.fcb44l , 4, 6, tab.cb4440l0, tab.cb4440l1, 17}
},
2048, 20, tab.lsp44, 1, 6, 4, 4, tab.shape44 , 9, 84, 54, 7, 432
};
static const ModeTab mode_44_48 = {
{
{16, bark_tab_s44_128, 10, tab.fcb44s , 1, 6, tab.cb4448s0, tab.cb4448s1, 15},
{ 4, bark_tab_m44_512, 20, tab.fcb44m , 2, 6, tab.cb4448m0, tab.cb4448m1, 14},
{ 1, bark_tab_l44_2048,40, tab.fcb44l , 4, 6, tab.cb4448l0, tab.cb4448l1, 14}
},
2048, 20, tab.lsp44, 1, 6, 4, 4, tab.shape44 , 9, 84, 54, 7, 432
};
typedef struct TwinContext {
AVCodecContext *avctx;
DSPContext dsp;
FFTContext mdct_ctx[3];
const ModeTab *mtab;
// history
float lsp_hist[2][20]; ///< LSP coefficients of the last frame
float bark_hist[3][2][40]; ///< BSE coefficients of last frame
// bitstream parameters
int16_t permut[4][4096];
uint8_t length[4][2]; ///< main codebook stride
uint8_t length_change[4];
uint8_t bits_main_spec[2][4][2]; ///< bits for the main codebook
int bits_main_spec_change[4];
int n_div[4];
float *spectrum;
float *curr_frame; ///< non-interleaved output
float *prev_frame; ///< non-interleaved previous frame
int last_block_pos[2];
float *cos_tabs[3];
// scratch buffers
float *tmp_buf;
} TwinContext;
#define PPC_SHAPE_CB_SIZE 64
#define SUB_AMP_MAX 4500.0
#define MULAW_MU 100.0
#define GAIN_BITS 8
#define AMP_MAX 13000.0
#define SUB_GAIN_BITS 5
#define WINDOW_TYPE_BITS 4
#define PGAIN_MU 200
/** @note not speed critical, hence not optimized */
static void memset_float(float *buf, float val, int size)
{
while (size--)
*buf++ = val;
}
/**
* Evaluate a single LPC amplitude spectrum envelope coefficient from the line
* spectrum pairs.
*
* @param lsp a vector of the cosinus of the LSP values
* @param cos_val cos(PI*i/N) where i is the index of the LPC amplitude
* @param order the order of the LSP (and the size of the *lsp buffer). Must
* be a multiple of four.
* @return the LPC value
*
* @todo reuse code from vorbis_dec.c: vorbis_floor0_decode
*/
static float eval_lpc_spectrum(const float *lsp, float cos_val, int order)
{
int j;
float p = 0.5f;
float q = 0.5f;
float two_cos_w = 2.0f*cos_val;
for (j = 0; j + 1 < order; j += 2*2) {
// Unroll the loop once since order is a multiple of four
q *= lsp[j ] - two_cos_w;
p *= lsp[j+1] - two_cos_w;
q *= lsp[j+2] - two_cos_w;
p *= lsp[j+3] - two_cos_w;
}
p *= p * (2.0f - two_cos_w);
q *= q * (2.0f + two_cos_w);
return 0.5 / (p + q);
}
/**
* Evaluates the LPC amplitude spectrum envelope from the line spectrum pairs.
*/
static void eval_lpcenv(TwinContext *tctx, const float *cos_vals, float *lpc)
{
int i;
const ModeTab *mtab = tctx->mtab;
int size_s = mtab->size / mtab->fmode[FT_SHORT].sub;
for (i = 0; i < size_s/2; i++) {
float cos_i = tctx->cos_tabs[0][i];
lpc[i] = eval_lpc_spectrum(cos_vals, cos_i, mtab->n_lsp);
lpc[size_s-i-1] = eval_lpc_spectrum(cos_vals, -cos_i, mtab->n_lsp);
}
}
static void interpolate(float *out, float v1, float v2, int size)
{
int i;
float step = (v1 - v2)/(size + 1);
for (i = 0; i < size; i++) {
v2 += step;
out[i] = v2;
}
}
static inline float get_cos(int idx, int part, const float *cos_tab, int size)
{
return part ? -cos_tab[size - idx - 1] :
cos_tab[ idx ];
}
/**
* Evaluates the LPC amplitude spectrum envelope from the line spectrum pairs.
* Probably for speed reasons, the coefficients are evaluated as
* siiiibiiiisiiiibiiiisiiiibiiiisiiiibiiiis ...
* where s is an evaluated value, i is a value interpolated from the others
* and b might be either calculated or interpolated, depending on an
* unexplained condition.
*
* @param step the size of a block "siiiibiiii"
* @param in the cosinus of the LSP data
* @param part is 0 for 0...PI (positive cossinus values) and 1 for PI...2PI
(negative cossinus values)
* @param size the size of the whole output
*/
static inline void eval_lpcenv_or_interp(TwinContext *tctx,
enum FrameType ftype,
float *out, const float *in,
int size, int step, int part)
{
int i;
const ModeTab *mtab = tctx->mtab;
const float *cos_tab = tctx->cos_tabs[ftype];
// Fill the 's'
for (i = 0; i < size; i += step)
out[i] =
eval_lpc_spectrum(in,
get_cos(i, part, cos_tab, size),
mtab->n_lsp);
// Fill the 'iiiibiiii'
for (i = step; i <= size - 2*step; i += step) {
if (out[i + step] + out[i - step] > 1.95*out[i] ||
out[i + step] >= out[i - step]) {
interpolate(out + i - step + 1, out[i], out[i-step], step - 1);
} else {
out[i - step/2] =
eval_lpc_spectrum(in,
get_cos(i-step/2, part, cos_tab, size),
mtab->n_lsp);
interpolate(out + i - step + 1, out[i-step/2], out[i-step ], step/2 - 1);
interpolate(out + i - step/2 + 1, out[i ], out[i-step/2], step/2 - 1);
}
}
interpolate(out + size - 2*step + 1, out[size-step], out[size - 2*step], step - 1);
}
static void eval_lpcenv_2parts(TwinContext *tctx, enum FrameType ftype,
const float *buf, float *lpc,
int size, int step)
{
eval_lpcenv_or_interp(tctx, ftype, lpc , buf, size/2, step, 0);
eval_lpcenv_or_interp(tctx, ftype, lpc + size/2, buf, size/2, 2*step, 1);
interpolate(lpc+size/2-step+1, lpc[size/2], lpc[size/2-step], step);
memset_float(lpc + size - 2*step + 1, lpc[size - 2*step], 2*step - 1);
}
/**
* Inverse quantization. Read CB coefficients for cb1 and cb2 from the
* bitstream, sum the corresponding vectors and write the result to *out
* after permutation.
*/
static void dequant(TwinContext *tctx, GetBitContext *gb, float *out,
enum FrameType ftype,
const int16_t *cb0, const int16_t *cb1, int cb_len)
{
int pos = 0;
int i, j;
for (i = 0; i < tctx->n_div[ftype]; i++) {
int tmp0, tmp1;
int sign0 = 1;
int sign1 = 1;
const int16_t *tab0, *tab1;
int length = tctx->length[ftype][i >= tctx->length_change[ftype]];
int bitstream_second_part = (i >= tctx->bits_main_spec_change[ftype]);
int bits = tctx->bits_main_spec[0][ftype][bitstream_second_part];
if (bits == 7) {
if (get_bits1(gb))
sign0 = -1;
bits = 6;
}
tmp0 = get_bits(gb, bits);
bits = tctx->bits_main_spec[1][ftype][bitstream_second_part];
if (bits == 7) {
if (get_bits1(gb))
sign1 = -1;
bits = 6;
}
tmp1 = get_bits(gb, bits);
tab0 = cb0 + tmp0*cb_len;
tab1 = cb1 + tmp1*cb_len;
for (j = 0; j < length; j++)
out[tctx->permut[ftype][pos+j]] = sign0*tab0[j] + sign1*tab1[j];
pos += length;
}
}
static inline float mulawinv(float y, float clip, float mu)
{
y = av_clipf(y/clip, -1, 1);
return clip * FFSIGN(y) * (exp(log(1+mu) * fabs(y)) - 1) / mu;
}
/**
* Evaluate a*b/400 rounded to the nearest integer. When, for example,
* a*b == 200 and the nearest integer is ill-defined, use a table to emulate
* the following broken float-based implementation used by the binary decoder:
*
* \code
* static int very_broken_op(int a, int b)
* {
* static float test; // Ugh, force gcc to do the division first...
*
* test = a/400.;
* return b * test + 0.5;
* }
* \endcode
*
* @note if this function is replaced by just ROUNDED_DIV(a*b,400.), the stddev
* between the original file (before encoding with Yamaha encoder) and the
* decoded output increases, which leads one to believe that the encoder expects
* exactly this broken calculation.
*/
static int very_broken_op(int a, int b)
{
int x = a*b + 200;
int size;
const uint8_t *rtab;
if (x%400 || b%5)
return x/400;
x /= 400;
size = tabs[b/5].size;
rtab = tabs[b/5].tab;
return x - rtab[size*av_log2(2*(x - 1)/size)+(x - 1)%size];
}
/**
* Sum to data a periodic peak of a given period, width and shape.
*
* @param period the period of the peak divised by 400.0
*/
static void add_peak(int period, int width, const float *shape,
float ppc_gain, float *speech, int len)
{
int i, j;
const float *shape_end = shape + len;
int center;
// First peak centered around zero
for (i = 0; i < width/2; i++)
speech[i] += ppc_gain * *shape++;
for (i = 1; i < ROUNDED_DIV(len,width) ; i++) {
center = very_broken_op(period, i);
for (j = -width/2; j < (width+1)/2; j++)
speech[j+center] += ppc_gain * *shape++;
}
// For the last block, be careful not to go beyond the end of the buffer
center = very_broken_op(period, i);
for (j = -width/2; j < (width + 1)/2 && shape < shape_end; j++)
speech[j+center] += ppc_gain * *shape++;
}
static void decode_ppc(TwinContext *tctx, int period_coef, const float *shape,
float ppc_gain, float *speech)
{
const ModeTab *mtab = tctx->mtab;
int isampf = tctx->avctx->sample_rate/1000;
int ibps = tctx->avctx->bit_rate/(1000 * tctx->avctx->channels);
int min_period = ROUNDED_DIV( 40*2*mtab->size, isampf);
int max_period = ROUNDED_DIV(6*40*2*mtab->size, isampf);
int period_range = max_period - min_period;
// This is actually the period multiplied by 400. It is just linearly coded
// between its maximum and minimum value.
int period = min_period +
ROUNDED_DIV(period_coef*period_range, (1 << mtab->ppc_period_bit) - 1);
int width;
if (isampf == 22 && ibps == 32) {
// For some unknown reason, NTT decided to code this case differently...
width = ROUNDED_DIV((period + 800)* mtab->peak_per2wid, 400*mtab->size);
} else
width = (period )* mtab->peak_per2wid/(400*mtab->size);
add_peak(period, width, shape, ppc_gain, speech, mtab->ppc_shape_len);
}
static void dec_gain(TwinContext *tctx, GetBitContext *gb, enum FrameType ftype,
float *out)
{
const ModeTab *mtab = tctx->mtab;
int i, j;
int sub = mtab->fmode[ftype].sub;
float step = AMP_MAX / ((1 << GAIN_BITS) - 1);
float sub_step = SUB_AMP_MAX / ((1 << SUB_GAIN_BITS) - 1);
if (ftype == FT_LONG) {
for (i = 0; i < tctx->avctx->channels; i++)
out[i] = (1./(1<<13)) *
mulawinv(step * 0.5 + step * get_bits(gb, GAIN_BITS),
AMP_MAX, MULAW_MU);
} else {
for (i = 0; i < tctx->avctx->channels; i++) {
float val = (1./(1<<23)) *
mulawinv(step * 0.5 + step * get_bits(gb, GAIN_BITS),
AMP_MAX, MULAW_MU);
for (j = 0; j < sub; j++) {
out[i*sub + j] =
val*mulawinv(sub_step* 0.5 +
sub_step* get_bits(gb, SUB_GAIN_BITS),
SUB_AMP_MAX, MULAW_MU);
}
}
}
}
/**
* Rearrange the LSP coefficients so that they have a minimum distance of
* min_dist. This function does it exactly as described in section of 3.2.4
* of the G.729 specification (but interestingly is different from what the
* reference decoder actually does).
*/
static void rearrange_lsp(int order, float *lsp, float min_dist)
{
int i;
float min_dist2 = min_dist * 0.5;
for (i = 1; i < order; i++)
if (lsp[i] - lsp[i-1] < min_dist) {
float avg = (lsp[i] + lsp[i-1]) * 0.5;
lsp[i-1] = avg - min_dist2;
lsp[i ] = avg + min_dist2;
}
}
static void decode_lsp(TwinContext *tctx, int lpc_idx1, uint8_t *lpc_idx2,
int lpc_hist_idx, float *lsp, float *hist)
{
const ModeTab *mtab = tctx->mtab;
int i, j;
const float *cb = mtab->lspcodebook;
const float *cb2 = cb + (1 << mtab->lsp_bit1)*mtab->n_lsp;
const float *cb3 = cb2 + (1 << mtab->lsp_bit2)*mtab->n_lsp;
const int8_t funny_rounding[4] = {
-2,
mtab->lsp_split == 4 ? -2 : 1,
mtab->lsp_split == 4 ? -2 : 1,
0
};
j = 0;
for (i = 0; i < mtab->lsp_split; i++) {
int chunk_end = ((i + 1)*mtab->n_lsp + funny_rounding[i])/mtab->lsp_split;
for (; j < chunk_end; j++)
lsp[j] = cb [lpc_idx1 * mtab->n_lsp + j] +
cb2[lpc_idx2[i] * mtab->n_lsp + j];
}
rearrange_lsp(mtab->n_lsp, lsp, 0.0001);
for (i = 0; i < mtab->n_lsp; i++) {
float tmp1 = 1. - cb3[lpc_hist_idx*mtab->n_lsp + i];
float tmp2 = hist[i] * cb3[lpc_hist_idx*mtab->n_lsp + i];
hist[i] = lsp[i];
lsp[i] = lsp[i] * tmp1 + tmp2;
}
rearrange_lsp(mtab->n_lsp, lsp, 0.0001);
rearrange_lsp(mtab->n_lsp, lsp, 0.000095);
ff_sort_nearly_sorted_floats(lsp, mtab->n_lsp);
}
static void dec_lpc_spectrum_inv(TwinContext *tctx, float *lsp,
enum FrameType ftype, float *lpc)
{
int i;
int size = tctx->mtab->size / tctx->mtab->fmode[ftype].sub;
for (i = 0; i < tctx->mtab->n_lsp; i++)
lsp[i] = 2*cos(lsp[i]);
switch (ftype) {
case FT_LONG:
eval_lpcenv_2parts(tctx, ftype, lsp, lpc, size, 8);
break;
case FT_MEDIUM:
eval_lpcenv_2parts(tctx, ftype, lsp, lpc, size, 2);
break;
case FT_SHORT:
eval_lpcenv(tctx, lsp, lpc);
break;
}
}
static void imdct_and_window(TwinContext *tctx, enum FrameType ftype, int wtype,
float *in, float *prev, int ch)
{
const ModeTab *mtab = tctx->mtab;
int bsize = mtab->size / mtab->fmode[ftype].sub;
int size = mtab->size;
float *buf1 = tctx->tmp_buf;
int j;
int wsize; // Window size
float *out = tctx->curr_frame + 2*ch*mtab->size;
float *out2 = out;
float *prev_buf;
int first_wsize;
static const uint8_t wtype_to_wsize[] = {0, 0, 2, 2, 2, 1, 0, 1, 1};
int types_sizes[] = {
mtab->size / mtab->fmode[FT_LONG ].sub,
mtab->size / mtab->fmode[FT_MEDIUM].sub,
mtab->size / (2*mtab->fmode[FT_SHORT ].sub),
};
wsize = types_sizes[wtype_to_wsize[wtype]];
first_wsize = wsize;
prev_buf = prev + (size - bsize)/2;
for (j = 0; j < mtab->fmode[ftype].sub; j++) {
int sub_wtype = ftype == FT_MEDIUM ? 8 : wtype;
if (!j && wtype == 4)
sub_wtype = 4;
else if (j == mtab->fmode[ftype].sub-1 && wtype == 7)
sub_wtype = 7;
wsize = types_sizes[wtype_to_wsize[sub_wtype]];
ff_imdct_half(&tctx->mdct_ctx[ftype], buf1 + bsize*j, in + bsize*j);
tctx->dsp.vector_fmul_window(out2,
prev_buf + (bsize-wsize)/2,
buf1 + bsize*j,
ff_sine_windows[av_log2(wsize)],
0.0,
wsize/2);
out2 += wsize;
memcpy(out2, buf1 + bsize*j + wsize/2, (bsize - wsize/2)*sizeof(float));
out2 += ftype == FT_MEDIUM ? (bsize-wsize)/2 : bsize - wsize;
prev_buf = buf1 + bsize*j + bsize/2;
}
tctx->last_block_pos[ch] = (size + first_wsize)/2;
}
static void imdct_output(TwinContext *tctx, enum FrameType ftype, int wtype,
float *out)
{
const ModeTab *mtab = tctx->mtab;
float *prev_buf = tctx->prev_frame + tctx->last_block_pos[0];
int i, j;
for (i = 0; i < tctx->avctx->channels; i++) {
imdct_and_window(tctx, ftype, wtype,
tctx->spectrum + i*mtab->size,
prev_buf + 2*i*mtab->size,
i);
}
if (tctx->avctx->channels == 2) {
for (i = 0; i < mtab->size - tctx->last_block_pos[0]; i++) {
float f1 = prev_buf[ i];
float f2 = prev_buf[2*mtab->size + i];
out[2*i ] = f1 + f2;
out[2*i + 1] = f1 - f2;
}
for (j = 0; i < mtab->size; j++,i++) {
float f1 = tctx->curr_frame[ j];
float f2 = tctx->curr_frame[2*mtab->size + j];
out[2*i ] = f1 + f2;
out[2*i + 1] = f1 - f2;
}
} else {
memcpy(out, prev_buf,
(mtab->size - tctx->last_block_pos[0]) * sizeof(*out));
out += mtab->size - tctx->last_block_pos[0];
memcpy(out, tctx->curr_frame,
(tctx->last_block_pos[0]) * sizeof(*out));
}
}
static void dec_bark_env(TwinContext *tctx, const uint8_t *in, int use_hist,
int ch, float *out, float gain, enum FrameType ftype)
{
const ModeTab *mtab = tctx->mtab;
int i,j;
float *hist = tctx->bark_hist[ftype][ch];
float val = ((const float []) {0.4, 0.35, 0.28})[ftype];
int bark_n_coef = mtab->fmode[ftype].bark_n_coef;
int fw_cb_len = mtab->fmode[ftype].bark_env_size / bark_n_coef;
int idx = 0;
for (i = 0; i < fw_cb_len; i++)
for (j = 0; j < bark_n_coef; j++, idx++) {
float tmp2 =
mtab->fmode[ftype].bark_cb[fw_cb_len*in[j] + i] * (1./4096);
float st = use_hist ?
(1. - val) * tmp2 + val*hist[idx] + 1. : tmp2 + 1.;
hist[idx] = tmp2;
if (st < -1.) st = 1.;
memset_float(out, st * gain, mtab->fmode[ftype].bark_tab[idx]);
out += mtab->fmode[ftype].bark_tab[idx];
}
}
static void read_and_decode_spectrum(TwinContext *tctx, GetBitContext *gb,
float *out, enum FrameType ftype)
{
const ModeTab *mtab = tctx->mtab;
int channels = tctx->avctx->channels;
int sub = mtab->fmode[ftype].sub;
int block_size = mtab->size / sub;
float gain[channels*sub];
float ppc_shape[mtab->ppc_shape_len * channels * 4];
uint8_t bark1[channels][sub][mtab->fmode[ftype].bark_n_coef];
uint8_t bark_use_hist[channels][sub];
uint8_t lpc_idx1[channels];
uint8_t lpc_idx2[channels][tctx->mtab->lsp_split];
uint8_t lpc_hist_idx[channels];
int i, j, k;
dequant(tctx, gb, out, ftype,
mtab->fmode[ftype].cb0, mtab->fmode[ftype].cb1,
mtab->fmode[ftype].cb_len_read);
for (i = 0; i < channels; i++)
for (j = 0; j < sub; j++)
for (k = 0; k < mtab->fmode[ftype].bark_n_coef; k++)
bark1[i][j][k] =
get_bits(gb, mtab->fmode[ftype].bark_n_bit);
for (i = 0; i < channels; i++)
for (j = 0; j < sub; j++)
bark_use_hist[i][j] = get_bits1(gb);
dec_gain(tctx, gb, ftype, gain);
for (i = 0; i < channels; i++) {
lpc_hist_idx[i] = get_bits(gb, tctx->mtab->lsp_bit0);
lpc_idx1 [i] = get_bits(gb, tctx->mtab->lsp_bit1);
for (j = 0; j < tctx->mtab->lsp_split; j++)
lpc_idx2[i][j] = get_bits(gb, tctx->mtab->lsp_bit2);
}
if (ftype == FT_LONG) {
int cb_len_p = (tctx->n_div[3] + mtab->ppc_shape_len*channels - 1)/
tctx->n_div[3];
dequant(tctx, gb, ppc_shape, FT_PPC, mtab->ppc_shape_cb,
mtab->ppc_shape_cb + cb_len_p*PPC_SHAPE_CB_SIZE, cb_len_p);
}
for (i = 0; i < channels; i++) {
float *chunk = out + mtab->size * i;
float lsp[tctx->mtab->n_lsp];
for (j = 0; j < sub; j++) {
dec_bark_env(tctx, bark1[i][j], bark_use_hist[i][j], i,
tctx->tmp_buf, gain[sub*i+j], ftype);
tctx->dsp.vector_fmul(chunk + block_size*j, tctx->tmp_buf,
block_size);
}
if (ftype == FT_LONG) {
float pgain_step = 25000. / ((1 << mtab->pgain_bit) - 1);
int p_coef = get_bits(gb, tctx->mtab->ppc_period_bit);
int g_coef = get_bits(gb, tctx->mtab->pgain_bit);
float v = 1./8192*
mulawinv(pgain_step*g_coef+ pgain_step/2, 25000., PGAIN_MU);
decode_ppc(tctx, p_coef, ppc_shape + i*mtab->ppc_shape_len, v,
chunk);
}
decode_lsp(tctx, lpc_idx1[i], lpc_idx2[i], lpc_hist_idx[i], lsp,
tctx->lsp_hist[i]);
dec_lpc_spectrum_inv(tctx, lsp, ftype, tctx->tmp_buf);
for (j = 0; j < mtab->fmode[ftype].sub; j++) {
tctx->dsp.vector_fmul(chunk, tctx->tmp_buf, block_size);
chunk += block_size;
}
}
}
static int twin_decode_frame(AVCodecContext * avctx, void *data,
int *data_size, AVPacket *avpkt)
{
const uint8_t *buf = avpkt->data;
int buf_size = avpkt->size;
TwinContext *tctx = avctx->priv_data;
GetBitContext gb;
const ModeTab *mtab = tctx->mtab;
float *out = data;
enum FrameType ftype;
int window_type;
static const enum FrameType wtype_to_ftype_table[] = {
FT_LONG, FT_LONG, FT_SHORT, FT_LONG,
FT_MEDIUM, FT_LONG, FT_LONG, FT_MEDIUM, FT_MEDIUM
};
if (buf_size*8 < avctx->bit_rate*mtab->size/avctx->sample_rate + 8) {
av_log(avctx, AV_LOG_ERROR,
"Frame too small (%d bytes). Truncated file?\n", buf_size);
*data_size = 0;
return buf_size;
}
init_get_bits(&gb, buf, buf_size * 8);
skip_bits(&gb, get_bits(&gb, 8));
window_type = get_bits(&gb, WINDOW_TYPE_BITS);
if (window_type > 8) {
av_log(avctx, AV_LOG_ERROR, "Invalid window type, broken sample?\n");
return -1;
}
ftype = wtype_to_ftype_table[window_type];
read_and_decode_spectrum(tctx, &gb, tctx->spectrum, ftype);
imdct_output(tctx, ftype, window_type, out);
FFSWAP(float*, tctx->curr_frame, tctx->prev_frame);
if (tctx->avctx->frame_number < 2) {
*data_size=0;
return buf_size;
}
*data_size = mtab->size*avctx->channels*4;
return buf_size;
}
/**
* Init IMDCT and windowing tables
*/
static av_cold void init_mdct_win(TwinContext *tctx)
{
int i,j;
const ModeTab *mtab = tctx->mtab;
int size_s = mtab->size / mtab->fmode[FT_SHORT].sub;
int size_m = mtab->size / mtab->fmode[FT_MEDIUM].sub;
int channels = tctx->avctx->channels;
float norm = channels == 1 ? 2. : 1.;
for (i = 0; i < 3; i++) {
int bsize = tctx->mtab->size/tctx->mtab->fmode[i].sub;
ff_mdct_init(&tctx->mdct_ctx[i], av_log2(bsize) + 1, 1,
-sqrt(norm/bsize) / (1<<15));
}
tctx->tmp_buf = av_malloc(mtab->size * sizeof(*tctx->tmp_buf));
tctx->spectrum = av_malloc(2*mtab->size*channels*sizeof(float));
tctx->curr_frame = av_malloc(2*mtab->size*channels*sizeof(float));
tctx->prev_frame = av_malloc(2*mtab->size*channels*sizeof(float));
for (i = 0; i < 3; i++) {
int m = 4*mtab->size/mtab->fmode[i].sub;
double freq = 2*M_PI/m;
tctx->cos_tabs[i] = av_malloc((m/4)*sizeof(*tctx->cos_tabs));
for (j = 0; j <= m/8; j++)
tctx->cos_tabs[i][j] = cos((2*j + 1)*freq);
for (j = 1; j < m/8; j++)
tctx->cos_tabs[i][m/4-j] = tctx->cos_tabs[i][j];
}
ff_init_ff_sine_windows(av_log2(size_m));
ff_init_ff_sine_windows(av_log2(size_s/2));
ff_init_ff_sine_windows(av_log2(mtab->size));
}
/**
* Interpret the data as if it were a num_blocks x line_len[0] matrix and for
* each line do a cyclic permutation, i.e.
* abcdefghijklm -> defghijklmabc
* where the amount to be shifted is evaluated depending on the column.
*/
static void permutate_in_line(int16_t *tab, int num_vect, int num_blocks,
int block_size,
const uint8_t line_len[2], int length_div,
enum FrameType ftype)
{
int i,j;
for (i = 0; i < line_len[0]; i++) {
int shift;
if (num_blocks == 1 ||
(ftype == FT_LONG && num_vect % num_blocks) ||
(ftype != FT_LONG && num_vect & 1 ) ||
i == line_len[1]) {
shift = 0;
} else if (ftype == FT_LONG) {
shift = i;
} else
shift = i*i;
for (j = 0; j < num_vect && (j+num_vect*i < block_size*num_blocks); j++)
tab[i*num_vect+j] = i*num_vect + (j + shift) % num_vect;
}
}
/**
* Interpret the input data as in the following table:
*
* \verbatim
*
* abcdefgh
* ijklmnop
* qrstuvw
* x123456
*
* \endverbatim
*
* and transpose it, giving the output
* aiqxbjr1cks2dlt3emu4fvn5gow6hp
*/
static void transpose_perm(int16_t *out, int16_t *in, int num_vect,
const uint8_t line_len[2], int length_div)
{
int i,j;
int cont= 0;
for (i = 0; i < num_vect; i++)
for (j = 0; j < line_len[i >= length_div]; j++)
out[cont++] = in[j*num_vect + i];
}
static void linear_perm(int16_t *out, int16_t *in, int n_blocks, int size)
{
int block_size = size/n_blocks;
int i;
for (i = 0; i < size; i++)
out[i] = block_size * (in[i] % n_blocks) + in[i] / n_blocks;
}
static av_cold void construct_perm_table(TwinContext *tctx,enum FrameType ftype)
{
int block_size;
const ModeTab *mtab = tctx->mtab;
int size = tctx->avctx->channels*mtab->fmode[ftype].sub;
int16_t *tmp_perm = (int16_t *) tctx->tmp_buf;
if (ftype == FT_PPC) {
size = tctx->avctx->channels;
block_size = mtab->ppc_shape_len;
} else
block_size = mtab->size / mtab->fmode[ftype].sub;
permutate_in_line(tmp_perm, tctx->n_div[ftype], size,
block_size, tctx->length[ftype],
tctx->length_change[ftype], ftype);
transpose_perm(tctx->permut[ftype], tmp_perm, tctx->n_div[ftype],
tctx->length[ftype], tctx->length_change[ftype]);
linear_perm(tctx->permut[ftype], tctx->permut[ftype], size,
size*block_size);
}
static av_cold void init_bitstream_params(TwinContext *tctx)
{
const ModeTab *mtab = tctx->mtab;
int n_ch = tctx->avctx->channels;
int total_fr_bits = tctx->avctx->bit_rate*mtab->size/
tctx->avctx->sample_rate;
int lsp_bits_per_block = n_ch*(mtab->lsp_bit0 + mtab->lsp_bit1 +
mtab->lsp_split*mtab->lsp_bit2);
int ppc_bits = n_ch*(mtab->pgain_bit + mtab->ppc_shape_bit +
mtab->ppc_period_bit);
int bsize_no_main_cb[3];
int bse_bits[3];
int i;
enum FrameType frametype;
for (i = 0; i < 3; i++)
// +1 for history usage switch
bse_bits[i] = n_ch *
(mtab->fmode[i].bark_n_coef * mtab->fmode[i].bark_n_bit + 1);
bsize_no_main_cb[2] = bse_bits[2] + lsp_bits_per_block + ppc_bits +
WINDOW_TYPE_BITS + n_ch*GAIN_BITS;
for (i = 0; i < 2; i++)
bsize_no_main_cb[i] =
lsp_bits_per_block + n_ch*GAIN_BITS + WINDOW_TYPE_BITS +
mtab->fmode[i].sub*(bse_bits[i] + n_ch*SUB_GAIN_BITS);
// The remaining bits are all used for the main spectrum coefficients
for (i = 0; i < 4; i++) {
int bit_size;
int vect_size;
int rounded_up, rounded_down, num_rounded_down, num_rounded_up;
if (i == 3) {
bit_size = n_ch * mtab->ppc_shape_bit;
vect_size = n_ch * mtab->ppc_shape_len;
} else {
bit_size = total_fr_bits - bsize_no_main_cb[i];
vect_size = n_ch * mtab->size;
}
tctx->n_div[i] = (bit_size + 13) / 14;
rounded_up = (bit_size + tctx->n_div[i] - 1)/tctx->n_div[i];
rounded_down = (bit_size )/tctx->n_div[i];
num_rounded_down = rounded_up * tctx->n_div[i] - bit_size;
num_rounded_up = tctx->n_div[i] - num_rounded_down;
tctx->bits_main_spec[0][i][0] = (rounded_up + 1)/2;
tctx->bits_main_spec[1][i][0] = (rounded_up )/2;
tctx->bits_main_spec[0][i][1] = (rounded_down + 1)/2;
tctx->bits_main_spec[1][i][1] = (rounded_down )/2;
tctx->bits_main_spec_change[i] = num_rounded_up;
rounded_up = (vect_size + tctx->n_div[i] - 1)/tctx->n_div[i];
rounded_down = (vect_size )/tctx->n_div[i];
num_rounded_down = rounded_up * tctx->n_div[i] - vect_size;
num_rounded_up = tctx->n_div[i] - num_rounded_down;
tctx->length[i][0] = rounded_up;
tctx->length[i][1] = rounded_down;
tctx->length_change[i] = num_rounded_up;
}
for (frametype = FT_SHORT; frametype <= FT_PPC; frametype++)
construct_perm_table(tctx, frametype);
}
static av_cold int twin_decode_init(AVCodecContext *avctx)
{
TwinContext *tctx = avctx->priv_data;
int isampf = avctx->sample_rate/1000;
int ibps = avctx->bit_rate/(1000 * avctx->channels);
tctx->avctx = avctx;
avctx->sample_fmt = SAMPLE_FMT_FLT;
if (avctx->channels > 2) {
av_log(avctx, AV_LOG_ERROR, "Unsupported number of channels: %i\n",
avctx->channels);
return -1;
}
switch ((isampf << 8) + ibps) {
case (8 <<8) + 8: tctx->mtab = &mode_08_08; break;
case (11<<8) + 8: tctx->mtab = &mode_11_08; break;
case (11<<8) + 10: tctx->mtab = &mode_11_10; break;
case (16<<8) + 16: tctx->mtab = &mode_16_16; break;
case (22<<8) + 20: tctx->mtab = &mode_22_20; break;
case (22<<8) + 24: tctx->mtab = &mode_22_24; break;
case (22<<8) + 32: tctx->mtab = &mode_22_32; break;
case (44<<8) + 40: tctx->mtab = &mode_44_40; break;
case (44<<8) + 48: tctx->mtab = &mode_44_48; break;
default:
av_log(avctx, AV_LOG_ERROR, "This version does not support %d kHz - %d kbit/s/ch mode.\n", isampf, isampf);
return -1;
}
dsputil_init(&tctx->dsp, avctx);
init_mdct_win(tctx);
init_bitstream_params(tctx);
memset_float(tctx->bark_hist[0][0], 0.1, FF_ARRAY_ELEMS(tctx->bark_hist));
return 0;
}
static av_cold int twin_decode_close(AVCodecContext *avctx)
{
TwinContext *tctx = avctx->priv_data;
int i;
for (i = 0; i < 3; i++) {
ff_mdct_end(&tctx->mdct_ctx[i]);
av_free(tctx->cos_tabs[i]);
}
av_free(tctx->curr_frame);
av_free(tctx->spectrum);
av_free(tctx->prev_frame);
av_free(tctx->tmp_buf);
return 0;
}
AVCodec twinvq_decoder =
{
"twinvq",
AVMEDIA_TYPE_AUDIO,
CODEC_ID_TWINVQ,
sizeof(TwinContext),
twin_decode_init,
NULL,
twin_decode_close,
twin_decode_frame,
.long_name = NULL_IF_CONFIG_SMALL("VQF TwinVQ"),
};
| 123linslouis-android-video-cutter | jni/libavcodec/twinvq.c | C | asf20 | 37,198 |
/*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef AVCODEC_SH4_DSPUTIL_SH4_H
#define AVCODEC_SH4_DSPUTIL_SH4_H
#include "libavcodec/avcodec.h"
#include "libavcodec/dsputil.h"
void idct_sh4(DCTELEM *block);
void dsputil_init_align(DSPContext* c, AVCodecContext *avctx);
#endif
| 123linslouis-android-video-cutter | jni/libavcodec/sh4/dsputil_sh4.h | C | asf20 | 1,000 |
/*
* idct for sh4
*
* Copyright (c) 2001-2003 BERO <bero@geocities.co.jp>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "libavcodec/dsputil.h"
#include "dsputil_sh4.h"
#include "sh4.h"
#define c1 1.38703984532214752434 /* sqrt(2)*cos(1*pi/16) */
#define c2 1.30656296487637657577 /* sqrt(2)*cos(2*pi/16) */
#define c3 1.17587560241935884520 /* sqrt(2)*cos(3*pi/16) */
#define c4 1.00000000000000000000 /* sqrt(2)*cos(4*pi/16) */
#define c5 0.78569495838710234903 /* sqrt(2)*cos(5*pi/16) */
#define c6 0.54119610014619712324 /* sqrt(2)*cos(6*pi/16) */
#define c7 0.27589937928294311353 /* sqrt(2)*cos(7*pi/16) */
static const float even_table[] __attribute__ ((aligned(8))) = {
c4, c4, c4, c4,
c2, c6,-c6,-c2,
c4,-c4,-c4, c4,
c6,-c2, c2,-c6
};
static const float odd_table[] __attribute__ ((aligned(8))) = {
c1, c3, c5, c7,
c3,-c7,-c1,-c5,
c5,-c1, c7, c3,
c7,-c5, c3,-c1
};
#undef c1
#undef c2
#undef c3
#undef c4
#undef c5
#undef c6
#undef c7
#if 1
#define load_matrix(table) \
do { \
const float *t = table; \
__asm__ volatile( \
" fschg\n" \
" fmov @%0+,xd0\n" \
" fmov @%0+,xd2\n" \
" fmov @%0+,xd4\n" \
" fmov @%0+,xd6\n" \
" fmov @%0+,xd8\n" \
" fmov @%0+,xd10\n" \
" fmov @%0+,xd12\n" \
" fmov @%0+,xd14\n" \
" fschg\n" \
: "+r"(t) \
); \
} while (0)
#define ftrv() \
__asm__ volatile("ftrv xmtrx,fv0" \
: "+f"(fr0),"+f"(fr1),"+f"(fr2),"+f"(fr3));
#define DEFREG \
register float fr0 __asm__("fr0"); \
register float fr1 __asm__("fr1"); \
register float fr2 __asm__("fr2"); \
register float fr3 __asm__("fr3")
#else
/* generic C code for check */
static void ftrv_(const float xf[],float fv[])
{
float f0,f1,f2,f3;
f0 = fv[0];
f1 = fv[1];
f2 = fv[2];
f3 = fv[3];
fv[0] = xf[0]*f0 + xf[4]*f1 + xf[ 8]*f2 + xf[12]*f3;
fv[1] = xf[1]*f0 + xf[5]*f1 + xf[ 9]*f2 + xf[13]*f3;
fv[2] = xf[2]*f0 + xf[6]*f1 + xf[10]*f2 + xf[14]*f3;
fv[3] = xf[3]*f0 + xf[7]*f1 + xf[11]*f2 + xf[15]*f3;
}
static void load_matrix_(float xf[],const float table[])
{
int i;
for(i=0;i<16;i++) xf[i]=table[i];
}
#define ftrv() ftrv_(xf,fv)
#define load_matrix(table) load_matrix_(xf,table)
#define DEFREG \
float fv[4],xf[16]
#define fr0 fv[0]
#define fr1 fv[1]
#define fr2 fv[2]
#define fr3 fv[3]
#endif
#if 1
#define DESCALE(x,n) (x)*(1.0f/(1<<(n)))
#else
#define DESCALE(x,n) (((int)(x)+(1<<(n-1)))>>(n))
#endif
/* this code work worse on gcc cvs. 3.2.3 work fine */
#if 1
//optimized
void idct_sh4(DCTELEM *block)
{
DEFREG;
int i;
float tblock[8*8],*fblock;
int ofs1,ofs2,ofs3;
int fpscr;
fp_single_enter(fpscr);
/* row */
/* even part */
load_matrix(even_table);
fblock = tblock+4;
i = 8;
do {
fr0 = block[0];
fr1 = block[2];
fr2 = block[4];
fr3 = block[6];
block+=8;
ftrv();
*--fblock = fr3;
*--fblock = fr2;
*--fblock = fr1;
*--fblock = fr0;
fblock+=8+4;
} while(--i);
block-=8*8;
fblock-=8*8+4;
load_matrix(odd_table);
i = 8;
do {
float t0,t1,t2,t3;
fr0 = block[1];
fr1 = block[3];
fr2 = block[5];
fr3 = block[7];
block+=8;
ftrv();
t0 = *fblock++;
t1 = *fblock++;
t2 = *fblock++;
t3 = *fblock++;
fblock+=4;
*--fblock = t0 - fr0;
*--fblock = t1 - fr1;
*--fblock = t2 - fr2;
*--fblock = t3 - fr3;
*--fblock = t3 + fr3;
*--fblock = t2 + fr2;
*--fblock = t1 + fr1;
*--fblock = t0 + fr0;
fblock+=8;
} while(--i);
block-=8*8;
fblock-=8*8;
/* col */
/* even part */
load_matrix(even_table);
ofs1 = sizeof(float)*2*8;
ofs2 = sizeof(float)*4*8;
ofs3 = sizeof(float)*6*8;
i = 8;
#define OA(fblock,ofs) *(float*)((char*)fblock + ofs)
do {
fr0 = OA(fblock, 0);
fr1 = OA(fblock,ofs1);
fr2 = OA(fblock,ofs2);
fr3 = OA(fblock,ofs3);
ftrv();
OA(fblock,0 ) = fr0;
OA(fblock,ofs1) = fr1;
OA(fblock,ofs2) = fr2;
OA(fblock,ofs3) = fr3;
fblock++;
} while(--i);
fblock-=8;
load_matrix(odd_table);
i=8;
do {
float t0,t1,t2,t3;
t0 = OA(fblock, 0); /* [8*0] */
t1 = OA(fblock,ofs1); /* [8*2] */
t2 = OA(fblock,ofs2); /* [8*4] */
t3 = OA(fblock,ofs3); /* [8*6] */
fblock+=8;
fr0 = OA(fblock, 0); /* [8*1] */
fr1 = OA(fblock,ofs1); /* [8*3] */
fr2 = OA(fblock,ofs2); /* [8*5] */
fr3 = OA(fblock,ofs3); /* [8*7] */
fblock+=-8+1;
ftrv();
block[8*0] = DESCALE(t0 + fr0,3);
block[8*7] = DESCALE(t0 - fr0,3);
block[8*1] = DESCALE(t1 + fr1,3);
block[8*6] = DESCALE(t1 - fr1,3);
block[8*2] = DESCALE(t2 + fr2,3);
block[8*5] = DESCALE(t2 - fr2,3);
block[8*3] = DESCALE(t3 + fr3,3);
block[8*4] = DESCALE(t3 - fr3,3);
block++;
} while(--i);
fp_single_leave(fpscr);
}
#else
void idct_sh4(DCTELEM *block)
{
DEFREG;
int i;
float tblock[8*8],*fblock;
/* row */
/* even part */
load_matrix(even_table);
fblock = tblock;
i = 8;
do {
fr0 = block[0];
fr1 = block[2];
fr2 = block[4];
fr3 = block[6];
block+=8;
ftrv();
fblock[0] = fr0;
fblock[2] = fr1;
fblock[4] = fr2;
fblock[6] = fr3;
fblock+=8;
} while(--i);
block-=8*8;
fblock-=8*8;
load_matrix(odd_table);
i = 8;
do {
float t0,t1,t2,t3;
fr0 = block[1];
fr1 = block[3];
fr2 = block[5];
fr3 = block[7];
block+=8;
ftrv();
t0 = fblock[0];
t1 = fblock[2];
t2 = fblock[4];
t3 = fblock[6];
fblock[0] = t0 + fr0;
fblock[7] = t0 - fr0;
fblock[1] = t1 + fr1;
fblock[6] = t1 - fr1;
fblock[2] = t2 + fr2;
fblock[5] = t2 - fr2;
fblock[3] = t3 + fr3;
fblock[4] = t3 - fr3;
fblock+=8;
} while(--i);
block-=8*8;
fblock-=8*8;
/* col */
/* even part */
load_matrix(even_table);
i = 8;
do {
fr0 = fblock[8*0];
fr1 = fblock[8*2];
fr2 = fblock[8*4];
fr3 = fblock[8*6];
ftrv();
fblock[8*0] = fr0;
fblock[8*2] = fr1;
fblock[8*4] = fr2;
fblock[8*6] = fr3;
fblock++;
} while(--i);
fblock-=8;
load_matrix(odd_table);
i=8;
do {
float t0,t1,t2,t3;
fr0 = fblock[8*1];
fr1 = fblock[8*3];
fr2 = fblock[8*5];
fr3 = fblock[8*7];
ftrv();
t0 = fblock[8*0];
t1 = fblock[8*2];
t2 = fblock[8*4];
t3 = fblock[8*6];
fblock++;
block[8*0] = DESCALE(t0 + fr0,3);
block[8*7] = DESCALE(t0 - fr0,3);
block[8*1] = DESCALE(t1 + fr1,3);
block[8*6] = DESCALE(t1 - fr1,3);
block[8*2] = DESCALE(t2 + fr2,3);
block[8*5] = DESCALE(t2 - fr2,3);
block[8*3] = DESCALE(t3 + fr3,3);
block[8*4] = DESCALE(t3 - fr3,3);
block++;
} while(--i);
}
#endif
| 123linslouis-android-video-cutter | jni/libavcodec/sh4/idct_sh4.c | C | asf20 | 9,806 |
/*
* aligned/packed access motion
*
* Copyright (c) 2001-2003 BERO <bero@geocities.co.jp>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "libavcodec/avcodec.h"
#include "libavcodec/dsputil.h"
#include "dsputil_sh4.h"
#define LP(p) *(uint32_t*)(p)
#define LPC(p) *(const uint32_t*)(p)
#define UNPACK(ph,pl,tt0,tt1) do { \
uint32_t t0,t1; t0=tt0;t1=tt1; \
ph = ( (t0 & ~BYTE_VEC32(0x03))>>2) + ( (t1 & ~BYTE_VEC32(0x03))>>2); \
pl = (t0 & BYTE_VEC32(0x03)) + (t1 & BYTE_VEC32(0x03)); } while(0)
#define rnd_PACK(ph,pl,nph,npl) ph + nph + (((pl + npl + BYTE_VEC32(0x02))>>2) & BYTE_VEC32(0x03))
#define no_rnd_PACK(ph,pl,nph,npl) ph + nph + (((pl + npl + BYTE_VEC32(0x01))>>2) & BYTE_VEC32(0x03))
/* little endian */
#define MERGE1(a,b,ofs) (ofs==0)?a:( ((a)>>(8*ofs))|((b)<<(32-8*ofs)) )
#define MERGE2(a,b,ofs) (ofs==3)?b:( ((a)>>(8*(ofs+1)))|((b)<<(32-8*(ofs+1))) )
/* big
#define MERGE1(a,b,ofs) (ofs==0)?a:( ((a)<<(8*ofs))|((b)>>(32-8*ofs)) )
#define MERGE2(a,b,ofs) (ofs==3)?b:( ((a)<<(8+8*ofs))|((b)>>(32-8-8*ofs)) )
*/
#define put(d,s) d = s
#define avg(d,s) d = rnd_avg32(s,d)
#define OP_C4(ofs) \
ref-=ofs; \
do { \
OP(LP(dest),MERGE1(LPC(ref),LPC(ref+4),ofs)); \
ref+=stride; \
dest+=stride; \
} while(--height)
#define OP_C40() \
do { \
OP(LP(dest),LPC(ref)); \
ref+=stride; \
dest+=stride; \
} while(--height)
#define OP put
static void put_pixels4_c(uint8_t *dest,const uint8_t *ref, const int stride,int height)
{
switch((int)ref&3){
case 0: OP_C40(); return;
case 1: OP_C4(1); return;
case 2: OP_C4(2); return;
case 3: OP_C4(3); return;
}
}
#undef OP
#define OP avg
static void avg_pixels4_c(uint8_t *dest,const uint8_t *ref, const int stride,int height)
{
switch((int)ref&3){
case 0: OP_C40(); return;
case 1: OP_C4(1); return;
case 2: OP_C4(2); return;
case 3: OP_C4(3); return;
}
}
#undef OP
#define OP_C(ofs,sz,avg2) \
{ \
ref-=ofs; \
do { \
uint32_t t0,t1; \
t0 = LPC(ref+0); \
t1 = LPC(ref+4); \
OP(LP(dest+0), MERGE1(t0,t1,ofs)); \
t0 = LPC(ref+8); \
OP(LP(dest+4), MERGE1(t1,t0,ofs)); \
if (sz==16) { \
t1 = LPC(ref+12); \
OP(LP(dest+8), MERGE1(t0,t1,ofs)); \
t0 = LPC(ref+16); \
OP(LP(dest+12), MERGE1(t1,t0,ofs)); \
} \
ref+=stride; \
dest+= stride; \
} while(--height); \
}
/* aligned */
#define OP_C0(sz,avg2) \
{ \
do { \
OP(LP(dest+0), LPC(ref+0)); \
OP(LP(dest+4), LPC(ref+4)); \
if (sz==16) { \
OP(LP(dest+8), LPC(ref+8)); \
OP(LP(dest+12), LPC(ref+12)); \
} \
ref+=stride; \
dest+= stride; \
} while(--height); \
}
#define OP_X(ofs,sz,avg2) \
{ \
ref-=ofs; \
do { \
uint32_t t0,t1; \
t0 = LPC(ref+0); \
t1 = LPC(ref+4); \
OP(LP(dest+0), avg2(MERGE1(t0,t1,ofs),MERGE2(t0,t1,ofs))); \
t0 = LPC(ref+8); \
OP(LP(dest+4), avg2(MERGE1(t1,t0,ofs),MERGE2(t1,t0,ofs))); \
if (sz==16) { \
t1 = LPC(ref+12); \
OP(LP(dest+8), avg2(MERGE1(t0,t1,ofs),MERGE2(t0,t1,ofs))); \
t0 = LPC(ref+16); \
OP(LP(dest+12), avg2(MERGE1(t1,t0,ofs),MERGE2(t1,t0,ofs))); \
} \
ref+=stride; \
dest+= stride; \
} while(--height); \
}
/* aligned */
#define OP_Y0(sz,avg2) \
{ \
uint32_t t0,t1,t2,t3,t; \
\
t0 = LPC(ref+0); \
t1 = LPC(ref+4); \
if (sz==16) { \
t2 = LPC(ref+8); \
t3 = LPC(ref+12); \
} \
do { \
ref += stride; \
\
t = LPC(ref+0); \
OP(LP(dest+0), avg2(t0,t)); t0 = t; \
t = LPC(ref+4); \
OP(LP(dest+4), avg2(t1,t)); t1 = t; \
if (sz==16) { \
t = LPC(ref+8); \
OP(LP(dest+8), avg2(t2,t)); t2 = t; \
t = LPC(ref+12); \
OP(LP(dest+12), avg2(t3,t)); t3 = t; \
} \
dest+= stride; \
} while(--height); \
}
#define OP_Y(ofs,sz,avg2) \
{ \
uint32_t t0,t1,t2,t3,t,w0,w1; \
\
ref-=ofs; \
w0 = LPC(ref+0); \
w1 = LPC(ref+4); \
t0 = MERGE1(w0,w1,ofs); \
w0 = LPC(ref+8); \
t1 = MERGE1(w1,w0,ofs); \
if (sz==16) { \
w1 = LPC(ref+12); \
t2 = MERGE1(w0,w1,ofs); \
w0 = LPC(ref+16); \
t3 = MERGE1(w1,w0,ofs); \
} \
do { \
ref += stride; \
\
w0 = LPC(ref+0); \
w1 = LPC(ref+4); \
t = MERGE1(w0,w1,ofs); \
OP(LP(dest+0), avg2(t0,t)); t0 = t; \
w0 = LPC(ref+8); \
t = MERGE1(w1,w0,ofs); \
OP(LP(dest+4), avg2(t1,t)); t1 = t; \
if (sz==16) { \
w1 = LPC(ref+12); \
t = MERGE1(w0,w1,ofs); \
OP(LP(dest+8), avg2(t2,t)); t2 = t; \
w0 = LPC(ref+16); \
t = MERGE1(w1,w0,ofs); \
OP(LP(dest+12), avg2(t3,t)); t3 = t; \
} \
dest+=stride; \
} while(--height); \
}
#define OP_X0(sz,avg2) OP_X(0,sz,avg2)
#define OP_XY0(sz,PACK) OP_XY(0,sz,PACK)
#define OP_XY(ofs,sz,PACK) \
{ \
uint32_t t2,t3,w0,w1; \
uint32_t a0,a1,a2,a3,a4,a5,a6,a7; \
\
ref -= ofs; \
w0 = LPC(ref+0); \
w1 = LPC(ref+4); \
UNPACK(a0,a1,MERGE1(w0,w1,ofs),MERGE2(w0,w1,ofs)); \
w0 = LPC(ref+8); \
UNPACK(a2,a3,MERGE1(w1,w0,ofs),MERGE2(w1,w0,ofs)); \
if (sz==16) { \
w1 = LPC(ref+12); \
UNPACK(a4,a5,MERGE1(w0,w1,ofs),MERGE2(w0,w1,ofs)); \
w0 = LPC(ref+16); \
UNPACK(a6,a7,MERGE1(w1,w0,ofs),MERGE2(w1,w0,ofs)); \
} \
do { \
ref+=stride; \
w0 = LPC(ref+0); \
w1 = LPC(ref+4); \
UNPACK(t2,t3,MERGE1(w0,w1,ofs),MERGE2(w0,w1,ofs)); \
OP(LP(dest+0),PACK(a0,a1,t2,t3)); \
a0 = t2; a1 = t3; \
w0 = LPC(ref+8); \
UNPACK(t2,t3,MERGE1(w1,w0,ofs),MERGE2(w1,w0,ofs)); \
OP(LP(dest+4),PACK(a2,a3,t2,t3)); \
a2 = t2; a3 = t3; \
if (sz==16) { \
w1 = LPC(ref+12); \
UNPACK(t2,t3,MERGE1(w0,w1,ofs),MERGE2(w0,w1,ofs)); \
OP(LP(dest+8),PACK(a4,a5,t2,t3)); \
a4 = t2; a5 = t3; \
w0 = LPC(ref+16); \
UNPACK(t2,t3,MERGE1(w1,w0,ofs),MERGE2(w1,w0,ofs)); \
OP(LP(dest+12),PACK(a6,a7,t2,t3)); \
a6 = t2; a7 = t3; \
} \
dest+=stride; \
} while(--height); \
}
#define DEFFUNC(op,rnd,xy,sz,OP_N,avgfunc) \
static void op##_##rnd##_pixels##sz##_##xy (uint8_t * dest, const uint8_t * ref, \
const int stride, int height) \
{ \
switch((int)ref&3) { \
case 0:OP_N##0(sz,rnd##_##avgfunc); return; \
case 1:OP_N(1,sz,rnd##_##avgfunc); return; \
case 2:OP_N(2,sz,rnd##_##avgfunc); return; \
case 3:OP_N(3,sz,rnd##_##avgfunc); return; \
} \
}
#define OP put
DEFFUNC(put, rnd,o,8,OP_C,avg32)
DEFFUNC(put, rnd,x,8,OP_X,avg32)
DEFFUNC(put,no_rnd,x,8,OP_X,avg32)
DEFFUNC(put, rnd,y,8,OP_Y,avg32)
DEFFUNC(put,no_rnd,y,8,OP_Y,avg32)
DEFFUNC(put, rnd,xy,8,OP_XY,PACK)
DEFFUNC(put,no_rnd,xy,8,OP_XY,PACK)
DEFFUNC(put, rnd,o,16,OP_C,avg32)
DEFFUNC(put, rnd,x,16,OP_X,avg32)
DEFFUNC(put,no_rnd,x,16,OP_X,avg32)
DEFFUNC(put, rnd,y,16,OP_Y,avg32)
DEFFUNC(put,no_rnd,y,16,OP_Y,avg32)
DEFFUNC(put, rnd,xy,16,OP_XY,PACK)
DEFFUNC(put,no_rnd,xy,16,OP_XY,PACK)
#undef OP
#define OP avg
DEFFUNC(avg, rnd,o,8,OP_C,avg32)
DEFFUNC(avg, rnd,x,8,OP_X,avg32)
DEFFUNC(avg,no_rnd,x,8,OP_X,avg32)
DEFFUNC(avg, rnd,y,8,OP_Y,avg32)
DEFFUNC(avg,no_rnd,y,8,OP_Y,avg32)
DEFFUNC(avg, rnd,xy,8,OP_XY,PACK)
DEFFUNC(avg,no_rnd,xy,8,OP_XY,PACK)
DEFFUNC(avg, rnd,o,16,OP_C,avg32)
DEFFUNC(avg, rnd,x,16,OP_X,avg32)
DEFFUNC(avg,no_rnd,x,16,OP_X,avg32)
DEFFUNC(avg, rnd,y,16,OP_Y,avg32)
DEFFUNC(avg,no_rnd,y,16,OP_Y,avg32)
DEFFUNC(avg, rnd,xy,16,OP_XY,PACK)
DEFFUNC(avg,no_rnd,xy,16,OP_XY,PACK)
#undef OP
#define put_no_rnd_pixels8_o put_rnd_pixels8_o
#define put_no_rnd_pixels16_o put_rnd_pixels16_o
#define avg_no_rnd_pixels8_o avg_rnd_pixels8_o
#define avg_no_rnd_pixels16_o avg_rnd_pixels16_o
#define put_pixels8_c put_rnd_pixels8_o
#define put_pixels16_c put_rnd_pixels16_o
#define avg_pixels8_c avg_rnd_pixels8_o
#define avg_pixels16_c avg_rnd_pixels16_o
#define put_no_rnd_pixels8_c put_rnd_pixels8_o
#define put_no_rnd_pixels16_c put_rnd_pixels16_o
#define avg_no_rnd_pixels8_c avg_rnd_pixels8_o
#define avg_no_rnd_pixels16_c avg_rnd_pixels16_o
#define QPEL
#ifdef QPEL
#include "qpel.c"
#endif
void dsputil_init_align(DSPContext* c, AVCodecContext *avctx)
{
c->put_pixels_tab[0][0] = put_rnd_pixels16_o;
c->put_pixels_tab[0][1] = put_rnd_pixels16_x;
c->put_pixels_tab[0][2] = put_rnd_pixels16_y;
c->put_pixels_tab[0][3] = put_rnd_pixels16_xy;
c->put_pixels_tab[1][0] = put_rnd_pixels8_o;
c->put_pixels_tab[1][1] = put_rnd_pixels8_x;
c->put_pixels_tab[1][2] = put_rnd_pixels8_y;
c->put_pixels_tab[1][3] = put_rnd_pixels8_xy;
c->put_no_rnd_pixels_tab[0][0] = put_no_rnd_pixels16_o;
c->put_no_rnd_pixels_tab[0][1] = put_no_rnd_pixels16_x;
c->put_no_rnd_pixels_tab[0][2] = put_no_rnd_pixels16_y;
c->put_no_rnd_pixels_tab[0][3] = put_no_rnd_pixels16_xy;
c->put_no_rnd_pixels_tab[1][0] = put_no_rnd_pixels8_o;
c->put_no_rnd_pixels_tab[1][1] = put_no_rnd_pixels8_x;
c->put_no_rnd_pixels_tab[1][2] = put_no_rnd_pixels8_y;
c->put_no_rnd_pixels_tab[1][3] = put_no_rnd_pixels8_xy;
c->avg_pixels_tab[0][0] = avg_rnd_pixels16_o;
c->avg_pixels_tab[0][1] = avg_rnd_pixels16_x;
c->avg_pixels_tab[0][2] = avg_rnd_pixels16_y;
c->avg_pixels_tab[0][3] = avg_rnd_pixels16_xy;
c->avg_pixels_tab[1][0] = avg_rnd_pixels8_o;
c->avg_pixels_tab[1][1] = avg_rnd_pixels8_x;
c->avg_pixels_tab[1][2] = avg_rnd_pixels8_y;
c->avg_pixels_tab[1][3] = avg_rnd_pixels8_xy;
c->avg_no_rnd_pixels_tab[0][0] = avg_no_rnd_pixels16_o;
c->avg_no_rnd_pixels_tab[0][1] = avg_no_rnd_pixels16_x;
c->avg_no_rnd_pixels_tab[0][2] = avg_no_rnd_pixels16_y;
c->avg_no_rnd_pixels_tab[0][3] = avg_no_rnd_pixels16_xy;
c->avg_no_rnd_pixels_tab[1][0] = avg_no_rnd_pixels8_o;
c->avg_no_rnd_pixels_tab[1][1] = avg_no_rnd_pixels8_x;
c->avg_no_rnd_pixels_tab[1][2] = avg_no_rnd_pixels8_y;
c->avg_no_rnd_pixels_tab[1][3] = avg_no_rnd_pixels8_xy;
#ifdef QPEL
#define dspfunc(PFX, IDX, NUM) \
c->PFX ## _pixels_tab[IDX][ 0] = PFX ## NUM ## _mc00_sh4; \
c->PFX ## _pixels_tab[IDX][ 1] = PFX ## NUM ## _mc10_sh4; \
c->PFX ## _pixels_tab[IDX][ 2] = PFX ## NUM ## _mc20_sh4; \
c->PFX ## _pixels_tab[IDX][ 3] = PFX ## NUM ## _mc30_sh4; \
c->PFX ## _pixels_tab[IDX][ 4] = PFX ## NUM ## _mc01_sh4; \
c->PFX ## _pixels_tab[IDX][ 5] = PFX ## NUM ## _mc11_sh4; \
c->PFX ## _pixels_tab[IDX][ 6] = PFX ## NUM ## _mc21_sh4; \
c->PFX ## _pixels_tab[IDX][ 7] = PFX ## NUM ## _mc31_sh4; \
c->PFX ## _pixels_tab[IDX][ 8] = PFX ## NUM ## _mc02_sh4; \
c->PFX ## _pixels_tab[IDX][ 9] = PFX ## NUM ## _mc12_sh4; \
c->PFX ## _pixels_tab[IDX][10] = PFX ## NUM ## _mc22_sh4; \
c->PFX ## _pixels_tab[IDX][11] = PFX ## NUM ## _mc32_sh4; \
c->PFX ## _pixels_tab[IDX][12] = PFX ## NUM ## _mc03_sh4; \
c->PFX ## _pixels_tab[IDX][13] = PFX ## NUM ## _mc13_sh4; \
c->PFX ## _pixels_tab[IDX][14] = PFX ## NUM ## _mc23_sh4; \
c->PFX ## _pixels_tab[IDX][15] = PFX ## NUM ## _mc33_sh4
dspfunc(put_qpel, 0, 16);
dspfunc(put_no_rnd_qpel, 0, 16);
dspfunc(avg_qpel, 0, 16);
/* dspfunc(avg_no_rnd_qpel, 0, 16); */
dspfunc(put_qpel, 1, 8);
dspfunc(put_no_rnd_qpel, 1, 8);
dspfunc(avg_qpel, 1, 8);
/* dspfunc(avg_no_rnd_qpel, 1, 8); */
dspfunc(put_h264_qpel, 0, 16);
dspfunc(put_h264_qpel, 1, 8);
dspfunc(put_h264_qpel, 2, 4);
dspfunc(avg_h264_qpel, 0, 16);
dspfunc(avg_h264_qpel, 1, 8);
dspfunc(avg_h264_qpel, 2, 4);
#undef dspfunc
c->put_h264_chroma_pixels_tab[0]= put_h264_chroma_mc8_sh4;
c->put_h264_chroma_pixels_tab[1]= put_h264_chroma_mc4_sh4;
c->put_h264_chroma_pixels_tab[2]= put_h264_chroma_mc2_sh4;
c->avg_h264_chroma_pixels_tab[0]= avg_h264_chroma_mc8_sh4;
c->avg_h264_chroma_pixels_tab[1]= avg_h264_chroma_mc4_sh4;
c->avg_h264_chroma_pixels_tab[2]= avg_h264_chroma_mc2_sh4;
c->put_mspel_pixels_tab[0]= put_mspel8_mc00_sh4;
c->put_mspel_pixels_tab[1]= put_mspel8_mc10_sh4;
c->put_mspel_pixels_tab[2]= put_mspel8_mc20_sh4;
c->put_mspel_pixels_tab[3]= put_mspel8_mc30_sh4;
c->put_mspel_pixels_tab[4]= put_mspel8_mc02_sh4;
c->put_mspel_pixels_tab[5]= put_mspel8_mc12_sh4;
c->put_mspel_pixels_tab[6]= put_mspel8_mc22_sh4;
c->put_mspel_pixels_tab[7]= put_mspel8_mc32_sh4;
c->gmc1 = gmc1_c;
c->gmc = gmc_c;
#endif
}
| 123linslouis-android-video-cutter | jni/libavcodec/sh4/dsputil_align.c | C | asf20 | 14,740 |
OBJS += sh4/dsputil_align.o \
sh4/dsputil_sh4.o \
sh4/idct_sh4.o \
| 123linslouis-android-video-cutter | jni/libavcodec/sh4/Makefile | Makefile | asf20 | 222 |
/*
* sh4 dsputil
*
* Copyright (c) 2003 BERO <bero@geocities.co.jp>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "libavcodec/avcodec.h"
#include "libavcodec/dsputil.h"
#include "dsputil_sh4.h"
#include "sh4.h"
static void memzero_align8(void *dst,size_t size)
{
int fpscr;
fp_single_enter(fpscr);
dst = (char *)dst + size;
size /= 32;
__asm__ volatile (
" fldi0 fr0\n"
" fldi0 fr1\n"
" fschg\n" // double
"1: \n" \
" dt %1\n"
" fmov dr0,@-%0\n"
" fmov dr0,@-%0\n"
" fmov dr0,@-%0\n"
" bf.s 1b\n"
" fmov dr0,@-%0\n"
" fschg" //back to single
: "+r"(dst),"+r"(size) :: "memory" );
fp_single_leave(fpscr);
}
static void clear_blocks_sh4(DCTELEM *blocks)
{
memzero_align8(blocks,sizeof(DCTELEM)*6*64);
}
static void idct_put(uint8_t *dest, int line_size, DCTELEM *block)
{
int i;
uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
idct_sh4(block);
for(i=0;i<8;i++) {
dest[0] = cm[block[0]];
dest[1] = cm[block[1]];
dest[2] = cm[block[2]];
dest[3] = cm[block[3]];
dest[4] = cm[block[4]];
dest[5] = cm[block[5]];
dest[6] = cm[block[6]];
dest[7] = cm[block[7]];
dest+=line_size;
block+=8;
}
}
static void idct_add(uint8_t *dest, int line_size, DCTELEM *block)
{
int i;
uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
idct_sh4(block);
for(i=0;i<8;i++) {
dest[0] = cm[dest[0]+block[0]];
dest[1] = cm[dest[1]+block[1]];
dest[2] = cm[dest[2]+block[2]];
dest[3] = cm[dest[3]+block[3]];
dest[4] = cm[dest[4]+block[4]];
dest[5] = cm[dest[5]+block[5]];
dest[6] = cm[dest[6]+block[6]];
dest[7] = cm[dest[7]+block[7]];
dest+=line_size;
block+=8;
}
}
void dsputil_init_sh4(DSPContext* c, AVCodecContext *avctx)
{
const int idct_algo= avctx->idct_algo;
dsputil_init_align(c,avctx);
c->clear_blocks = clear_blocks_sh4;
if(idct_algo==FF_IDCT_AUTO || idct_algo==FF_IDCT_SH4){
c->idct_put = idct_put;
c->idct_add = idct_add;
c->idct = idct_sh4;
c->idct_permutation_type= FF_NO_IDCT_PERM;
}
}
| 123linslouis-android-video-cutter | jni/libavcodec/sh4/dsputil_sh4.c | C | asf20 | 3,253 |
/*
* This is optimized for sh, which have post increment addressing (*p++).
* Some CPU may be index (p[n]) faster than post increment (*p++).
*
* copyright (c) 2001-2003 BERO <bero@geocities.co.jp>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define PIXOP2(OPNAME, OP) \
\
static inline void OPNAME ## _pixels4_l2_aligned(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int dst_stride, int src_stride1, int src_stride2, int h) \
{\
do {\
OP(LP(dst ),rnd_avg32(LPC(src1 ),LPC(src2 )) ); \
src1+=src_stride1; \
src2+=src_stride2; \
dst+=dst_stride; \
} while(--h); \
}\
\
static inline void OPNAME ## _pixels4_l2_aligned2(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int dst_stride, int src_stride1, int src_stride2, int h) \
{\
do {\
OP(LP(dst ),rnd_avg32(AV_RN32(src1 ),LPC(src2 )) ); \
src1+=src_stride1; \
src2+=src_stride2; \
dst+=dst_stride; \
} while(--h); \
}\
\
static inline void OPNAME ## _no_rnd_pixels16_l2_aligned2(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int dst_stride, int src_stride1, int src_stride2, int h) \
{\
do {\
OP(LP(dst ),no_rnd_avg32(AV_RN32(src1 ),LPC(src2 )) ); \
OP(LP(dst+4),no_rnd_avg32(AV_RN32(src1+4),LPC(src2+4)) ); \
OP(LP(dst+8),no_rnd_avg32(AV_RN32(src1+8),LPC(src2+8)) ); \
OP(LP(dst+12),no_rnd_avg32(AV_RN32(src1+12),LPC(src2+12)) ); \
src1+=src_stride1; \
src2+=src_stride2; \
dst+=dst_stride; \
} while(--h); \
}\
\
static inline void OPNAME ## _pixels16_l2_aligned2(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int dst_stride, int src_stride1, int src_stride2, int h) \
{\
do {\
OP(LP(dst ),rnd_avg32(AV_RN32(src1 ),LPC(src2 )) ); \
OP(LP(dst+4),rnd_avg32(AV_RN32(src1+4),LPC(src2+4)) ); \
OP(LP(dst+8),rnd_avg32(AV_RN32(src1+8),LPC(src2+8)) ); \
OP(LP(dst+12),rnd_avg32(AV_RN32(src1+12),LPC(src2+12)) ); \
src1+=src_stride1; \
src2+=src_stride2; \
dst+=dst_stride; \
} while(--h); \
}\
\
static inline void OPNAME ## _no_rnd_pixels8_l2_aligned2(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int dst_stride, int src_stride1, int src_stride2, int h) \
{\
do { /* onlye src2 aligned */\
OP(LP(dst ),no_rnd_avg32(AV_RN32(src1 ),LPC(src2 )) ); \
OP(LP(dst+4),no_rnd_avg32(AV_RN32(src1+4),LPC(src2+4)) ); \
src1+=src_stride1; \
src2+=src_stride2; \
dst+=dst_stride; \
} while(--h); \
}\
\
static inline void OPNAME ## _pixels8_l2_aligned2(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int dst_stride, int src_stride1, int src_stride2, int h) \
{\
do {\
OP(LP(dst ),rnd_avg32(AV_RN32(src1 ),LPC(src2 )) ); \
OP(LP(dst+4),rnd_avg32(AV_RN32(src1+4),LPC(src2+4)) ); \
src1+=src_stride1; \
src2+=src_stride2; \
dst+=dst_stride; \
} while(--h); \
}\
\
static inline void OPNAME ## _no_rnd_pixels8_l2_aligned(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int dst_stride, int src_stride1, int src_stride2, int h) \
{\
do {\
OP(LP(dst ),no_rnd_avg32(LPC(src1 ),LPC(src2 )) ); \
OP(LP(dst+4),no_rnd_avg32(LPC(src1+4),LPC(src2+4)) ); \
src1+=src_stride1; \
src2+=src_stride2; \
dst+=dst_stride; \
} while(--h); \
}\
\
static inline void OPNAME ## _pixels8_l2_aligned(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int dst_stride, int src_stride1, int src_stride2, int h) \
{\
do {\
OP(LP(dst ),rnd_avg32(LPC(src1 ),LPC(src2 )) ); \
OP(LP(dst+4),rnd_avg32(LPC(src1+4),LPC(src2+4)) ); \
src1+=src_stride1; \
src2+=src_stride2; \
dst+=dst_stride; \
} while(--h); \
}\
\
static inline void OPNAME ## _no_rnd_pixels16_l2_aligned(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int dst_stride, int src_stride1, int src_stride2, int h) \
{\
do {\
OP(LP(dst ),no_rnd_avg32(LPC(src1 ),LPC(src2 )) ); \
OP(LP(dst+4),no_rnd_avg32(LPC(src1+4),LPC(src2+4)) ); \
OP(LP(dst+8),no_rnd_avg32(LPC(src1+8),LPC(src2+8)) ); \
OP(LP(dst+12),no_rnd_avg32(LPC(src1+12),LPC(src2+12)) ); \
src1+=src_stride1; \
src2+=src_stride2; \
dst+=dst_stride; \
} while(--h); \
}\
\
static inline void OPNAME ## _pixels16_l2_aligned(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int dst_stride, int src_stride1, int src_stride2, int h) \
{\
do {\
OP(LP(dst ),rnd_avg32(LPC(src1 ),LPC(src2 )) ); \
OP(LP(dst+4),rnd_avg32(LPC(src1+4),LPC(src2+4)) ); \
OP(LP(dst+8),rnd_avg32(LPC(src1+8),LPC(src2+8)) ); \
OP(LP(dst+12),rnd_avg32(LPC(src1+12),LPC(src2+12)) ); \
src1+=src_stride1; \
src2+=src_stride2; \
dst+=dst_stride; \
} while(--h); \
}\
\
static inline void OPNAME ## _no_rnd_pixels16_l2_aligned1(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int dst_stride, int src_stride1, int src_stride2, int h) \
{ OPNAME ## _no_rnd_pixels16_l2_aligned2(dst,src2,src1,dst_stride,src_stride2,src_stride1,h); } \
\
static inline void OPNAME ## _pixels16_l2_aligned1(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int dst_stride, int src_stride1, int src_stride2, int h) \
{ OPNAME ## _pixels16_l2_aligned2(dst,src2,src1,dst_stride,src_stride2,src_stride1,h); } \
\
static inline void OPNAME ## _no_rnd_pixels8_l2_aligned1(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int dst_stride, int src_stride1, int src_stride2, int h) \
{ OPNAME ## _no_rnd_pixels8_l2_aligned2(dst,src2,src1,dst_stride,src_stride2,src_stride1,h); } \
\
static inline void OPNAME ## _pixels8_l2_aligned1(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int dst_stride, int src_stride1, int src_stride2, int h) \
{ OPNAME ## _pixels8_l2_aligned2(dst,src2,src1,dst_stride,src_stride2,src_stride1,h); } \
\
static inline void OPNAME ## _pixels8_l4_aligned(uint8_t *dst, const uint8_t *src1, uint8_t *src2, uint8_t *src3, uint8_t *src4,int dst_stride, int src_stride1, int src_stride2,int src_stride3,int src_stride4, int h){\
do { \
uint32_t a0,a1,a2,a3; \
UNPACK(a0,a1,LPC(src1),LPC(src2)); \
UNPACK(a2,a3,LPC(src3),LPC(src4)); \
OP(LP(dst),rnd_PACK(a0,a1,a2,a3)); \
UNPACK(a0,a1,LPC(src1+4),LPC(src2+4)); \
UNPACK(a2,a3,LPC(src3+4),LPC(src4+4)); \
OP(LP(dst+4),rnd_PACK(a0,a1,a2,a3)); \
src1+=src_stride1;\
src2+=src_stride2;\
src3+=src_stride3;\
src4+=src_stride4;\
dst+=dst_stride;\
} while(--h); \
} \
\
static inline void OPNAME ## _no_rnd_pixels8_l4_aligned(uint8_t *dst, const uint8_t *src1, uint8_t *src2, uint8_t *src3, uint8_t *src4,int dst_stride, int src_stride1, int src_stride2,int src_stride3,int src_stride4, int h){\
do { \
uint32_t a0,a1,a2,a3; \
UNPACK(a0,a1,LPC(src1),LPC(src2)); \
UNPACK(a2,a3,LPC(src3),LPC(src4)); \
OP(LP(dst),no_rnd_PACK(a0,a1,a2,a3)); \
UNPACK(a0,a1,LPC(src1+4),LPC(src2+4)); \
UNPACK(a2,a3,LPC(src3+4),LPC(src4+4)); \
OP(LP(dst+4),no_rnd_PACK(a0,a1,a2,a3)); \
src1+=src_stride1;\
src2+=src_stride2;\
src3+=src_stride3;\
src4+=src_stride4;\
dst+=dst_stride;\
} while(--h); \
} \
\
static inline void OPNAME ## _pixels8_l4_aligned0(uint8_t *dst, const uint8_t *src1, uint8_t *src2, uint8_t *src3, uint8_t *src4,int dst_stride, int src_stride1, int src_stride2,int src_stride3,int src_stride4, int h){\
do { \
uint32_t a0,a1,a2,a3; /* src1 only not aligned */\
UNPACK(a0,a1,AV_RN32(src1),LPC(src2)); \
UNPACK(a2,a3,LPC(src3),LPC(src4)); \
OP(LP(dst),rnd_PACK(a0,a1,a2,a3)); \
UNPACK(a0,a1,AV_RN32(src1+4),LPC(src2+4)); \
UNPACK(a2,a3,LPC(src3+4),LPC(src4+4)); \
OP(LP(dst+4),rnd_PACK(a0,a1,a2,a3)); \
src1+=src_stride1;\
src2+=src_stride2;\
src3+=src_stride3;\
src4+=src_stride4;\
dst+=dst_stride;\
} while(--h); \
} \
\
static inline void OPNAME ## _no_rnd_pixels8_l4_aligned0(uint8_t *dst, const uint8_t *src1, uint8_t *src2, uint8_t *src3, uint8_t *src4,int dst_stride, int src_stride1, int src_stride2,int src_stride3,int src_stride4, int h){\
do { \
uint32_t a0,a1,a2,a3; \
UNPACK(a0,a1,AV_RN32(src1),LPC(src2)); \
UNPACK(a2,a3,LPC(src3),LPC(src4)); \
OP(LP(dst),no_rnd_PACK(a0,a1,a2,a3)); \
UNPACK(a0,a1,AV_RN32(src1+4),LPC(src2+4)); \
UNPACK(a2,a3,LPC(src3+4),LPC(src4+4)); \
OP(LP(dst+4),no_rnd_PACK(a0,a1,a2,a3)); \
src1+=src_stride1;\
src2+=src_stride2;\
src3+=src_stride3;\
src4+=src_stride4;\
dst+=dst_stride;\
} while(--h); \
} \
\
static inline void OPNAME ## _pixels16_l4_aligned(uint8_t *dst, const uint8_t *src1, uint8_t *src2, uint8_t *src3, uint8_t *src4,int dst_stride, int src_stride1, int src_stride2,int src_stride3,int src_stride4, int h){\
do { \
uint32_t a0,a1,a2,a3; \
UNPACK(a0,a1,LPC(src1),LPC(src2)); \
UNPACK(a2,a3,LPC(src3),LPC(src4)); \
OP(LP(dst),rnd_PACK(a0,a1,a2,a3)); \
UNPACK(a0,a1,LPC(src1+4),LPC(src2+4)); \
UNPACK(a2,a3,LPC(src3+4),LPC(src4+4)); \
OP(LP(dst+8),rnd_PACK(a0,a1,a2,a3)); \
UNPACK(a0,a1,LPC(src1+8),LPC(src2+8)); \
UNPACK(a2,a3,LPC(src3+8),LPC(src4+8)); \
OP(LP(dst+8),rnd_PACK(a0,a1,a2,a3)); \
UNPACK(a0,a1,LPC(src1+12),LPC(src2+12)); \
UNPACK(a2,a3,LPC(src3+12),LPC(src4+12)); \
OP(LP(dst+12),rnd_PACK(a0,a1,a2,a3)); \
src1+=src_stride1;\
src2+=src_stride2;\
src3+=src_stride3;\
src4+=src_stride4;\
dst+=dst_stride;\
} while(--h); \
} \
\
static inline void OPNAME ## _no_rnd_pixels16_l4_aligned(uint8_t *dst, const uint8_t *src1, uint8_t *src2, uint8_t *src3, uint8_t *src4,int dst_stride, int src_stride1, int src_stride2,int src_stride3,int src_stride4, int h){\
do { \
uint32_t a0,a1,a2,a3; \
UNPACK(a0,a1,LPC(src1),LPC(src2)); \
UNPACK(a2,a3,LPC(src3),LPC(src4)); \
OP(LP(dst),no_rnd_PACK(a0,a1,a2,a3)); \
UNPACK(a0,a1,LPC(src1+4),LPC(src2+4)); \
UNPACK(a2,a3,LPC(src3+4),LPC(src4+4)); \
OP(LP(dst+4),no_rnd_PACK(a0,a1,a2,a3)); \
UNPACK(a0,a1,LPC(src1+8),LPC(src2+8)); \
UNPACK(a2,a3,LPC(src3+8),LPC(src4+8)); \
OP(LP(dst+8),no_rnd_PACK(a0,a1,a2,a3)); \
UNPACK(a0,a1,LPC(src1+12),LPC(src2+12)); \
UNPACK(a2,a3,LPC(src3+12),LPC(src4+12)); \
OP(LP(dst+12),no_rnd_PACK(a0,a1,a2,a3)); \
src1+=src_stride1;\
src2+=src_stride2;\
src3+=src_stride3;\
src4+=src_stride4;\
dst+=dst_stride;\
} while(--h); \
} \
\
static inline void OPNAME ## _pixels16_l4_aligned0(uint8_t *dst, const uint8_t *src1, uint8_t *src2, uint8_t *src3, uint8_t *src4,int dst_stride, int src_stride1, int src_stride2,int src_stride3,int src_stride4, int h){\
do { /* src1 is unaligned */\
uint32_t a0,a1,a2,a3; \
UNPACK(a0,a1,AV_RN32(src1),LPC(src2)); \
UNPACK(a2,a3,LPC(src3),LPC(src4)); \
OP(LP(dst),rnd_PACK(a0,a1,a2,a3)); \
UNPACK(a0,a1,AV_RN32(src1+4),LPC(src2+4)); \
UNPACK(a2,a3,LPC(src3+4),LPC(src4+4)); \
OP(LP(dst+8),rnd_PACK(a0,a1,a2,a3)); \
UNPACK(a0,a1,AV_RN32(src1+8),LPC(src2+8)); \
UNPACK(a2,a3,LPC(src3+8),LPC(src4+8)); \
OP(LP(dst+8),rnd_PACK(a0,a1,a2,a3)); \
UNPACK(a0,a1,AV_RN32(src1+12),LPC(src2+12)); \
UNPACK(a2,a3,LPC(src3+12),LPC(src4+12)); \
OP(LP(dst+12),rnd_PACK(a0,a1,a2,a3)); \
src1+=src_stride1;\
src2+=src_stride2;\
src3+=src_stride3;\
src4+=src_stride4;\
dst+=dst_stride;\
} while(--h); \
} \
\
static inline void OPNAME ## _no_rnd_pixels16_l4_aligned0(uint8_t *dst, const uint8_t *src1, uint8_t *src2, uint8_t *src3, uint8_t *src4,int dst_stride, int src_stride1, int src_stride2,int src_stride3,int src_stride4, int h){\
do { \
uint32_t a0,a1,a2,a3; \
UNPACK(a0,a1,AV_RN32(src1),LPC(src2)); \
UNPACK(a2,a3,LPC(src3),LPC(src4)); \
OP(LP(dst),no_rnd_PACK(a0,a1,a2,a3)); \
UNPACK(a0,a1,AV_RN32(src1+4),LPC(src2+4)); \
UNPACK(a2,a3,LPC(src3+4),LPC(src4+4)); \
OP(LP(dst+4),no_rnd_PACK(a0,a1,a2,a3)); \
UNPACK(a0,a1,AV_RN32(src1+8),LPC(src2+8)); \
UNPACK(a2,a3,LPC(src3+8),LPC(src4+8)); \
OP(LP(dst+8),no_rnd_PACK(a0,a1,a2,a3)); \
UNPACK(a0,a1,AV_RN32(src1+12),LPC(src2+12)); \
UNPACK(a2,a3,LPC(src3+12),LPC(src4+12)); \
OP(LP(dst+12),no_rnd_PACK(a0,a1,a2,a3)); \
src1+=src_stride1;\
src2+=src_stride2;\
src3+=src_stride3;\
src4+=src_stride4;\
dst+=dst_stride;\
} while(--h); \
} \
\
#define op_avg(a, b) a = rnd_avg32(a,b)
#define op_put(a, b) a = b
PIXOP2(avg, op_avg)
PIXOP2(put, op_put)
#undef op_avg
#undef op_put
#define avg2(a,b) ((a+b+1)>>1)
#define avg4(a,b,c,d) ((a+b+c+d+2)>>2)
static void gmc1_c(uint8_t *dst, uint8_t *src, int stride, int h, int x16, int y16, int rounder)
{
const int A=(16-x16)*(16-y16);
const int B=( x16)*(16-y16);
const int C=(16-x16)*( y16);
const int D=( x16)*( y16);
do {
int t0,t1,t2,t3;
uint8_t *s0 = src;
uint8_t *s1 = src+stride;
t0 = *s0++; t2 = *s1++;
t1 = *s0++; t3 = *s1++;
dst[0]= (A*t0 + B*t1 + C*t2 + D*t3 + rounder)>>8;
t0 = *s0++; t2 = *s1++;
dst[1]= (A*t1 + B*t0 + C*t3 + D*t2 + rounder)>>8;
t1 = *s0++; t3 = *s1++;
dst[2]= (A*t0 + B*t1 + C*t2 + D*t3 + rounder)>>8;
t0 = *s0++; t2 = *s1++;
dst[3]= (A*t1 + B*t0 + C*t3 + D*t2 + rounder)>>8;
t1 = *s0++; t3 = *s1++;
dst[4]= (A*t0 + B*t1 + C*t2 + D*t3 + rounder)>>8;
t0 = *s0++; t2 = *s1++;
dst[5]= (A*t1 + B*t0 + C*t3 + D*t2 + rounder)>>8;
t1 = *s0++; t3 = *s1++;
dst[6]= (A*t0 + B*t1 + C*t2 + D*t3 + rounder)>>8;
t0 = *s0++; t2 = *s1++;
dst[7]= (A*t1 + B*t0 + C*t3 + D*t2 + rounder)>>8;
dst+= stride;
src+= stride;
}while(--h);
}
static void gmc_c(uint8_t *dst, uint8_t *src, int stride, int h, int ox, int oy,
int dxx, int dxy, int dyx, int dyy, int shift, int r, int width, int height)
{
int y, vx, vy;
const int s= 1<<shift;
width--;
height--;
for(y=0; y<h; y++){
int x;
vx= ox;
vy= oy;
for(x=0; x<8; x++){ //XXX FIXME optimize
int src_x, src_y, frac_x, frac_y, index;
src_x= vx>>16;
src_y= vy>>16;
frac_x= src_x&(s-1);
frac_y= src_y&(s-1);
src_x>>=shift;
src_y>>=shift;
if((unsigned)src_x < width){
if((unsigned)src_y < height){
index= src_x + src_y*stride;
dst[y*stride + x]= ( ( src[index ]*(s-frac_x)
+ src[index +1]* frac_x )*(s-frac_y)
+ ( src[index+stride ]*(s-frac_x)
+ src[index+stride+1]* frac_x )* frac_y
+ r)>>(shift*2);
}else{
index= src_x + av_clip(src_y, 0, height)*stride;
dst[y*stride + x]= ( ( src[index ]*(s-frac_x)
+ src[index +1]* frac_x )*s
+ r)>>(shift*2);
}
}else{
if((unsigned)src_y < height){
index= av_clip(src_x, 0, width) + src_y*stride;
dst[y*stride + x]= ( ( src[index ]*(s-frac_y)
+ src[index+stride ]* frac_y )*s
+ r)>>(shift*2);
}else{
index= av_clip(src_x, 0, width) + av_clip(src_y, 0, height)*stride;
dst[y*stride + x]= src[index ];
}
}
vx+= dxx;
vy+= dyx;
}
ox += dxy;
oy += dyy;
}
}
#define H264_CHROMA_MC(OPNAME, OP)\
static void OPNAME ## h264_chroma_mc2_sh4(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int stride, int h, int x, int y){\
const int A=(8-x)*(8-y);\
const int B=( x)*(8-y);\
const int C=(8-x)*( y);\
const int D=( x)*( y);\
\
assert(x<8 && y<8 && x>=0 && y>=0);\
\
do {\
int t0,t1,t2,t3; \
uint8_t *s0 = src; \
uint8_t *s1 = src+stride; \
t0 = *s0++; t2 = *s1++; \
t1 = *s0++; t3 = *s1++; \
OP(dst[0], (A*t0 + B*t1 + C*t2 + D*t3));\
t0 = *s0++; t2 = *s1++; \
OP(dst[1], (A*t1 + B*t0 + C*t3 + D*t2));\
dst+= stride;\
src+= stride;\
}while(--h);\
}\
\
static void OPNAME ## h264_chroma_mc4_sh4(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int stride, int h, int x, int y){\
const int A=(8-x)*(8-y);\
const int B=( x)*(8-y);\
const int C=(8-x)*( y);\
const int D=( x)*( y);\
\
assert(x<8 && y<8 && x>=0 && y>=0);\
\
do {\
int t0,t1,t2,t3; \
uint8_t *s0 = src; \
uint8_t *s1 = src+stride; \
t0 = *s0++; t2 = *s1++; \
t1 = *s0++; t3 = *s1++; \
OP(dst[0], (A*t0 + B*t1 + C*t2 + D*t3));\
t0 = *s0++; t2 = *s1++; \
OP(dst[1], (A*t1 + B*t0 + C*t3 + D*t2));\
t1 = *s0++; t3 = *s1++; \
OP(dst[2], (A*t0 + B*t1 + C*t2 + D*t3));\
t0 = *s0++; t2 = *s1++; \
OP(dst[3], (A*t1 + B*t0 + C*t3 + D*t2));\
dst+= stride;\
src+= stride;\
}while(--h);\
}\
\
static void OPNAME ## h264_chroma_mc8_sh4(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int stride, int h, int x, int y){\
const int A=(8-x)*(8-y);\
const int B=( x)*(8-y);\
const int C=(8-x)*( y);\
const int D=( x)*( y);\
\
assert(x<8 && y<8 && x>=0 && y>=0);\
\
do {\
int t0,t1,t2,t3; \
uint8_t *s0 = src; \
uint8_t *s1 = src+stride; \
t0 = *s0++; t2 = *s1++; \
t1 = *s0++; t3 = *s1++; \
OP(dst[0], (A*t0 + B*t1 + C*t2 + D*t3));\
t0 = *s0++; t2 = *s1++; \
OP(dst[1], (A*t1 + B*t0 + C*t3 + D*t2));\
t1 = *s0++; t3 = *s1++; \
OP(dst[2], (A*t0 + B*t1 + C*t2 + D*t3));\
t0 = *s0++; t2 = *s1++; \
OP(dst[3], (A*t1 + B*t0 + C*t3 + D*t2));\
t1 = *s0++; t3 = *s1++; \
OP(dst[4], (A*t0 + B*t1 + C*t2 + D*t3));\
t0 = *s0++; t2 = *s1++; \
OP(dst[5], (A*t1 + B*t0 + C*t3 + D*t2));\
t1 = *s0++; t3 = *s1++; \
OP(dst[6], (A*t0 + B*t1 + C*t2 + D*t3));\
t0 = *s0++; t2 = *s1++; \
OP(dst[7], (A*t1 + B*t0 + C*t3 + D*t2));\
dst+= stride;\
src+= stride;\
}while(--h);\
}
#define op_avg(a, b) a = (((a)+(((b) + 32)>>6)+1)>>1)
#define op_put(a, b) a = (((b) + 32)>>6)
H264_CHROMA_MC(put_ , op_put)
H264_CHROMA_MC(avg_ , op_avg)
#undef op_avg
#undef op_put
#define QPEL_MC(r, OPNAME, RND, OP) \
static void OPNAME ## mpeg4_qpel8_h_lowpass(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int h){\
uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;\
do {\
uint8_t *s = src; \
int src0,src1,src2,src3,src4,src5,src6,src7,src8;\
src0= *s++;\
src1= *s++;\
src2= *s++;\
src3= *s++;\
src4= *s++;\
OP(dst[0], (src0+src1)*20 - (src0+src2)*6 + (src1+src3)*3 - (src2+src4));\
src5= *s++;\
OP(dst[1], (src1+src2)*20 - (src0+src3)*6 + (src0+src4)*3 - (src1+src5));\
src6= *s++;\
OP(dst[2], (src2+src3)*20 - (src1+src4)*6 + (src0+src5)*3 - (src0+src6));\
src7= *s++;\
OP(dst[3], (src3+src4)*20 - (src2+src5)*6 + (src1+src6)*3 - (src0+src7));\
src8= *s++;\
OP(dst[4], (src4+src5)*20 - (src3+src6)*6 + (src2+src7)*3 - (src1+src8));\
OP(dst[5], (src5+src6)*20 - (src4+src7)*6 + (src3+src8)*3 - (src2+src8));\
OP(dst[6], (src6+src7)*20 - (src5+src8)*6 + (src4+src8)*3 - (src3+src7));\
OP(dst[7], (src7+src8)*20 - (src6+src8)*6 + (src5+src7)*3 - (src4+src6));\
dst+=dstStride;\
src+=srcStride;\
}while(--h);\
}\
\
static void OPNAME ## mpeg4_qpel8_v_lowpass(uint8_t *dst, uint8_t *src, int dstStride, int srcStride){\
uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;\
int w=8;\
do{\
uint8_t *s = src, *d=dst;\
int src0,src1,src2,src3,src4,src5,src6,src7,src8;\
src0 = *s; s+=srcStride; \
src1 = *s; s+=srcStride; \
src2 = *s; s+=srcStride; \
src3 = *s; s+=srcStride; \
src4 = *s; s+=srcStride; \
OP(*d, (src0+src1)*20 - (src0+src2)*6 + (src1+src3)*3 - (src2+src4));d+=dstStride;\
src5 = *s; s+=srcStride; \
OP(*d, (src1+src2)*20 - (src0+src3)*6 + (src0+src4)*3 - (src1+src5));d+=dstStride;\
src6 = *s; s+=srcStride; \
OP(*d, (src2+src3)*20 - (src1+src4)*6 + (src0+src5)*3 - (src0+src6));d+=dstStride;\
src7 = *s; s+=srcStride; \
OP(*d, (src3+src4)*20 - (src2+src5)*6 + (src1+src6)*3 - (src0+src7));d+=dstStride;\
src8 = *s; \
OP(*d, (src4+src5)*20 - (src3+src6)*6 + (src2+src7)*3 - (src1+src8));d+=dstStride;\
OP(*d, (src5+src6)*20 - (src4+src7)*6 + (src3+src8)*3 - (src2+src8));d+=dstStride;\
OP(*d, (src6+src7)*20 - (src5+src8)*6 + (src4+src8)*3 - (src3+src7));d+=dstStride;\
OP(*d, (src7+src8)*20 - (src6+src8)*6 + (src5+src7)*3 - (src4+src6));\
dst++;\
src++;\
}while(--w);\
}\
\
static void OPNAME ## mpeg4_qpel16_h_lowpass(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int h){\
uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;\
do {\
uint8_t *s = src;\
int src0,src1,src2,src3,src4,src5,src6,src7,src8;\
int src9,src10,src11,src12,src13,src14,src15,src16;\
src0= *s++;\
src1= *s++;\
src2= *s++;\
src3= *s++;\
src4= *s++;\
OP(dst[ 0], (src0 +src1 )*20 - (src0 +src2 )*6 + (src1 +src3 )*3 - (src2 +src4 ));\
src5= *s++;\
OP(dst[ 1], (src1 +src2 )*20 - (src0 +src3 )*6 + (src0 +src4 )*3 - (src1 +src5 ));\
src6= *s++;\
OP(dst[ 2], (src2 +src3 )*20 - (src1 +src4 )*6 + (src0 +src5 )*3 - (src0 +src6 ));\
src7= *s++;\
OP(dst[ 3], (src3 +src4 )*20 - (src2 +src5 )*6 + (src1 +src6 )*3 - (src0 +src7 ));\
src8= *s++;\
OP(dst[ 4], (src4 +src5 )*20 - (src3 +src6 )*6 + (src2 +src7 )*3 - (src1 +src8 ));\
src9= *s++;\
OP(dst[ 5], (src5 +src6 )*20 - (src4 +src7 )*6 + (src3 +src8 )*3 - (src2 +src9 ));\
src10= *s++;\
OP(dst[ 6], (src6 +src7 )*20 - (src5 +src8 )*6 + (src4 +src9 )*3 - (src3 +src10));\
src11= *s++;\
OP(dst[ 7], (src7 +src8 )*20 - (src6 +src9 )*6 + (src5 +src10)*3 - (src4 +src11));\
src12= *s++;\
OP(dst[ 8], (src8 +src9 )*20 - (src7 +src10)*6 + (src6 +src11)*3 - (src5 +src12));\
src13= *s++;\
OP(dst[ 9], (src9 +src10)*20 - (src8 +src11)*6 + (src7 +src12)*3 - (src6 +src13));\
src14= *s++;\
OP(dst[10], (src10+src11)*20 - (src9 +src12)*6 + (src8 +src13)*3 - (src7 +src14));\
src15= *s++;\
OP(dst[11], (src11+src12)*20 - (src10+src13)*6 + (src9 +src14)*3 - (src8 +src15));\
src16= *s++;\
OP(dst[12], (src12+src13)*20 - (src11+src14)*6 + (src10+src15)*3 - (src9 +src16));\
OP(dst[13], (src13+src14)*20 - (src12+src15)*6 + (src11+src16)*3 - (src10+src16));\
OP(dst[14], (src14+src15)*20 - (src13+src16)*6 + (src12+src16)*3 - (src11+src15));\
OP(dst[15], (src15+src16)*20 - (src14+src16)*6 + (src13+src15)*3 - (src12+src14));\
dst+=dstStride;\
src+=srcStride;\
}while(--h);\
}\
\
static void OPNAME ## mpeg4_qpel16_v_lowpass(uint8_t *dst, uint8_t *src, int dstStride, int srcStride){\
uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;\
int w=16;\
do {\
uint8_t *s = src, *d=dst;\
int src0,src1,src2,src3,src4,src5,src6,src7,src8;\
int src9,src10,src11,src12,src13,src14,src15,src16;\
src0 = *s; s+=srcStride; \
src1 = *s; s+=srcStride; \
src2 = *s; s+=srcStride; \
src3 = *s; s+=srcStride; \
src4 = *s; s+=srcStride; \
OP(*d, (src0 +src1 )*20 - (src0 +src2 )*6 + (src1 +src3 )*3 - (src2 +src4 ));d+=dstStride;\
src5 = *s; s+=srcStride; \
OP(*d, (src1 +src2 )*20 - (src0 +src3 )*6 + (src0 +src4 )*3 - (src1 +src5 ));d+=dstStride;\
src6 = *s; s+=srcStride; \
OP(*d, (src2 +src3 )*20 - (src1 +src4 )*6 + (src0 +src5 )*3 - (src0 +src6 ));d+=dstStride;\
src7 = *s; s+=srcStride; \
OP(*d, (src3 +src4 )*20 - (src2 +src5 )*6 + (src1 +src6 )*3 - (src0 +src7 ));d+=dstStride;\
src8 = *s; s+=srcStride; \
OP(*d, (src4 +src5 )*20 - (src3 +src6 )*6 + (src2 +src7 )*3 - (src1 +src8 ));d+=dstStride;\
src9 = *s; s+=srcStride; \
OP(*d, (src5 +src6 )*20 - (src4 +src7 )*6 + (src3 +src8 )*3 - (src2 +src9 ));d+=dstStride;\
src10 = *s; s+=srcStride; \
OP(*d, (src6 +src7 )*20 - (src5 +src8 )*6 + (src4 +src9 )*3 - (src3 +src10));d+=dstStride;\
src11 = *s; s+=srcStride; \
OP(*d, (src7 +src8 )*20 - (src6 +src9 )*6 + (src5 +src10)*3 - (src4 +src11));d+=dstStride;\
src12 = *s; s+=srcStride; \
OP(*d, (src8 +src9 )*20 - (src7 +src10)*6 + (src6 +src11)*3 - (src5 +src12));d+=dstStride;\
src13 = *s; s+=srcStride; \
OP(*d, (src9 +src10)*20 - (src8 +src11)*6 + (src7 +src12)*3 - (src6 +src13));d+=dstStride;\
src14 = *s; s+=srcStride; \
OP(*d, (src10+src11)*20 - (src9 +src12)*6 + (src8 +src13)*3 - (src7 +src14));d+=dstStride;\
src15 = *s; s+=srcStride; \
OP(*d, (src11+src12)*20 - (src10+src13)*6 + (src9 +src14)*3 - (src8 +src15));d+=dstStride;\
src16 = *s; \
OP(*d, (src12+src13)*20 - (src11+src14)*6 + (src10+src15)*3 - (src9 +src16));d+=dstStride;\
OP(*d, (src13+src14)*20 - (src12+src15)*6 + (src11+src16)*3 - (src10+src16));d+=dstStride;\
OP(*d, (src14+src15)*20 - (src13+src16)*6 + (src12+src16)*3 - (src11+src15));d+=dstStride;\
OP(*d, (src15+src16)*20 - (src14+src16)*6 + (src13+src15)*3 - (src12+src14));\
dst++;\
src++;\
}while(--w);\
}\
\
static void OPNAME ## qpel8_mc00_sh4 (uint8_t *dst, uint8_t *src, int stride){\
OPNAME ## pixels8_c(dst, src, stride, 8);\
}\
\
static void OPNAME ## qpel8_mc10_sh4(uint8_t *dst, uint8_t *src, int stride){\
uint8_t half[64];\
put ## RND ## mpeg4_qpel8_h_lowpass(half, src, 8, stride, 8);\
OPNAME ## pixels8_l2_aligned2(dst, src, half, stride, stride, 8, 8);\
}\
\
static void OPNAME ## qpel8_mc20_sh4(uint8_t *dst, uint8_t *src, int stride){\
OPNAME ## mpeg4_qpel8_h_lowpass(dst, src, stride, stride, 8);\
}\
\
static void OPNAME ## qpel8_mc30_sh4(uint8_t *dst, uint8_t *src, int stride){\
uint8_t half[64];\
put ## RND ## mpeg4_qpel8_h_lowpass(half, src, 8, stride, 8);\
OPNAME ## pixels8_l2_aligned2(dst, src+1, half, stride, stride, 8, 8);\
}\
\
static void OPNAME ## qpel8_mc01_sh4(uint8_t *dst, uint8_t *src, int stride){\
uint8_t full[16*9];\
uint8_t half[64];\
copy_block9(full, src, 16, stride, 9);\
put ## RND ## mpeg4_qpel8_v_lowpass(half, full, 8, 16);\
OPNAME ## pixels8_l2_aligned(dst, full, half, stride, 16, 8, 8);\
}\
\
static void OPNAME ## qpel8_mc02_sh4(uint8_t *dst, uint8_t *src, int stride){\
uint8_t full[16*9];\
copy_block9(full, src, 16, stride, 9);\
OPNAME ## mpeg4_qpel8_v_lowpass(dst, full, stride, 16);\
}\
\
static void OPNAME ## qpel8_mc03_sh4(uint8_t *dst, uint8_t *src, int stride){\
uint8_t full[16*9];\
uint8_t half[64];\
copy_block9(full, src, 16, stride, 9);\
put ## RND ## mpeg4_qpel8_v_lowpass(half, full, 8, 16);\
OPNAME ## pixels8_l2_aligned(dst, full+16, half, stride, 16, 8, 8);\
}\
static void OPNAME ## qpel8_mc11_sh4(uint8_t *dst, uint8_t *src, int stride){\
uint8_t full[16*9];\
uint8_t halfH[72];\
uint8_t halfHV[64];\
copy_block9(full, src, 16, stride, 9);\
put ## RND ## mpeg4_qpel8_h_lowpass(halfH, full, 8, 16, 9);\
put ## RND ## pixels8_l2_aligned(halfH, halfH, full, 8, 8, 16, 9);\
put ## RND ## mpeg4_qpel8_v_lowpass(halfHV, halfH, 8, 8);\
OPNAME ## pixels8_l2_aligned(dst, halfH, halfHV, stride, 8, 8, 8);\
}\
static void OPNAME ## qpel8_mc31_sh4(uint8_t *dst, uint8_t *src, int stride){\
uint8_t full[16*9];\
uint8_t halfH[72];\
uint8_t halfHV[64];\
copy_block9(full, src, 16, stride, 9);\
put ## RND ## mpeg4_qpel8_h_lowpass(halfH, full, 8, 16, 9);\
put ## RND ## pixels8_l2_aligned1(halfH, halfH, full+1, 8, 8, 16, 9);\
put ## RND ## mpeg4_qpel8_v_lowpass(halfHV, halfH, 8, 8);\
OPNAME ## pixels8_l2_aligned(dst, halfH, halfHV, stride, 8, 8, 8);\
}\
static void OPNAME ## qpel8_mc13_sh4(uint8_t *dst, uint8_t *src, int stride){\
uint8_t full[16*9];\
uint8_t halfH[72];\
uint8_t halfHV[64];\
copy_block9(full, src, 16, stride, 9);\
put ## RND ## mpeg4_qpel8_h_lowpass(halfH, full, 8, 16, 9);\
put ## RND ## pixels8_l2_aligned(halfH, halfH, full, 8, 8, 16, 9);\
put ## RND ## mpeg4_qpel8_v_lowpass(halfHV, halfH, 8, 8);\
OPNAME ## pixels8_l2_aligned(dst, halfH+8, halfHV, stride, 8, 8, 8);\
}\
static void OPNAME ## qpel8_mc33_sh4(uint8_t *dst, uint8_t *src, int stride){\
uint8_t full[16*9];\
uint8_t halfH[72];\
uint8_t halfHV[64];\
copy_block9(full, src, 16, stride, 9);\
put ## RND ## mpeg4_qpel8_h_lowpass(halfH, full, 8, 16, 9);\
put ## RND ## pixels8_l2_aligned1(halfH, halfH, full+1, 8, 8, 16, 9);\
put ## RND ## mpeg4_qpel8_v_lowpass(halfHV, halfH, 8, 8);\
OPNAME ## pixels8_l2_aligned(dst, halfH+8, halfHV, stride, 8, 8, 8);\
}\
static void OPNAME ## qpel8_mc21_sh4(uint8_t *dst, uint8_t *src, int stride){\
uint8_t halfH[72];\
uint8_t halfHV[64];\
put ## RND ## mpeg4_qpel8_h_lowpass(halfH, src, 8, stride, 9);\
put ## RND ## mpeg4_qpel8_v_lowpass(halfHV, halfH, 8, 8);\
OPNAME ## pixels8_l2_aligned(dst, halfH, halfHV, stride, 8, 8, 8);\
}\
static void OPNAME ## qpel8_mc23_sh4(uint8_t *dst, uint8_t *src, int stride){\
uint8_t halfH[72];\
uint8_t halfHV[64];\
put ## RND ## mpeg4_qpel8_h_lowpass(halfH, src, 8, stride, 9);\
put ## RND ## mpeg4_qpel8_v_lowpass(halfHV, halfH, 8, 8);\
OPNAME ## pixels8_l2_aligned(dst, halfH+8, halfHV, stride, 8, 8, 8);\
}\
static void OPNAME ## qpel8_mc12_sh4(uint8_t *dst, uint8_t *src, int stride){\
uint8_t full[16*9];\
uint8_t halfH[72];\
copy_block9(full, src, 16, stride, 9);\
put ## RND ## mpeg4_qpel8_h_lowpass(halfH, full, 8, 16, 9);\
put ## RND ## pixels8_l2_aligned(halfH, halfH, full, 8, 8, 16, 9);\
OPNAME ## mpeg4_qpel8_v_lowpass(dst, halfH, stride, 8);\
}\
static void OPNAME ## qpel8_mc32_sh4(uint8_t *dst, uint8_t *src, int stride){\
uint8_t full[16*9];\
uint8_t halfH[72];\
copy_block9(full, src, 16, stride, 9);\
put ## RND ## mpeg4_qpel8_h_lowpass(halfH, full, 8, 16, 9);\
put ## RND ## pixels8_l2_aligned1(halfH, halfH, full+1, 8, 8, 16, 9);\
OPNAME ## mpeg4_qpel8_v_lowpass(dst, halfH, stride, 8);\
}\
static void OPNAME ## qpel8_mc22_sh4(uint8_t *dst, uint8_t *src, int stride){\
uint8_t halfH[72];\
put ## RND ## mpeg4_qpel8_h_lowpass(halfH, src, 8, stride, 9);\
OPNAME ## mpeg4_qpel8_v_lowpass(dst, halfH, stride, 8);\
}\
static void OPNAME ## qpel16_mc00_sh4 (uint8_t *dst, uint8_t *src, int stride){\
OPNAME ## pixels16_c(dst, src, stride, 16);\
}\
\
static void OPNAME ## qpel16_mc10_sh4(uint8_t *dst, uint8_t *src, int stride){\
uint8_t half[256];\
put ## RND ## mpeg4_qpel16_h_lowpass(half, src, 16, stride, 16);\
OPNAME ## pixels16_l2_aligned2(dst, src, half, stride, stride, 16, 16);\
}\
\
static void OPNAME ## qpel16_mc20_sh4(uint8_t *dst, uint8_t *src, int stride){\
OPNAME ## mpeg4_qpel16_h_lowpass(dst, src, stride, stride, 16);\
}\
\
static void OPNAME ## qpel16_mc30_sh4(uint8_t *dst, uint8_t *src, int stride){\
uint8_t half[256];\
put ## RND ## mpeg4_qpel16_h_lowpass(half, src, 16, stride, 16);\
OPNAME ## pixels16_l2_aligned2(dst, src+1, half, stride, stride, 16, 16);\
}\
\
static void OPNAME ## qpel16_mc01_sh4(uint8_t *dst, uint8_t *src, int stride){\
uint8_t full[24*17];\
uint8_t half[256];\
copy_block17(full, src, 24, stride, 17);\
put ## RND ## mpeg4_qpel16_v_lowpass(half, full, 16, 24);\
OPNAME ## pixels16_l2_aligned(dst, full, half, stride, 24, 16, 16);\
}\
\
static void OPNAME ## qpel16_mc02_sh4(uint8_t *dst, uint8_t *src, int stride){\
uint8_t full[24*17];\
copy_block17(full, src, 24, stride, 17);\
OPNAME ## mpeg4_qpel16_v_lowpass(dst, full, stride, 24);\
}\
\
static void OPNAME ## qpel16_mc03_sh4(uint8_t *dst, uint8_t *src, int stride){\
uint8_t full[24*17];\
uint8_t half[256];\
copy_block17(full, src, 24, stride, 17);\
put ## RND ## mpeg4_qpel16_v_lowpass(half, full, 16, 24);\
OPNAME ## pixels16_l2_aligned(dst, full+24, half, stride, 24, 16, 16);\
}\
static void OPNAME ## qpel16_mc11_sh4(uint8_t *dst, uint8_t *src, int stride){\
uint8_t full[24*17];\
uint8_t halfH[272];\
uint8_t halfHV[256];\
copy_block17(full, src, 24, stride, 17);\
put ## RND ## mpeg4_qpel16_h_lowpass(halfH, full, 16, 24, 17);\
put ## RND ## pixels16_l2_aligned(halfH, halfH, full, 16, 16, 24, 17);\
put ## RND ## mpeg4_qpel16_v_lowpass(halfHV, halfH, 16, 16);\
OPNAME ## pixels16_l2_aligned(dst, halfH, halfHV, stride, 16, 16, 16);\
}\
static void OPNAME ## qpel16_mc31_sh4(uint8_t *dst, uint8_t *src, int stride){\
uint8_t full[24*17];\
uint8_t halfH[272];\
uint8_t halfHV[256];\
copy_block17(full, src, 24, stride, 17);\
put ## RND ## mpeg4_qpel16_h_lowpass(halfH, full, 16, 24, 17);\
put ## RND ## pixels16_l2_aligned1(halfH, halfH, full+1, 16, 16, 24, 17);\
put ## RND ## mpeg4_qpel16_v_lowpass(halfHV, halfH, 16, 16);\
OPNAME ## pixels16_l2_aligned(dst, halfH, halfHV, stride, 16, 16, 16);\
}\
static void OPNAME ## qpel16_mc13_sh4(uint8_t *dst, uint8_t *src, int stride){\
uint8_t full[24*17];\
uint8_t halfH[272];\
uint8_t halfHV[256];\
copy_block17(full, src, 24, stride, 17);\
put ## RND ## mpeg4_qpel16_h_lowpass(halfH, full, 16, 24, 17);\
put ## RND ## pixels16_l2_aligned(halfH, halfH, full, 16, 16, 24, 17);\
put ## RND ## mpeg4_qpel16_v_lowpass(halfHV, halfH, 16, 16);\
OPNAME ## pixels16_l2_aligned(dst, halfH+16, halfHV, stride, 16, 16, 16);\
}\
static void OPNAME ## qpel16_mc33_sh4(uint8_t *dst, uint8_t *src, int stride){\
uint8_t full[24*17];\
uint8_t halfH[272];\
uint8_t halfHV[256];\
copy_block17(full, src, 24, stride, 17);\
put ## RND ## mpeg4_qpel16_h_lowpass(halfH, full, 16, 24, 17);\
put ## RND ## pixels16_l2_aligned1(halfH, halfH, full+1, 16, 16, 24, 17);\
put ## RND ## mpeg4_qpel16_v_lowpass(halfHV, halfH, 16, 16);\
OPNAME ## pixels16_l2_aligned(dst, halfH+16, halfHV, stride, 16, 16, 16);\
}\
static void OPNAME ## qpel16_mc21_sh4(uint8_t *dst, uint8_t *src, int stride){\
uint8_t halfH[272];\
uint8_t halfHV[256];\
put ## RND ## mpeg4_qpel16_h_lowpass(halfH, src, 16, stride, 17);\
put ## RND ## mpeg4_qpel16_v_lowpass(halfHV, halfH, 16, 16);\
OPNAME ## pixels16_l2_aligned(dst, halfH, halfHV, stride, 16, 16, 16);\
}\
static void OPNAME ## qpel16_mc23_sh4(uint8_t *dst, uint8_t *src, int stride){\
uint8_t halfH[272];\
uint8_t halfHV[256];\
put ## RND ## mpeg4_qpel16_h_lowpass(halfH, src, 16, stride, 17);\
put ## RND ## mpeg4_qpel16_v_lowpass(halfHV, halfH, 16, 16);\
OPNAME ## pixels16_l2_aligned(dst, halfH+16, halfHV, stride, 16, 16, 16);\
}\
static void OPNAME ## qpel16_mc12_sh4(uint8_t *dst, uint8_t *src, int stride){\
uint8_t full[24*17];\
uint8_t halfH[272];\
copy_block17(full, src, 24, stride, 17);\
put ## RND ## mpeg4_qpel16_h_lowpass(halfH, full, 16, 24, 17);\
put ## RND ## pixels16_l2_aligned(halfH, halfH, full, 16, 16, 24, 17);\
OPNAME ## mpeg4_qpel16_v_lowpass(dst, halfH, stride, 16);\
}\
static void OPNAME ## qpel16_mc32_sh4(uint8_t *dst, uint8_t *src, int stride){\
uint8_t full[24*17];\
uint8_t halfH[272];\
copy_block17(full, src, 24, stride, 17);\
put ## RND ## mpeg4_qpel16_h_lowpass(halfH, full, 16, 24, 17);\
put ## RND ## pixels16_l2_aligned1(halfH, halfH, full+1, 16, 16, 24, 17);\
OPNAME ## mpeg4_qpel16_v_lowpass(dst, halfH, stride, 16);\
}\
static void OPNAME ## qpel16_mc22_sh4(uint8_t *dst, uint8_t *src, int stride){\
uint8_t halfH[272];\
put ## RND ## mpeg4_qpel16_h_lowpass(halfH, src, 16, stride, 17);\
OPNAME ## mpeg4_qpel16_v_lowpass(dst, halfH, stride, 16);\
}
#define op_avg(a, b) a = (((a)+cm[((b) + 16)>>5]+1)>>1)
#define op_avg_no_rnd(a, b) a = (((a)+cm[((b) + 15)>>5])>>1)
#define op_put(a, b) a = cm[((b) + 16)>>5]
#define op_put_no_rnd(a, b) a = cm[((b) + 15)>>5]
QPEL_MC(0, put_ , _ , op_put)
QPEL_MC(1, put_no_rnd_, _no_rnd_, op_put_no_rnd)
QPEL_MC(0, avg_ , _ , op_avg)
//QPEL_MC(1, avg_no_rnd , _ , op_avg)
#undef op_avg
#undef op_avg_no_rnd
#undef op_put
#undef op_put_no_rnd
#if 1
#define H264_LOWPASS(OPNAME, OP, OP2) \
static inline void OPNAME ## h264_qpel_h_lowpass(uint8_t *dst, uint8_t *src, int dstStride, int srcStride,int w,int h){\
uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;\
do {\
int srcB,srcA,src0,src1,src2,src3,src4,src5,src6;\
uint8_t *s = src-2;\
srcB = *s++;\
srcA = *s++;\
src0 = *s++;\
src1 = *s++;\
src2 = *s++;\
src3 = *s++;\
OP(dst[0], (src0+src1)*20 - (srcA+src2)*5 + (srcB+src3));\
src4 = *s++;\
OP(dst[1], (src1+src2)*20 - (src0+src3)*5 + (srcA+src4));\
src5 = *s++;\
OP(dst[2], (src2+src3)*20 - (src1+src4)*5 + (src0+src5));\
src6 = *s++;\
OP(dst[3], (src3+src4)*20 - (src2+src5)*5 + (src1+src6));\
if (w>4) { /* it optimized */ \
int src7,src8,src9,src10; \
src7 = *s++;\
OP(dst[4], (src4+src5)*20 - (src3+src6)*5 + (src2+src7));\
src8 = *s++;\
OP(dst[5], (src5+src6)*20 - (src4+src7)*5 + (src3+src8));\
src9 = *s++;\
OP(dst[6], (src6+src7)*20 - (src5+src8)*5 + (src4+src9));\
src10 = *s++;\
OP(dst[7], (src7+src8)*20 - (src6+src9)*5 + (src5+src10));\
if (w>8) { \
int src11,src12,src13,src14,src15,src16,src17,src18; \
src11 = *s++;\
OP(dst[8] , (src8 +src9 )*20 - (src7 +src10)*5 + (src6 +src11));\
src12 = *s++;\
OP(dst[9] , (src9 +src10)*20 - (src8 +src11)*5 + (src7 +src12));\
src13 = *s++;\
OP(dst[10], (src10+src11)*20 - (src9 +src12)*5 + (src8 +src13));\
src14 = *s++;\
OP(dst[11], (src11+src12)*20 - (src10+src13)*5 + (src9 +src14));\
src15 = *s++;\
OP(dst[12], (src12+src13)*20 - (src11+src14)*5 + (src10+src15));\
src16 = *s++;\
OP(dst[13], (src13+src14)*20 - (src12+src15)*5 + (src11+src16));\
src17 = *s++;\
OP(dst[14], (src14+src15)*20 - (src13+src16)*5 + (src12+src17));\
src18 = *s++;\
OP(dst[15], (src15+src16)*20 - (src14+src17)*5 + (src13+src18));\
} \
} \
dst+=dstStride;\
src+=srcStride;\
}while(--h);\
}\
\
static inline void OPNAME ## h264_qpel_v_lowpass(uint8_t *dst, uint8_t *src, int dstStride, int srcStride,int w,int h){\
uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;\
do{\
int srcB,srcA,src0,src1,src2,src3,src4,src5,src6;\
uint8_t *s = src-2*srcStride,*d=dst;\
srcB = *s; s+=srcStride;\
srcA = *s; s+=srcStride;\
src0 = *s; s+=srcStride;\
src1 = *s; s+=srcStride;\
src2 = *s; s+=srcStride;\
src3 = *s; s+=srcStride;\
OP(*d, (src0+src1)*20 - (srcA+src2)*5 + (srcB+src3));d+=dstStride;\
src4 = *s; s+=srcStride;\
OP(*d, (src1+src2)*20 - (src0+src3)*5 + (srcA+src4));d+=dstStride;\
src5 = *s; s+=srcStride;\
OP(*d, (src2+src3)*20 - (src1+src4)*5 + (src0+src5));d+=dstStride;\
src6 = *s; s+=srcStride;\
OP(*d, (src3+src4)*20 - (src2+src5)*5 + (src1+src6));d+=dstStride;\
if (h>4) { \
int src7,src8,src9,src10; \
src7 = *s; s+=srcStride;\
OP(*d, (src4+src5)*20 - (src3+src6)*5 + (src2+src7));d+=dstStride;\
src8 = *s; s+=srcStride;\
OP(*d, (src5+src6)*20 - (src4+src7)*5 + (src3+src8));d+=dstStride;\
src9 = *s; s+=srcStride;\
OP(*d, (src6+src7)*20 - (src5+src8)*5 + (src4+src9));d+=dstStride;\
src10 = *s; s+=srcStride;\
OP(*d, (src7+src8)*20 - (src6+src9)*5 + (src5+src10));d+=dstStride;\
if (h>8) { \
int src11,src12,src13,src14,src15,src16,src17,src18; \
src11 = *s; s+=srcStride;\
OP(*d , (src8 +src9 )*20 - (src7 +src10)*5 + (src6 +src11));d+=dstStride;\
src12 = *s; s+=srcStride;\
OP(*d , (src9 +src10)*20 - (src8 +src11)*5 + (src7 +src12));d+=dstStride;\
src13 = *s; s+=srcStride;\
OP(*d, (src10+src11)*20 - (src9 +src12)*5 + (src8 +src13));d+=dstStride;\
src14 = *s; s+=srcStride;\
OP(*d, (src11+src12)*20 - (src10+src13)*5 + (src9 +src14));d+=dstStride;\
src15 = *s; s+=srcStride;\
OP(*d, (src12+src13)*20 - (src11+src14)*5 + (src10+src15));d+=dstStride;\
src16 = *s; s+=srcStride;\
OP(*d, (src13+src14)*20 - (src12+src15)*5 + (src11+src16));d+=dstStride;\
src17 = *s; s+=srcStride;\
OP(*d, (src14+src15)*20 - (src13+src16)*5 + (src12+src17));d+=dstStride;\
src18 = *s; s+=srcStride;\
OP(*d, (src15+src16)*20 - (src14+src17)*5 + (src13+src18));d+=dstStride;\
} \
} \
dst++;\
src++;\
}while(--w);\
}\
\
static inline void OPNAME ## h264_qpel_hv_lowpass(uint8_t *dst, int16_t *tmp, uint8_t *src, int dstStride, int tmpStride, int srcStride,int w,int h){\
uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;\
int i;\
src -= 2*srcStride;\
i= h+5; \
do {\
int srcB,srcA,src0,src1,src2,src3,src4,src5,src6;\
uint8_t *s = src-2;\
srcB = *s++;\
srcA = *s++;\
src0 = *s++;\
src1 = *s++;\
src2 = *s++;\
src3 = *s++;\
tmp[0] = ((src0+src1)*20 - (srcA+src2)*5 + (srcB+src3));\
src4 = *s++;\
tmp[1] = ((src1+src2)*20 - (src0+src3)*5 + (srcA+src4));\
src5 = *s++;\
tmp[2] = ((src2+src3)*20 - (src1+src4)*5 + (src0+src5));\
src6 = *s++;\
tmp[3] = ((src3+src4)*20 - (src2+src5)*5 + (src1+src6));\
if (w>4) { /* it optimized */ \
int src7,src8,src9,src10; \
src7 = *s++;\
tmp[4] = ((src4+src5)*20 - (src3+src6)*5 + (src2+src7));\
src8 = *s++;\
tmp[5] = ((src5+src6)*20 - (src4+src7)*5 + (src3+src8));\
src9 = *s++;\
tmp[6] = ((src6+src7)*20 - (src5+src8)*5 + (src4+src9));\
src10 = *s++;\
tmp[7] = ((src7+src8)*20 - (src6+src9)*5 + (src5+src10));\
if (w>8) { \
int src11,src12,src13,src14,src15,src16,src17,src18; \
src11 = *s++;\
tmp[8] = ((src8 +src9 )*20 - (src7 +src10)*5 + (src6 +src11));\
src12 = *s++;\
tmp[9] = ((src9 +src10)*20 - (src8 +src11)*5 + (src7 +src12));\
src13 = *s++;\
tmp[10] = ((src10+src11)*20 - (src9 +src12)*5 + (src8 +src13));\
src14 = *s++;\
tmp[11] = ((src11+src12)*20 - (src10+src13)*5 + (src9 +src14));\
src15 = *s++;\
tmp[12] = ((src12+src13)*20 - (src11+src14)*5 + (src10+src15));\
src16 = *s++;\
tmp[13] = ((src13+src14)*20 - (src12+src15)*5 + (src11+src16));\
src17 = *s++;\
tmp[14] = ((src14+src15)*20 - (src13+src16)*5 + (src12+src17));\
src18 = *s++;\
tmp[15] = ((src15+src16)*20 - (src14+src17)*5 + (src13+src18));\
} \
} \
tmp+=tmpStride;\
src+=srcStride;\
}while(--i);\
tmp -= tmpStride*(h+5-2);\
i = w; \
do {\
int tmpB,tmpA,tmp0,tmp1,tmp2,tmp3,tmp4,tmp5,tmp6;\
int16_t *s = tmp-2*tmpStride; \
uint8_t *d=dst;\
tmpB = *s; s+=tmpStride;\
tmpA = *s; s+=tmpStride;\
tmp0 = *s; s+=tmpStride;\
tmp1 = *s; s+=tmpStride;\
tmp2 = *s; s+=tmpStride;\
tmp3 = *s; s+=tmpStride;\
OP2(*d, (tmp0+tmp1)*20 - (tmpA+tmp2)*5 + (tmpB+tmp3));d+=dstStride;\
tmp4 = *s; s+=tmpStride;\
OP2(*d, (tmp1+tmp2)*20 - (tmp0+tmp3)*5 + (tmpA+tmp4));d+=dstStride;\
tmp5 = *s; s+=tmpStride;\
OP2(*d, (tmp2+tmp3)*20 - (tmp1+tmp4)*5 + (tmp0+tmp5));d+=dstStride;\
tmp6 = *s; s+=tmpStride;\
OP2(*d, (tmp3+tmp4)*20 - (tmp2+tmp5)*5 + (tmp1+tmp6));d+=dstStride;\
if (h>4) { \
int tmp7,tmp8,tmp9,tmp10; \
tmp7 = *s; s+=tmpStride;\
OP2(*d, (tmp4+tmp5)*20 - (tmp3+tmp6)*5 + (tmp2+tmp7));d+=dstStride;\
tmp8 = *s; s+=tmpStride;\
OP2(*d, (tmp5+tmp6)*20 - (tmp4+tmp7)*5 + (tmp3+tmp8));d+=dstStride;\
tmp9 = *s; s+=tmpStride;\
OP2(*d, (tmp6+tmp7)*20 - (tmp5+tmp8)*5 + (tmp4+tmp9));d+=dstStride;\
tmp10 = *s; s+=tmpStride;\
OP2(*d, (tmp7+tmp8)*20 - (tmp6+tmp9)*5 + (tmp5+tmp10));d+=dstStride;\
if (h>8) { \
int tmp11,tmp12,tmp13,tmp14,tmp15,tmp16,tmp17,tmp18; \
tmp11 = *s; s+=tmpStride;\
OP2(*d , (tmp8 +tmp9 )*20 - (tmp7 +tmp10)*5 + (tmp6 +tmp11));d+=dstStride;\
tmp12 = *s; s+=tmpStride;\
OP2(*d , (tmp9 +tmp10)*20 - (tmp8 +tmp11)*5 + (tmp7 +tmp12));d+=dstStride;\
tmp13 = *s; s+=tmpStride;\
OP2(*d, (tmp10+tmp11)*20 - (tmp9 +tmp12)*5 + (tmp8 +tmp13));d+=dstStride;\
tmp14 = *s; s+=tmpStride;\
OP2(*d, (tmp11+tmp12)*20 - (tmp10+tmp13)*5 + (tmp9 +tmp14));d+=dstStride;\
tmp15 = *s; s+=tmpStride;\
OP2(*d, (tmp12+tmp13)*20 - (tmp11+tmp14)*5 + (tmp10+tmp15));d+=dstStride;\
tmp16 = *s; s+=tmpStride;\
OP2(*d, (tmp13+tmp14)*20 - (tmp12+tmp15)*5 + (tmp11+tmp16));d+=dstStride;\
tmp17 = *s; s+=tmpStride;\
OP2(*d, (tmp14+tmp15)*20 - (tmp13+tmp16)*5 + (tmp12+tmp17));d+=dstStride;\
tmp18 = *s; s+=tmpStride;\
OP2(*d, (tmp15+tmp16)*20 - (tmp14+tmp17)*5 + (tmp13+tmp18));d+=dstStride;\
} \
} \
dst++;\
tmp++;\
}while(--i);\
}\
\
static void OPNAME ## h264_qpel4_h_lowpass(uint8_t *dst, uint8_t *src, int dstStride, int srcStride){\
OPNAME ## h264_qpel_h_lowpass(dst,src,dstStride,srcStride,4,4); \
}\
static void OPNAME ## h264_qpel8_h_lowpass(uint8_t *dst, uint8_t *src, int dstStride, int srcStride){\
OPNAME ## h264_qpel_h_lowpass(dst,src,dstStride,srcStride,8,8); \
}\
static void OPNAME ## h264_qpel16_h_lowpass(uint8_t *dst, uint8_t *src, int dstStride, int srcStride){\
OPNAME ## h264_qpel_h_lowpass(dst,src,dstStride,srcStride,16,16); \
}\
\
static void OPNAME ## h264_qpel4_v_lowpass(uint8_t *dst, uint8_t *src, int dstStride, int srcStride){\
OPNAME ## h264_qpel_v_lowpass(dst,src,dstStride,srcStride,4,4); \
}\
static void OPNAME ## h264_qpel8_v_lowpass(uint8_t *dst, uint8_t *src, int dstStride, int srcStride){\
OPNAME ## h264_qpel_v_lowpass(dst,src,dstStride,srcStride,8,8); \
}\
static void OPNAME ## h264_qpel16_v_lowpass(uint8_t *dst, uint8_t *src, int dstStride, int srcStride){\
OPNAME ## h264_qpel_v_lowpass(dst,src,dstStride,srcStride,16,16); \
}\
static void OPNAME ## h264_qpel4_hv_lowpass(uint8_t *dst, int16_t *tmp, uint8_t *src, int dstStride, int tmpStride, int srcStride){\
OPNAME ## h264_qpel_hv_lowpass(dst,tmp,src,dstStride,tmpStride,srcStride,4,4); \
}\
static void OPNAME ## h264_qpel8_hv_lowpass(uint8_t *dst, int16_t *tmp, uint8_t *src, int dstStride, int tmpStride, int srcStride){\
OPNAME ## h264_qpel_hv_lowpass(dst,tmp,src,dstStride,tmpStride,srcStride,8,8); \
}\
static void OPNAME ## h264_qpel16_hv_lowpass(uint8_t *dst, int16_t *tmp, uint8_t *src, int dstStride, int tmpStride, int srcStride){\
OPNAME ## h264_qpel_hv_lowpass(dst,tmp,src,dstStride,tmpStride,srcStride,16,16); \
}\
#define H264_MC(OPNAME, SIZE) \
static void OPNAME ## h264_qpel ## SIZE ## _mc00_sh4 (uint8_t *dst, uint8_t *src, int stride){\
OPNAME ## pixels ## SIZE ## _c(dst, src, stride, SIZE);\
}\
\
static void OPNAME ## h264_qpel ## SIZE ## _mc10_sh4(uint8_t *dst, uint8_t *src, int stride){\
uint8_t half[SIZE*SIZE];\
put_h264_qpel ## SIZE ## _h_lowpass(half, src, SIZE, stride);\
OPNAME ## pixels ## SIZE ## _l2_aligned2(dst, src, half, stride, stride, SIZE, SIZE);\
}\
\
static void OPNAME ## h264_qpel ## SIZE ## _mc20_sh4(uint8_t *dst, uint8_t *src, int stride){\
OPNAME ## h264_qpel ## SIZE ## _h_lowpass(dst, src, stride, stride);\
}\
\
static void OPNAME ## h264_qpel ## SIZE ## _mc30_sh4(uint8_t *dst, uint8_t *src, int stride){\
uint8_t half[SIZE*SIZE];\
put_h264_qpel ## SIZE ## _h_lowpass(half, src, SIZE, stride);\
OPNAME ## pixels ## SIZE ## _l2_aligned2(dst, src+1, half, stride, stride, SIZE, SIZE);\
}\
\
static void OPNAME ## h264_qpel ## SIZE ## _mc01_sh4(uint8_t *dst, uint8_t *src, int stride){\
uint8_t full[SIZE*(SIZE+5)];\
uint8_t * const full_mid= full + SIZE*2;\
uint8_t half[SIZE*SIZE];\
copy_block ## SIZE (full, src - stride*2, SIZE, stride, SIZE + 5);\
put_h264_qpel ## SIZE ## _v_lowpass(half, full_mid, SIZE, SIZE);\
OPNAME ## pixels ## SIZE ## _l2_aligned(dst, full_mid, half, stride, SIZE, SIZE, SIZE);\
}\
\
static void OPNAME ## h264_qpel ## SIZE ## _mc02_sh4(uint8_t *dst, uint8_t *src, int stride){\
uint8_t full[SIZE*(SIZE+5)];\
uint8_t * const full_mid= full + SIZE*2;\
copy_block ## SIZE (full, src - stride*2, SIZE, stride, SIZE + 5);\
OPNAME ## h264_qpel ## SIZE ## _v_lowpass(dst, full_mid, stride, SIZE);\
}\
\
static void OPNAME ## h264_qpel ## SIZE ## _mc03_sh4(uint8_t *dst, uint8_t *src, int stride){\
uint8_t full[SIZE*(SIZE+5)];\
uint8_t * const full_mid= full + SIZE*2;\
uint8_t half[SIZE*SIZE];\
copy_block ## SIZE (full, src - stride*2, SIZE, stride, SIZE + 5);\
put_h264_qpel ## SIZE ## _v_lowpass(half, full_mid, SIZE, SIZE);\
OPNAME ## pixels ## SIZE ## _l2_aligned(dst, full_mid+SIZE, half, stride, SIZE, SIZE, SIZE);\
}\
\
static void OPNAME ## h264_qpel ## SIZE ## _mc11_sh4(uint8_t *dst, uint8_t *src, int stride){\
uint8_t full[SIZE*(SIZE+5)];\
uint8_t * const full_mid= full + SIZE*2;\
uint8_t halfH[SIZE*SIZE];\
uint8_t halfV[SIZE*SIZE];\
put_h264_qpel ## SIZE ## _h_lowpass(halfH, src, SIZE, stride);\
copy_block ## SIZE (full, src - stride*2, SIZE, stride, SIZE + 5);\
put_h264_qpel ## SIZE ## _v_lowpass(halfV, full_mid, SIZE, SIZE);\
OPNAME ## pixels ## SIZE ## _l2_aligned(dst, halfH, halfV, stride, SIZE, SIZE, SIZE);\
}\
\
static void OPNAME ## h264_qpel ## SIZE ## _mc31_sh4(uint8_t *dst, uint8_t *src, int stride){\
uint8_t full[SIZE*(SIZE+5)];\
uint8_t * const full_mid= full + SIZE*2;\
uint8_t halfH[SIZE*SIZE];\
uint8_t halfV[SIZE*SIZE];\
put_h264_qpel ## SIZE ## _h_lowpass(halfH, src, SIZE, stride);\
copy_block ## SIZE (full, src - stride*2 + 1, SIZE, stride, SIZE + 5);\
put_h264_qpel ## SIZE ## _v_lowpass(halfV, full_mid, SIZE, SIZE);\
OPNAME ## pixels ## SIZE ## _l2_aligned(dst, halfH, halfV, stride, SIZE, SIZE, SIZE);\
}\
\
static void OPNAME ## h264_qpel ## SIZE ## _mc13_sh4(uint8_t *dst, uint8_t *src, int stride){\
uint8_t full[SIZE*(SIZE+5)];\
uint8_t * const full_mid= full + SIZE*2;\
uint8_t halfH[SIZE*SIZE];\
uint8_t halfV[SIZE*SIZE];\
put_h264_qpel ## SIZE ## _h_lowpass(halfH, src + stride, SIZE, stride);\
copy_block ## SIZE (full, src - stride*2, SIZE, stride, SIZE + 5);\
put_h264_qpel ## SIZE ## _v_lowpass(halfV, full_mid, SIZE, SIZE);\
OPNAME ## pixels ## SIZE ## _l2_aligned(dst, halfH, halfV, stride, SIZE, SIZE, SIZE);\
}\
\
static void OPNAME ## h264_qpel ## SIZE ## _mc33_sh4(uint8_t *dst, uint8_t *src, int stride){\
uint8_t full[SIZE*(SIZE+5)];\
uint8_t * const full_mid= full + SIZE*2;\
uint8_t halfH[SIZE*SIZE];\
uint8_t halfV[SIZE*SIZE];\
put_h264_qpel ## SIZE ## _h_lowpass(halfH, src + stride, SIZE, stride);\
copy_block ## SIZE (full, src - stride*2 + 1, SIZE, stride, SIZE + 5);\
put_h264_qpel ## SIZE ## _v_lowpass(halfV, full_mid, SIZE, SIZE);\
OPNAME ## pixels ## SIZE ## _l2_aligned(dst, halfH, halfV, stride, SIZE, SIZE, SIZE);\
}\
\
static void OPNAME ## h264_qpel ## SIZE ## _mc22_sh4(uint8_t *dst, uint8_t *src, int stride){\
int16_t tmp[SIZE*(SIZE+5)];\
OPNAME ## h264_qpel ## SIZE ## _hv_lowpass(dst, tmp, src, stride, SIZE, stride);\
}\
\
static void OPNAME ## h264_qpel ## SIZE ## _mc21_sh4(uint8_t *dst, uint8_t *src, int stride){\
int16_t tmp[SIZE*(SIZE+5)];\
uint8_t halfH[SIZE*SIZE];\
uint8_t halfHV[SIZE*SIZE];\
put_h264_qpel ## SIZE ## _h_lowpass(halfH, src, SIZE, stride);\
put_h264_qpel ## SIZE ## _hv_lowpass(halfHV, tmp, src, SIZE, SIZE, stride);\
OPNAME ## pixels ## SIZE ## _l2_aligned(dst, halfH, halfHV, stride, SIZE, SIZE, SIZE);\
}\
\
static void OPNAME ## h264_qpel ## SIZE ## _mc23_sh4(uint8_t *dst, uint8_t *src, int stride){\
int16_t tmp[SIZE*(SIZE+5)];\
uint8_t halfH[SIZE*SIZE];\
uint8_t halfHV[SIZE*SIZE];\
put_h264_qpel ## SIZE ## _h_lowpass(halfH, src + stride, SIZE, stride);\
put_h264_qpel ## SIZE ## _hv_lowpass(halfHV, tmp, src, SIZE, SIZE, stride);\
OPNAME ## pixels ## SIZE ## _l2_aligned(dst, halfH, halfHV, stride, SIZE, SIZE, SIZE);\
}\
\
static void OPNAME ## h264_qpel ## SIZE ## _mc12_sh4(uint8_t *dst, uint8_t *src, int stride){\
uint8_t full[SIZE*(SIZE+5)];\
uint8_t * const full_mid= full + SIZE*2;\
int16_t tmp[SIZE*(SIZE+5)];\
uint8_t halfV[SIZE*SIZE];\
uint8_t halfHV[SIZE*SIZE];\
copy_block ## SIZE (full, src - stride*2, SIZE, stride, SIZE + 5);\
put_h264_qpel ## SIZE ## _v_lowpass(halfV, full_mid, SIZE, SIZE);\
put_h264_qpel ## SIZE ## _hv_lowpass(halfHV, tmp, src, SIZE, SIZE, stride);\
OPNAME ## pixels ## SIZE ## _l2_aligned(dst, halfV, halfHV, stride, SIZE, SIZE, SIZE);\
}\
\
static void OPNAME ## h264_qpel ## SIZE ## _mc32_sh4(uint8_t *dst, uint8_t *src, int stride){\
uint8_t full[SIZE*(SIZE+5)];\
uint8_t * const full_mid= full + SIZE*2;\
int16_t tmp[SIZE*(SIZE+5)];\
uint8_t halfV[SIZE*SIZE];\
uint8_t halfHV[SIZE*SIZE];\
copy_block ## SIZE (full, src - stride*2 + 1, SIZE, stride, SIZE + 5);\
put_h264_qpel ## SIZE ## _v_lowpass(halfV, full_mid, SIZE, SIZE);\
put_h264_qpel ## SIZE ## _hv_lowpass(halfHV, tmp, src, SIZE, SIZE, stride);\
OPNAME ## pixels ## SIZE ## _l2_aligned(dst, halfV, halfHV, stride, SIZE, SIZE, SIZE);\
}\
#define op_avg(a, b) a = (((a)+cm[((b) + 16)>>5]+1)>>1)
//#define op_avg2(a, b) a = (((a)*w1+cm[((b) + 16)>>5]*w2 + o + 64)>>7)
#define op_put(a, b) a = cm[((b) + 16)>>5]
#define op2_avg(a, b) a = (((a)+cm[((b) + 512)>>10]+1)>>1)
#define op2_put(a, b) a = cm[((b) + 512)>>10]
H264_LOWPASS(put_ , op_put, op2_put)
H264_LOWPASS(avg_ , op_avg, op2_avg)
H264_MC(put_, 4)
H264_MC(put_, 8)
H264_MC(put_, 16)
H264_MC(avg_, 4)
H264_MC(avg_, 8)
H264_MC(avg_, 16)
#undef op_avg
#undef op_put
#undef op2_avg
#undef op2_put
#endif
static void wmv2_mspel8_h_lowpass(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int h){
uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
do{
int src_1,src0,src1,src2,src3,src4,src5,src6,src7,src8,src9;
uint8_t *s = src;
src_1 = s[-1];
src0 = *s++;
src1 = *s++;
src2 = *s++;
dst[0]= cm[(9*(src0 + src1) - (src_1 + src2) + 8)>>4];
src3 = *s++;
dst[1]= cm[(9*(src1 + src2) - (src0 + src3) + 8)>>4];
src4 = *s++;
dst[2]= cm[(9*(src2 + src3) - (src1 + src4) + 8)>>4];
src5 = *s++;
dst[3]= cm[(9*(src3 + src4) - (src2 + src5) + 8)>>4];
src6 = *s++;
dst[4]= cm[(9*(src4 + src5) - (src3 + src6) + 8)>>4];
src7 = *s++;
dst[5]= cm[(9*(src5 + src6) - (src4 + src7) + 8)>>4];
src8 = *s++;
dst[6]= cm[(9*(src6 + src7) - (src5 + src8) + 8)>>4];
src9 = *s++;
dst[7]= cm[(9*(src7 + src8) - (src6 + src9) + 8)>>4];
dst+=dstStride;
src+=srcStride;
}while(--h);
}
static void wmv2_mspel8_v_lowpass(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int w){
uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
do{
int src_1,src0,src1,src2,src3,src4,src5,src6,src7,src8,src9;
uint8_t *s = src,*d = dst;
src_1 = *(s-srcStride);
src0 = *s; s+=srcStride;
src1 = *s; s+=srcStride;
src2 = *s; s+=srcStride;
*d= cm[(9*(src0 + src1) - (src_1 + src2) + 8)>>4]; d+=dstStride;
src3 = *s; s+=srcStride;
*d= cm[(9*(src1 + src2) - (src0 + src3) + 8)>>4]; d+=dstStride;
src4 = *s; s+=srcStride;
*d= cm[(9*(src2 + src3) - (src1 + src4) + 8)>>4]; d+=dstStride;
src5 = *s; s+=srcStride;
*d= cm[(9*(src3 + src4) - (src2 + src5) + 8)>>4]; d+=dstStride;
src6 = *s; s+=srcStride;
*d= cm[(9*(src4 + src5) - (src3 + src6) + 8)>>4]; d+=dstStride;
src7 = *s; s+=srcStride;
*d= cm[(9*(src5 + src6) - (src4 + src7) + 8)>>4]; d+=dstStride;
src8 = *s; s+=srcStride;
*d= cm[(9*(src6 + src7) - (src5 + src8) + 8)>>4]; d+=dstStride;
src9 = *s;
*d= cm[(9*(src7 + src8) - (src6 + src9) + 8)>>4]; d+=dstStride;
src++;
dst++;
}while(--w);
}
static void put_mspel8_mc00_sh4 (uint8_t *dst, uint8_t *src, int stride){
put_pixels8_c(dst, src, stride, 8);
}
static void put_mspel8_mc10_sh4(uint8_t *dst, uint8_t *src, int stride){
uint8_t half[64];
wmv2_mspel8_h_lowpass(half, src, 8, stride, 8);
put_pixels8_l2_aligned2(dst, src, half, stride, stride, 8, 8);
}
static void put_mspel8_mc20_sh4(uint8_t *dst, uint8_t *src, int stride){
wmv2_mspel8_h_lowpass(dst, src, stride, stride, 8);
}
static void put_mspel8_mc30_sh4(uint8_t *dst, uint8_t *src, int stride){
uint8_t half[64];
wmv2_mspel8_h_lowpass(half, src, 8, stride, 8);
put_pixels8_l2_aligned2(dst, src+1, half, stride, stride, 8, 8);
}
static void put_mspel8_mc02_sh4(uint8_t *dst, uint8_t *src, int stride){
wmv2_mspel8_v_lowpass(dst, src, stride, stride, 8);
}
static void put_mspel8_mc12_sh4(uint8_t *dst, uint8_t *src, int stride){
uint8_t halfH[88];
uint8_t halfV[64];
uint8_t halfHV[64];
wmv2_mspel8_h_lowpass(halfH, src-stride, 8, stride, 11);
wmv2_mspel8_v_lowpass(halfV, src, 8, stride, 8);
wmv2_mspel8_v_lowpass(halfHV, halfH+8, 8, 8, 8);
put_pixels8_l2_aligned(dst, halfV, halfHV, stride, 8, 8, 8);
}
static void put_mspel8_mc32_sh4(uint8_t *dst, uint8_t *src, int stride){
uint8_t halfH[88];
uint8_t halfV[64];
uint8_t halfHV[64];
wmv2_mspel8_h_lowpass(halfH, src-stride, 8, stride, 11);
wmv2_mspel8_v_lowpass(halfV, src+1, 8, stride, 8);
wmv2_mspel8_v_lowpass(halfHV, halfH+8, 8, 8, 8);
put_pixels8_l2_aligned(dst, halfV, halfHV, stride, 8, 8, 8);
}
static void put_mspel8_mc22_sh4(uint8_t *dst, uint8_t *src, int stride){
uint8_t halfH[88];
wmv2_mspel8_h_lowpass(halfH, src-stride, 8, stride, 11);
wmv2_mspel8_v_lowpass(dst, halfH+8, stride, 8, 8);
}
| 123linslouis-android-video-cutter | jni/libavcodec/sh4/qpel.c | C | asf20 | 62,138 |
/*
* Copyright (c) 2008 Mans Rullgard <mans@mansr.com>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef AVCODEC_SH4_SH4_H
#define AVCODEC_SH4_SH4_H
#ifdef __SH4__
# define fp_single_enter(fpscr) \
do { \
__asm__ volatile ("sts fpscr, %0 \n\t" \
"and %1, %0 \n\t" \
"lds %0, fpscr \n\t" \
: "=&r"(fpscr) : "r"(~(1<<19))); \
} while (0)
# define fp_single_leave(fpscr) \
do { \
__asm__ volatile ("or %1, %0 \n\t" \
"lds %0, fpscr \n\t" \
: "+r"(fpscr) : "r"(1<<19)); \
} while (0)
#else
# define fp_single_enter(fpscr) ((void)fpscr)
# define fp_single_leave(fpscr)
#endif
#endif /* AVCODEC_SH4_SH4_H */
| 123linslouis-android-video-cutter | jni/libavcodec/sh4/sh4.h | C | asf20 | 1,734 |
/*
* Targa (.tga) image encoder
* Copyright (c) 2007 Bobby Bingham
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "libavutil/intreadwrite.h"
#include "avcodec.h"
#include "rle.h"
typedef struct TargaContext {
AVFrame picture;
} TargaContext;
/**
* RLE compress the image, with maximum size of out_size
* @param outbuf Output buffer
* @param out_size Maximum output size
* @param pic Image to compress
* @param bpp Bytes per pixel
* @param w Image width
* @param h Image height
* @return Size of output in bytes, or -1 if larger than out_size
*/
static int targa_encode_rle(uint8_t *outbuf, int out_size, AVFrame *pic,
int bpp, int w, int h)
{
int y,ret;
uint8_t *out;
out = outbuf;
for(y = 0; y < h; y ++) {
ret = ff_rle_encode(out, out_size, pic->data[0] + pic->linesize[0] * y, bpp, w, 0x7f, 0, -1, 0);
if(ret == -1){
return -1;
}
out+= ret;
out_size -= ret;
}
return out - outbuf;
}
static int targa_encode_normal(uint8_t *outbuf, AVFrame *pic, int bpp, int w, int h)
{
int i, n = bpp * w;
uint8_t *out = outbuf;
uint8_t *ptr = pic->data[0];
for(i=0; i < h; i++) {
memcpy(out, ptr, n);
out += n;
ptr += pic->linesize[0];
}
return out - outbuf;
}
static int targa_encode_frame(AVCodecContext *avctx,
unsigned char *outbuf,
int buf_size, void *data){
AVFrame *p = data;
int bpp, picsize, datasize = -1;
uint8_t *out;
if(avctx->width > 0xffff || avctx->height > 0xffff) {
av_log(avctx, AV_LOG_ERROR, "image dimensions too large\n");
return -1;
}
picsize = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
if(buf_size < picsize + 45) {
av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
return -1;
}
p->pict_type= FF_I_TYPE;
p->key_frame= 1;
/* zero out the header and only set applicable fields */
memset(outbuf, 0, 12);
AV_WL16(outbuf+12, avctx->width);
AV_WL16(outbuf+14, avctx->height);
outbuf[17] = 0x20; /* origin is top-left. no alpha */
/* TODO: support alpha channel */
switch(avctx->pix_fmt) {
case PIX_FMT_GRAY8:
outbuf[2] = 3; /* uncompressed grayscale image */
outbuf[16] = 8; /* bpp */
break;
case PIX_FMT_RGB555LE:
outbuf[2] = 2; /* uncompresses true-color image */
outbuf[16] = 16; /* bpp */
break;
case PIX_FMT_BGR24:
outbuf[2] = 2; /* uncompressed true-color image */
outbuf[16] = 24; /* bpp */
break;
default:
return -1;
}
bpp = outbuf[16] >> 3;
out = outbuf + 18; /* skip past the header we just output */
/* try RLE compression */
if (avctx->coder_type != FF_CODER_TYPE_RAW)
datasize = targa_encode_rle(out, picsize, p, bpp, avctx->width, avctx->height);
/* if that worked well, mark the picture as RLE compressed */
if(datasize >= 0)
outbuf[2] |= 8;
/* if RLE didn't make it smaller, go back to no compression */
else datasize = targa_encode_normal(out, p, bpp, avctx->width, avctx->height);
out += datasize;
/* The standard recommends including this section, even if we don't use
* any of the features it affords. TODO: take advantage of the pixel
* aspect ratio and encoder ID fields available? */
memcpy(out, "\0\0\0\0\0\0\0\0TRUEVISION-XFILE.", 26);
return out + 26 - outbuf;
}
static av_cold int targa_encode_init(AVCodecContext *avctx)
{
TargaContext *s = avctx->priv_data;
avcodec_get_frame_defaults(&s->picture);
s->picture.key_frame= 1;
avctx->coded_frame= &s->picture;
return 0;
}
AVCodec targa_encoder = {
.name = "targa",
.type = AVMEDIA_TYPE_VIDEO,
.id = CODEC_ID_TARGA,
.priv_data_size = sizeof(TargaContext),
.init = targa_encode_init,
.encode = targa_encode_frame,
.pix_fmts= (const enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_RGB555LE, PIX_FMT_GRAY8, PIX_FMT_NONE},
.long_name= NULL_IF_CONFIG_SMALL("Truevision Targa image"),
};
| 123linslouis-android-video-cutter | jni/libavcodec/targaenc.c | C | asf20 | 4,958 |
/*
* data for G.729 decoder
* Copyright (c) 2007 Vladimir Voroshilov
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef AVCODEC_G729DATA_H
#define AVCODEC_G729DATA_H
#include <stdint.h>
#define MA_NP 4 ///< Moving Average (MA) prediction order
#define VQ_1ST_BITS 7 ///< first stage vector of quantizer (size in bits)
#define VQ_2ND_BITS 5 ///< second stage vector of quantizer (size in bits)
#define GC_1ST_IDX_BITS_8K 3 ///< gain codebook (first stage) index, 8k mode (size in bits)
#define GC_2ND_IDX_BITS_8K 4 ///< gain codebook (second stage) index, 8k mode (size in bits)
#define GC_1ST_IDX_BITS_6K4 3 ///< gain codebook (first stage) index, 6.4k mode (size in bits)
#define GC_2ND_IDX_BITS_6K4 3 ///< gain codebook (second stage) index, 6.4k mode (size in bits)
/**
* first stage LSP codebook
* (10-dimensional, with 128 entries (3.24 of G.729)
*/
static const int16_t cb_lsp_1st[1<<VQ_1ST_BITS][10] = { /* (2.13) */
{ 1486, 2168, 3751, 9074, 12134, 13944, 17983, 19173, 21190, 21820},
{ 1730, 2640, 3450, 4870, 6126, 7876, 15644, 17817, 20294, 21902},
{ 1568, 2256, 3088, 4874, 11063, 13393, 18307, 19293, 21109, 21741},
{ 1733, 2512, 3357, 4708, 6977, 10296, 17024, 17956, 19145, 20350},
{ 1744, 2436, 3308, 8731, 10432, 12007, 15614, 16639, 21359, 21913},
{ 1786, 2369, 3372, 4521, 6795, 12963, 17674, 18988, 20855, 21640},
{ 1631, 2433, 3361, 6328, 10709, 12013, 13277, 13904, 19441, 21088},
{ 1489, 2364, 3291, 6250, 9227, 10403, 13843, 15278, 17721, 21451},
{ 1869, 2533, 3475, 4365, 9152, 14513, 15908, 17022, 20611, 21411},
{ 2070, 3025, 4333, 5854, 7805, 9231, 10597, 16047, 20109, 21834},
{ 1910, 2673, 3419, 4261, 11168, 15111, 16577, 17591, 19310, 20265},
{ 1141, 1815, 2624, 4623, 6495, 9588, 13968, 16428, 19351, 21286},
{ 2192, 3171, 4707, 5808, 10904, 12500, 14162, 15664, 21124, 21789},
{ 1286, 1907, 2548, 3453, 9574, 11964, 15978, 17344, 19691, 22495},
{ 1921, 2720, 4604, 6684, 11503, 12992, 14350, 15262, 16997, 20791},
{ 2052, 2759, 3897, 5246, 6638, 10267, 15834, 16814, 18149, 21675},
{ 1798, 2497, 5617, 11449, 13189, 14711, 17050, 18195, 20307, 21182},
{ 1009, 1647, 2889, 5709, 9541, 12354, 15231, 18494, 20966, 22033},
{ 3016, 3794, 5406, 7469, 12488, 13984, 15328, 16334, 19952, 20791},
{ 2203, 3040, 3796, 5442, 11987, 13512, 14931, 16370, 17856, 18803},
{ 2912, 4292, 7988, 9572, 11562, 13244, 14556, 16529, 20004, 21073},
{ 2861, 3607, 5923, 7034, 9234, 12054, 13729, 18056, 20262, 20974},
{ 3069, 4311, 5967, 7367, 11482, 12699, 14309, 16233, 18333, 19172},
{ 2434, 3661, 4866, 5798, 10383, 11722, 13049, 15668, 18862, 19831},
{ 2020, 2605, 3860, 9241, 13275, 14644, 16010, 17099, 19268, 20251},
{ 1877, 2809, 3590, 4707, 11056, 12441, 15622, 17168, 18761, 19907},
{ 2107, 2873, 3673, 5799, 13579, 14687, 15938, 17077, 18890, 19831},
{ 1612, 2284, 2944, 3572, 8219, 13959, 15924, 17239, 18592, 20117},
{ 2420, 3156, 6542, 10215, 12061, 13534, 15305, 16452, 18717, 19880},
{ 1667, 2612, 3534, 5237, 10513, 11696, 12940, 16798, 18058, 19378},
{ 2388, 3017, 4839, 9333, 11413, 12730, 15024, 16248, 17449, 18677},
{ 1875, 2786, 4231, 6320, 8694, 10149, 11785, 17013, 18608, 19960},
{ 679, 1411, 4654, 8006, 11446, 13249, 15763, 18127, 20361, 21567},
{ 1838, 2596, 3578, 4608, 5650, 11274, 14355, 15886, 20579, 21754},
{ 1303, 1955, 2395, 3322, 12023, 13764, 15883, 18077, 20180, 21232},
{ 1438, 2102, 2663, 3462, 8328, 10362, 13763, 17248, 19732, 22344},
{ 860, 1904, 6098, 7775, 9815, 12007, 14821, 16709, 19787, 21132},
{ 1673, 2723, 3704, 6125, 7668, 9447, 13683, 14443, 20538, 21731},
{ 1246, 1849, 2902, 4508, 7221, 12710, 14835, 16314, 19335, 22720},
{ 1525, 2260, 3862, 5659, 7342, 11748, 13370, 14442, 18044, 21334},
{ 1196, 1846, 3104, 7063, 10972, 12905, 14814, 17037, 19922, 22636},
{ 2147, 3106, 4475, 6511, 8227, 9765, 10984, 12161, 18971, 21300},
{ 1585, 2405, 2994, 4036, 11481, 13177, 14519, 15431, 19967, 21275},
{ 1778, 2688, 3614, 4680, 9465, 11064, 12473, 16320, 19742, 20800},
{ 1862, 2586, 3492, 6719, 11708, 13012, 14364, 16128, 19610, 20425},
{ 1395, 2156, 2669, 3386, 10607, 12125, 13614, 16705, 18976, 21367},
{ 1444, 2117, 3286, 6233, 9423, 12981, 14998, 15853, 17188, 21857},
{ 2004, 2895, 3783, 4897, 6168, 7297, 12609, 16445, 19297, 21465},
{ 1495, 2863, 6360, 8100, 11399, 14271, 15902, 17711, 20479, 22061},
{ 2484, 3114, 5718, 7097, 8400, 12616, 14073, 14847, 20535, 21396},
{ 2424, 3277, 5296, 6284, 11290, 12903, 16022, 17508, 19333, 20283},
{ 2565, 3778, 5360, 6989, 8782, 10428, 14390, 15742, 17770, 21734},
{ 2727, 3384, 6613, 9254, 10542, 12236, 14651, 15687, 20074, 21102},
{ 1916, 2953, 6274, 8088, 9710, 10925, 12392, 16434, 20010, 21183},
{ 3384, 4366, 5349, 7667, 11180, 12605, 13921, 15324, 19901, 20754},
{ 3075, 4283, 5951, 7619, 9604, 11010, 12384, 14006, 20658, 21497},
{ 1751, 2455, 5147, 9966, 11621, 13176, 14739, 16470, 20788, 21756},
{ 1442, 2188, 3330, 6813, 8929, 12135, 14476, 15306, 19635, 20544},
{ 2294, 2895, 4070, 8035, 12233, 13416, 14762, 17367, 18952, 19688},
{ 1937, 2659, 4602, 6697, 9071, 12863, 14197, 15230, 16047, 18877},
{ 2071, 2663, 4216, 9445, 10887, 12292, 13949, 14909, 19236, 20341},
{ 1740, 2491, 3488, 8138, 9656, 11153, 13206, 14688, 20896, 21907},
{ 2199, 2881, 4675, 8527, 10051, 11408, 14435, 15463, 17190, 20597},
{ 1943, 2988, 4177, 6039, 7478, 8536, 14181, 15551, 17622, 21579},
{ 1825, 3175, 7062, 9818, 12824, 15450, 18330, 19856, 21830, 22412},
{ 2464, 3046, 4822, 5977, 7696, 15398, 16730, 17646, 20588, 21320},
{ 2550, 3393, 5305, 6920, 10235, 14083, 18143, 19195, 20681, 21336},
{ 3003, 3799, 5321, 6437, 7919, 11643, 15810, 16846, 18119, 18980},
{ 3455, 4157, 6838, 8199, 9877, 12314, 15905, 16826, 19949, 20892},
{ 3052, 3769, 4891, 5810, 6977, 10126, 14788, 15990, 19773, 20904},
{ 3671, 4356, 5827, 6997, 8460, 12084, 14154, 14939, 19247, 20423},
{ 2716, 3684, 5246, 6686, 8463, 10001, 12394, 14131, 16150, 19776},
{ 1945, 2638, 4130, 7995, 14338, 15576, 17057, 18206, 20225, 20997},
{ 2304, 2928, 4122, 4824, 5640, 13139, 15825, 16938, 20108, 21054},
{ 1800, 2516, 3350, 5219, 13406, 15948, 17618, 18540, 20531, 21252},
{ 1436, 2224, 2753, 4546, 9657, 11245, 15177, 16317, 17489, 19135},
{ 2319, 2899, 4980, 6936, 8404, 13489, 15554, 16281, 20270, 20911},
{ 2187, 2919, 4610, 5875, 7390, 12556, 14033, 16794, 20998, 21769},
{ 2235, 2923, 5121, 6259, 8099, 13589, 15340, 16340, 17927, 20159},
{ 1765, 2638, 3751, 5730, 7883, 10108, 13633, 15419, 16808, 18574},
{ 3460, 5741, 9596, 11742, 14413, 16080, 18173, 19090, 20845, 21601},
{ 3735, 4426, 6199, 7363, 9250, 14489, 16035, 17026, 19873, 20876},
{ 3521, 4778, 6887, 8680, 12717, 14322, 15950, 18050, 20166, 21145},
{ 2141, 2968, 6865, 8051, 10010, 13159, 14813, 15861, 17528, 18655},
{ 4148, 6128, 9028, 10871, 12686, 14005, 15976, 17208, 19587, 20595},
{ 4403, 5367, 6634, 8371, 10163, 11599, 14963, 16331, 17982, 18768},
{ 4091, 5386, 6852, 8770, 11563, 13290, 15728, 16930, 19056, 20102},
{ 2746, 3625, 5299, 7504, 10262, 11432, 13172, 15490, 16875, 17514},
{ 2248, 3556, 8539, 10590, 12665, 14696, 16515, 17824, 20268, 21247},
{ 1279, 1960, 3920, 7793, 10153, 14753, 16646, 18139, 20679, 21466},
{ 2440, 3475, 6737, 8654, 12190, 14588, 17119, 17925, 19110, 19979},
{ 1879, 2514, 4497, 7572, 10017, 14948, 16141, 16897, 18397, 19376},
{ 2804, 3688, 7490, 10086, 11218, 12711, 16307, 17470, 20077, 21126},
{ 2023, 2682, 3873, 8268, 10255, 11645, 15187, 17102, 18965, 19788},
{ 2823, 3605, 5815, 8595, 10085, 11469, 16568, 17462, 18754, 19876},
{ 2851, 3681, 5280, 7648, 9173, 10338, 14961, 16148, 17559, 18474},
{ 1348, 2645, 5826, 8785, 10620, 12831, 16255, 18319, 21133, 22586},
{ 2141, 3036, 4293, 6082, 7593, 10629, 17158, 18033, 21466, 22084},
{ 1608, 2375, 3384, 6878, 9970, 11227, 16928, 17650, 20185, 21120},
{ 2774, 3616, 5014, 6557, 7788, 8959, 17068, 18302, 19537, 20542},
{ 1934, 4813, 6204, 7212, 8979, 11665, 15989, 17811, 20426, 21703},
{ 2288, 3507, 5037, 6841, 8278, 9638, 15066, 16481, 21653, 22214},
{ 2951, 3771, 4878, 7578, 9016, 10298, 14490, 15242, 20223, 20990},
{ 3256, 4791, 6601, 7521, 8644, 9707, 13398, 16078, 19102, 20249},
{ 1827, 2614, 3486, 6039, 12149, 13823, 16191, 17282, 21423, 22041},
{ 1000, 1704, 3002, 6335, 8471, 10500, 14878, 16979, 20026, 22427},
{ 1646, 2286, 3109, 7245, 11493, 12791, 16824, 17667, 18981, 20222},
{ 1708, 2501, 3315, 6737, 8729, 9924, 16089, 17097, 18374, 19917},
{ 2623, 3510, 4478, 5645, 9862, 11115, 15219, 18067, 19583, 20382},
{ 2518, 3434, 4728, 6388, 8082, 9285, 13162, 18383, 19819, 20552},
{ 1726, 2383, 4090, 6303, 7805, 12845, 14612, 17608, 19269, 20181},
{ 2860, 3735, 4838, 6044, 7254, 8402, 14031, 16381, 18037, 19410},
{ 4247, 5993, 7952, 9792, 12342, 14653, 17527, 18774, 20831, 21699},
{ 3502, 4051, 5680, 6805, 8146, 11945, 16649, 17444, 20390, 21564},
{ 3151, 4893, 5899, 7198, 11418, 13073, 15124, 17673, 20520, 21861},
{ 3960, 4848, 5926, 7259, 8811, 10529, 15661, 16560, 18196, 20183},
{ 4499, 6604, 8036, 9251, 10804, 12627, 15880, 17512, 20020, 21046},
{ 4251, 5541, 6654, 8318, 9900, 11686, 15100, 17093, 20572, 21687},
{ 3769, 5327, 7865, 9360, 10684, 11818, 13660, 15366, 18733, 19882},
{ 3083, 3969, 6248, 8121, 9798, 10994, 12393, 13686, 17888, 19105},
{ 2731, 4670, 7063, 9201, 11346, 13735, 16875, 18797, 20787, 22360},
{ 1187, 2227, 4737, 7214, 9622, 12633, 15404, 17968, 20262, 23533},
{ 1911, 2477, 3915, 10098, 11616, 12955, 16223, 17138, 19270, 20729},
{ 1764, 2519, 3887, 6944, 9150, 12590, 16258, 16984, 17924, 18435},
{ 1400, 3674, 7131, 8718, 10688, 12508, 15708, 17711, 19720, 21068},
{ 2322, 3073, 4287, 8108, 9407, 10628, 15862, 16693, 19714, 21474},
{ 2630, 3339, 4758, 8360, 10274, 11333, 12880, 17374, 19221, 19936},
{ 1721, 2577, 5553, 7195, 8651, 10686, 15069, 16953, 18703, 19929}
};
/**
* second stage LSP codebook, high and low parts
(both 5-dimensional, with 32 entries (3.2.4 of G.729)
*/
static const int16_t cb_lsp_2nd[1<<VQ_2ND_BITS][10] = { /* (2.13) */
{ -435, -815, -742, 1033, -518, 582, -1201, 829, 86, 385},
{ -833, -891, 463, -8, -1251, 1450, 72, -231, 864, 661},
{-1021, 231, -306, 321, -220, -163, -526, -754, -1633, 267},
{ 57, -198, -339, -33, -1468, 573, 796, -169, -631, 816},
{ 171, -350, 294, 1660, 453, 519, 291, 159, -640, -1296},
{ -701, -842, -58, 950, 892, 1549, 715, 527, -714, -193},
{ 584, 31, -289, 356, -333, -457, 612, -283, -1381, -741},
{ -109, -808, 231, 77, -87, -344, 1341, 1087, -654, -569},
{ -859, 1236, 550, 854, 714, -543, -1752, -195, -98, -276},
{ -877, -954, -1248, -299, 212, -235, -728, 949, 1517, 895},
{ -77, 344, -620, 763, 413, 502, -362, -960, -483, 1386},
{ -314, -307, -256, -1260, -429, 450, -466, -108, 1010, 2223},
{ 711, 693, 521, 650, 1305, -28, -378, 744, -1005, 240},
{ -112, -271, -500, 946, 1733, 271, -15, 909, -259, 1688},
{ 575, -10, -468, -199, 1101, -1011, 581, -53, -747, 878},
{ 145, -285, -1280, -398, 36, -498, -1377, 18, -444, 1483},
{-1133, -835, 1350, 1284, -95, 1015, -222, 443, 372, -354},
{-1459, -1237, 416, -213, 466, 669, 659, 1640, 932, 534},
{ -15, 66, 468, 1019, -748, 1385, -182, -907, -721, -262},
{ -338, 148, 1445, 75, -760, 569, 1247, 337, 416, -121},
{ 389, 239, 1568, 981, 113, 369, -1003, -507, -587, -904},
{ -312, -98, 949, 31, 1104, 72, -141, 1465, 63, -785},
{ 1127, 584, 835, 277, -1159, 208, 301, -882, 117, -404},
{ 539, -114, 856, -493, 223, -912, 623, -76, 276, -440},
{ 2197, 2337, 1268, 670, 304, -267, -525, 140, 882, -139},
{-1596, 550, 801, -456, -56, -697, 865, 1060, 413, 446},
{ 1154, 593, -77, 1237, -31, 581, -1037, -895, 669, 297},
{ 397, 558, 203, -797, -919, 3, 692, -292, 1050, 782},
{ 334, 1475, 632, -80, 48, -1061, -484, 362, -597, -852},
{ -545, -330, -429, -680, 1133, -1182, -744, 1340, 262, 63},
{ 1320, 827, -398, -576, 341, -774, -483, -1247, -70, 98},
{ -163, 674, -11, -886, 531, -1125, -265, -242, 724, 934}
};
/**
* gain codebook (first stage), 8k mode (3.9.2 of G.729)
*/
static const int16_t cb_gain_1st_8k[1<<GC_1ST_IDX_BITS_8K][2] = { /*(0.14) (2.13) */
{ 3242 , 9949 },
{ 1551 , 2425 },
{ 2678 , 27162 },
{ 1921 , 9291 },
{ 1831 , 5022 },
{ 1 , 1516 },
{ 356 , 14756 },
{ 57 , 5404 },
};
/**
* gain codebook (second stage), 8k mode (3.9.2 of G.729)
*/
static const int16_t cb_gain_2nd_8k[1<<GC_2ND_IDX_BITS_8K][2] = { /*(1.14) (1.13) */
{ 5142 , 592 },
{ 17299 , 1861 },
{ 6160 , 2395 },
{ 16112 , 3392 },
{ 826 , 2005 },
{ 18973 , 5935 },
{ 1994 , 0 },
{ 15434 , 237 },
{ 10573 , 2966 },
{ 15132 , 4914 },
{ 11569 , 1196 },
{ 14194 , 1630 },
{ 8091 , 4861 },
{ 15161 , 14276 },
{ 9120 , 525 },
{ 13260 , 3256 },
};
/**
* 4th order Moving Average (MA) Predictor codebook (3.2.4 of G.729)
*/
static const int16_t cb_ma_predictor[2][MA_NP][10] = { /* (0.15) */
{
{ 8421, 9109, 9175, 8965, 9034, 9057, 8765, 8775, 9106, 8673},
{ 7018, 7189, 7638, 7307, 7444, 7379, 7038, 6956, 6930, 6868},
{ 5472, 4990, 5134, 5177, 5246, 5141, 5206, 5095, 4830, 5147},
{ 4056, 3031, 2614, 3024, 2916, 2713, 3309, 3237, 2857, 3473}
},
{
{ 7733, 7880, 8188, 8175, 8247, 8490, 8637, 8601, 8359, 7569},
{ 4210, 3031, 2552, 3473, 3876, 3853, 4184, 4154, 3909, 3968},
{ 3214, 1930, 1313, 2143, 2493, 2385, 2755, 2706, 2542, 2919},
{ 3024, 1592, 940, 1631, 1723, 1579, 2034, 2084, 1913, 2601}
}
};
static const int16_t cb_ma_predictor_sum[2][10] = { /* (0.15) */
{ 7798, 8447, 8205, 8293, 8126, 8477, 8447, 8703, 9043, 8604},
{14585, 18333, 19772, 17344, 16426, 16459, 15155, 15220, 16043, 15708}
};
/**
* initial LSP coefficients belongs to virtual frame preceding the
* first frame of the stream
*/
static const int16_t lsp_init[10]= { /* (0.15) */
30000, 26000, 21000, 15000, 8000, 0, -8000,-15000,-21000,-26000
};
#endif /* AVCODEC_G729DATA_H */
| 123linslouis-android-video-cutter | jni/libavcodec/g729data.h | C | asf20 | 15,781 |
/*
* Copyright (C) 2003 Mike Melanson
* Copyright (C) 2003 Dr. Tim Ferguson
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef AVCODEC_ROQVIDEO_H
#define AVCODEC_ROQVIDEO_H
#include "libavutil/lfg.h"
#include "avcodec.h"
#include "dsputil.h"
typedef struct {
unsigned char y[4];
unsigned char u, v;
} roq_cell;
typedef struct {
int idx[4];
} roq_qcell;
typedef struct {
int d[2];
} motion_vect;
struct RoqTempData;
typedef struct RoqContext {
AVCodecContext *avctx;
DSPContext dsp;
AVFrame frames[2];
AVFrame *last_frame;
AVFrame *current_frame;
int first_frame;
roq_cell cb2x2[256];
roq_qcell cb4x4[256];
const unsigned char *buf;
int size;
int width, height;
/* Encoder only data */
AVLFG randctx;
uint64_t lambda;
motion_vect *this_motion4;
motion_vect *last_motion4;
motion_vect *this_motion8;
motion_vect *last_motion8;
unsigned int framesSinceKeyframe;
AVFrame *frame_to_enc;
uint8_t *out_buf;
struct RoqTempData *tmpData;
} RoqContext;
#define RoQ_INFO 0x1001
#define RoQ_QUAD_CODEBOOK 0x1002
#define RoQ_QUAD_VQ 0x1011
#define RoQ_SOUND_MONO 0x1020
#define RoQ_SOUND_STEREO 0x1021
#define RoQ_ID_MOT 0x00
#define RoQ_ID_FCC 0x01
#define RoQ_ID_SLD 0x02
#define RoQ_ID_CCC 0x03
void ff_apply_vector_2x2(RoqContext *ri, int x, int y, roq_cell *cell);
void ff_apply_vector_4x4(RoqContext *ri, int x, int y, roq_cell *cell);
void ff_apply_motion_4x4(RoqContext *ri, int x, int y, int deltax, int deltay);
void ff_apply_motion_8x8(RoqContext *ri, int x, int y, int deltax, int deltay);
#endif /* AVCODEC_ROQVIDEO_H */
| 123linslouis-android-video-cutter | jni/libavcodec/roqvideo.h | C | asf20 | 2,459 |
/*
* audio encoder psychoacoustic model
* Copyright (C) 2008 Konstantin Shishkov
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "avcodec.h"
#include "psymodel.h"
#include "iirfilter.h"
extern const FFPsyModel ff_aac_psy_model;
av_cold int ff_psy_init(FFPsyContext *ctx, AVCodecContext *avctx,
int num_lens,
const uint8_t **bands, const int* num_bands)
{
ctx->avctx = avctx;
ctx->psy_bands = av_mallocz(sizeof(FFPsyBand) * PSY_MAX_BANDS * avctx->channels);
ctx->bands = av_malloc (sizeof(ctx->bands[0]) * num_lens);
ctx->num_bands = av_malloc (sizeof(ctx->num_bands[0]) * num_lens);
memcpy(ctx->bands, bands, sizeof(ctx->bands[0]) * num_lens);
memcpy(ctx->num_bands, num_bands, sizeof(ctx->num_bands[0]) * num_lens);
switch (ctx->avctx->codec_id) {
case CODEC_ID_AAC:
ctx->model = &ff_aac_psy_model;
break;
}
if (ctx->model->init)
return ctx->model->init(ctx);
return 0;
}
FFPsyWindowInfo ff_psy_suggest_window(FFPsyContext *ctx,
const int16_t *audio, const int16_t *la,
int channel, int prev_type)
{
return ctx->model->window(ctx, audio, la, channel, prev_type);
}
void ff_psy_set_band_info(FFPsyContext *ctx, int channel,
const float *coeffs, FFPsyWindowInfo *wi)
{
ctx->model->analyze(ctx, channel, coeffs, wi);
}
av_cold void ff_psy_end(FFPsyContext *ctx)
{
if (ctx->model->end)
ctx->model->end(ctx);
av_freep(&ctx->bands);
av_freep(&ctx->num_bands);
av_freep(&ctx->psy_bands);
}
typedef struct FFPsyPreprocessContext{
AVCodecContext *avctx;
float stereo_att;
struct FFIIRFilterCoeffs *fcoeffs;
struct FFIIRFilterState **fstate;
}FFPsyPreprocessContext;
#define FILT_ORDER 4
av_cold struct FFPsyPreprocessContext* ff_psy_preprocess_init(AVCodecContext *avctx)
{
FFPsyPreprocessContext *ctx;
int i;
float cutoff_coeff = 0;
ctx = av_mallocz(sizeof(FFPsyPreprocessContext));
ctx->avctx = avctx;
if (avctx->cutoff > 0)
cutoff_coeff = 2.0 * avctx->cutoff / avctx->sample_rate;
if (cutoff_coeff)
ctx->fcoeffs = ff_iir_filter_init_coeffs(FF_FILTER_TYPE_BUTTERWORTH, FF_FILTER_MODE_LOWPASS,
FILT_ORDER, cutoff_coeff, 0.0, 0.0);
if (ctx->fcoeffs) {
ctx->fstate = av_mallocz(sizeof(ctx->fstate[0]) * avctx->channels);
for (i = 0; i < avctx->channels; i++)
ctx->fstate[i] = ff_iir_filter_init_state(FILT_ORDER);
}
return ctx;
}
void ff_psy_preprocess(struct FFPsyPreprocessContext *ctx,
const int16_t *audio, int16_t *dest,
int tag, int channels)
{
int ch, i;
if (ctx->fstate) {
for (ch = 0; ch < channels; ch++)
ff_iir_filter(ctx->fcoeffs, ctx->fstate[tag+ch], ctx->avctx->frame_size,
audio + ch, ctx->avctx->channels,
dest + ch, ctx->avctx->channels);
} else {
for (ch = 0; ch < channels; ch++)
for (i = 0; i < ctx->avctx->frame_size; i++)
dest[i*ctx->avctx->channels + ch] = audio[i*ctx->avctx->channels + ch];
}
}
av_cold void ff_psy_preprocess_end(struct FFPsyPreprocessContext *ctx)
{
int i;
ff_iir_filter_free_coeffs(ctx->fcoeffs);
if (ctx->fstate)
for (i = 0; i < ctx->avctx->channels; i++)
ff_iir_filter_free_state(ctx->fstate[i]);
av_freep(&ctx->fstate);
}
| 123linslouis-android-video-cutter | jni/libavcodec/psymodel.c | C | asf20 | 4,327 |
/*
* copyright (c) 2000,2001 Fabrice Bellard
* H263+ support
* copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* mpeg4 tables.
*/
#ifndef AVCODEC_MPEG4DATA_H
#define AVCODEC_MPEG4DATA_H
#include <stdint.h>
#include "mpegvideo.h"
/* dc encoding for mpeg4 */
const uint8_t ff_mpeg4_DCtab_lum[13][2] =
{
{3,3}, {3,2}, {2,2}, {2,3}, {1,3}, {1,4}, {1,5}, {1,6}, {1,7},
{1,8}, {1,9}, {1,10}, {1,11},
};
const uint8_t ff_mpeg4_DCtab_chrom[13][2] =
{
{3,2}, {2,2}, {1,2}, {1,3}, {1,4}, {1,5}, {1,6}, {1,7}, {1,8},
{1,9}, {1,10}, {1,11}, {1,12},
};
const uint16_t ff_mpeg4_intra_vlc[103][2] = {
{ 0x2, 2 },
{ 0x6, 3 },{ 0xf, 4 },{ 0xd, 5 },{ 0xc, 5 },
{ 0x15, 6 },{ 0x13, 6 },{ 0x12, 6 },{ 0x17, 7 },
{ 0x1f, 8 },{ 0x1e, 8 },{ 0x1d, 8 },{ 0x25, 9 },
{ 0x24, 9 },{ 0x23, 9 },{ 0x21, 9 },{ 0x21, 10 },
{ 0x20, 10 },{ 0xf, 10 },{ 0xe, 10 },{ 0x7, 11 },
{ 0x6, 11 },{ 0x20, 11 },{ 0x21, 11 },{ 0x50, 12 },
{ 0x51, 12 },{ 0x52, 12 },{ 0xe, 4 },{ 0x14, 6 },
{ 0x16, 7 },{ 0x1c, 8 },{ 0x20, 9 },{ 0x1f, 9 },
{ 0xd, 10 },{ 0x22, 11 },{ 0x53, 12 },{ 0x55, 12 },
{ 0xb, 5 },{ 0x15, 7 },{ 0x1e, 9 },{ 0xc, 10 },
{ 0x56, 12 },{ 0x11, 6 },{ 0x1b, 8 },{ 0x1d, 9 },
{ 0xb, 10 },{ 0x10, 6 },{ 0x22, 9 },{ 0xa, 10 },
{ 0xd, 6 },{ 0x1c, 9 },{ 0x8, 10 },{ 0x12, 7 },
{ 0x1b, 9 },{ 0x54, 12 },{ 0x14, 7 },{ 0x1a, 9 },
{ 0x57, 12 },{ 0x19, 8 },{ 0x9, 10 },{ 0x18, 8 },
{ 0x23, 11 },{ 0x17, 8 },{ 0x19, 9 },{ 0x18, 9 },
{ 0x7, 10 },{ 0x58, 12 },{ 0x7, 4 },{ 0xc, 6 },
{ 0x16, 8 },{ 0x17, 9 },{ 0x6, 10 },{ 0x5, 11 },
{ 0x4, 11 },{ 0x59, 12 },{ 0xf, 6 },{ 0x16, 9 },
{ 0x5, 10 },{ 0xe, 6 },{ 0x4, 10 },{ 0x11, 7 },
{ 0x24, 11 },{ 0x10, 7 },{ 0x25, 11 },{ 0x13, 7 },
{ 0x5a, 12 },{ 0x15, 8 },{ 0x5b, 12 },{ 0x14, 8 },
{ 0x13, 8 },{ 0x1a, 8 },{ 0x15, 9 },{ 0x14, 9 },
{ 0x13, 9 },{ 0x12, 9 },{ 0x11, 9 },{ 0x26, 11 },
{ 0x27, 11 },{ 0x5c, 12 },{ 0x5d, 12 },{ 0x5e, 12 },
{ 0x5f, 12 },{ 0x3, 7 },
};
const int8_t ff_mpeg4_intra_level[102] = {
1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24,
25, 26, 27, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 1, 2, 3,
4, 5, 1, 2, 3, 4, 1, 2,
3, 1, 2, 3, 1, 2, 3, 1,
2, 3, 1, 2, 1, 2, 1, 1,
1, 1, 1, 1, 2, 3, 4, 5,
6, 7, 8, 1, 2, 3, 1, 2,
1, 2, 1, 2, 1, 2, 1, 2,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1,
};
const int8_t ff_mpeg4_intra_run[102] = {
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 2, 2, 2,
2, 2, 3, 3, 3, 3, 4, 4,
4, 5, 5, 5, 6, 6, 6, 7,
7, 7, 8, 8, 9, 9, 10, 11,
12, 13, 14, 0, 0, 0, 0, 0,
0, 0, 0, 1, 1, 1, 2, 2,
3, 3, 4, 4, 5, 5, 6, 6,
7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20,
};
RLTable ff_mpeg4_rl_intra = {
102,
67,
ff_mpeg4_intra_vlc,
ff_mpeg4_intra_run,
ff_mpeg4_intra_level,
};
/* Note this is identical to the intra rvlc except that it is reordered. */
const uint16_t inter_rvlc[170][2]={
{0x0006, 3},{0x0001, 4},{0x0004, 5},{0x001C, 7},
{0x003C, 8},{0x003D, 8},{0x007C, 9},{0x00FC, 10},
{0x00FD, 10},{0x01FC, 11},{0x01FD, 11},{0x03FC, 12},
{0x07FC, 13},{0x07FD, 13},{0x0BFC, 13},{0x0BFD, 13},
{0x0FFC, 14},{0x0FFD, 14},{0x1FFC, 15},{0x0007, 3},
{0x000C, 6},{0x005C, 8},{0x007D, 9},{0x017C, 10},
{0x02FC, 11},{0x03FD, 12},{0x0DFC, 13},{0x17FC, 14},
{0x17FD, 14},{0x000A, 4},{0x001D, 7},{0x00BC, 9},
{0x02FD, 11},{0x05FC, 12},{0x1BFC, 14},{0x1BFD, 14},
{0x0005, 5},{0x005D, 8},{0x017D, 10},{0x05FD, 12},
{0x0DFD, 13},{0x1DFC, 14},{0x1FFD, 15},{0x0008, 5},
{0x006C, 8},{0x037C, 11},{0x0EFC, 13},{0x2FFC, 15},
{0x0009, 5},{0x00BD, 9},{0x037D, 11},{0x0EFD, 13},
{0x000D, 6},{0x01BC, 10},{0x06FC, 12},{0x1DFD, 14},
{0x0014, 6},{0x01BD, 10},{0x06FD, 12},{0x2FFD, 15},
{0x0015, 6},{0x01DC, 10},{0x0F7C, 13},{0x002C, 7},
{0x01DD, 10},{0x1EFC, 14},{0x002D, 7},{0x03BC, 11},
{0x0034, 7},{0x077C, 12},{0x006D, 8},{0x0F7D, 13},
{0x0074, 8},{0x1EFD, 14},{0x0075, 8},{0x1F7C, 14},
{0x00DC, 9},{0x1F7D, 14},{0x00DD, 9},{0x1FBC, 14},
{0x00EC, 9},{0x37FC, 15},{0x01EC, 10},{0x01ED, 10},
{0x01F4, 10},{0x03BD, 11},{0x03DC, 11},{0x03DD, 11},
{0x03EC, 11},{0x03ED, 11},{0x03F4, 11},{0x077D, 12},
{0x07BC, 12},{0x07BD, 12},{0x0FBC, 13},{0x0FBD, 13},
{0x0FDC, 13},{0x0FDD, 13},{0x1FBD, 14},{0x1FDC, 14},
{0x1FDD, 14},{0x37FD, 15},{0x3BFC, 15},
{0x000B, 4},{0x0078, 8},{0x03F5, 11},{0x0FEC, 13},
{0x1FEC, 14},{0x0012, 5},{0x00ED, 9},{0x07DC, 12},
{0x1FED, 14},{0x3BFD, 15},{0x0013, 5},{0x03F8, 11},
{0x3DFC, 15},{0x0018, 6},{0x07DD, 12},{0x0019, 6},
{0x07EC, 12},{0x0022, 6},{0x0FED, 13},{0x0023, 6},
{0x0FF4, 13},{0x0035, 7},{0x0FF5, 13},{0x0038, 7},
{0x0FF8, 13},{0x0039, 7},{0x0FF9, 13},{0x0042, 7},
{0x1FF4, 14},{0x0043, 7},{0x1FF5, 14},{0x0079, 8},
{0x1FF8, 14},{0x0082, 8},{0x3DFD, 15},{0x0083, 8},
{0x00F4, 9},{0x00F5, 9},{0x00F8, 9},{0x00F9, 9},
{0x0102, 9},{0x0103, 9},{0x01F5, 10},{0x01F8, 10},
{0x01F9, 10},{0x0202, 10},{0x0203, 10},{0x03F9, 11},
{0x0402, 11},{0x0403, 11},{0x07ED, 12},{0x07F4, 12},
{0x07F5, 12},{0x07F8, 12},{0x07F9, 12},{0x0802, 12},
{0x0803, 12},{0x1002, 13},{0x1003, 13},{0x1FF9, 14},
{0x2002, 14},{0x2003, 14},{0x3EFC, 15},{0x3EFD, 15},
{0x3F7C, 15},{0x3F7D, 15},{0x0000, 4}
};
static const int8_t inter_rvlc_run[169]={
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 2, 2, 2,
2, 2, 2, 2, 3, 3, 3, 3,
3, 3, 3, 4, 4, 4, 4, 4,
5, 5, 5, 5, 6, 6, 6, 6,
7, 7, 7, 7, 8, 8, 8, 9,
9, 9, 10, 10, 11, 11, 12, 12,
13, 13, 14, 14, 15, 15, 16, 16,
17, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31,
32, 33, 34, 35, 36, 37, 38,
0, 0, 0, 0, 0, 1, 1, 1,
1, 1, 2, 2, 2, 3, 3, 4,
4, 5, 5, 6, 6, 7, 7, 8,
8, 9, 9, 10, 10, 11, 11, 12,
12, 13, 13, 14, 15, 16, 17, 18,
19, 20, 21, 22, 23, 24, 25, 26,
27, 28, 29, 30, 31, 32, 33, 34,
35, 36, 37, 38, 39, 40, 41, 42,
43, 44,
};
static const int8_t inter_rvlc_level[169]={
1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 1, 2, 3,
4, 5, 6, 7, 1, 2, 3, 4,
5, 6, 7, 1, 2, 3, 4, 5,
1, 2, 3, 4, 1, 2, 3, 4,
1, 2, 3, 4, 1, 2, 3, 1,
2, 3, 1, 2, 1, 2, 1, 2,
1, 2, 1, 2, 1, 2, 1, 2,
1, 2, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1,
1, 2, 3, 4, 5, 1, 2, 3,
4, 5, 1, 2, 3, 1, 2, 1,
2, 1, 2, 1, 2, 1, 2, 1,
2, 1, 2, 1, 2, 1, 2, 1,
2, 1, 2, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1,
};
RLTable rvlc_rl_inter = {
169,
103,
inter_rvlc,
inter_rvlc_run,
inter_rvlc_level,
};
const uint16_t intra_rvlc[170][2]={
{0x0006, 3},{0x0007, 3},{0x000A, 4},{0x0009, 5},
{0x0014, 6},{0x0015, 6},{0x0034, 7},{0x0074, 8},
{0x0075, 8},{0x00DD, 9},{0x00EC, 9},{0x01EC, 10},
{0x01ED, 10},{0x01F4, 10},{0x03EC, 11},{0x03ED, 11},
{0x03F4, 11},{0x077D, 12},{0x07BC, 12},{0x0FBD, 13},
{0x0FDC, 13},{0x07BD, 12},{0x0FDD, 13},{0x1FBD, 14},
{0x1FDC, 14},{0x1FDD, 14},{0x1FFC, 15},{0x0001, 4},
{0x0008, 5},{0x002D, 7},{0x006C, 8},{0x006D, 8},
{0x00DC, 9},{0x01DD, 10},{0x03DC, 11},{0x03DD, 11},
{0x077C, 12},{0x0FBC, 13},{0x1F7D, 14},{0x1FBC, 14},
{0x0004, 5},{0x002C, 7},{0x00BC, 9},{0x01DC, 10},
{0x03BC, 11},{0x03BD, 11},{0x0EFD, 13},{0x0F7C, 13},
{0x0F7D, 13},{0x1EFD, 14},{0x1F7C, 14},{0x0005, 5},
{0x005C, 8},{0x00BD, 9},{0x037D, 11},{0x06FC, 12},
{0x0EFC, 13},{0x1DFD, 14},{0x1EFC, 14},{0x1FFD, 15},
{0x000C, 6},{0x005D, 8},{0x01BD, 10},{0x03FD, 12},
{0x06FD, 12},{0x1BFD, 14},{0x000D, 6},{0x007D, 9},
{0x02FC, 11},{0x05FC, 12},{0x1BFC, 14},{0x1DFC, 14},
{0x001C, 7},{0x017C, 10},{0x02FD, 11},{0x05FD, 12},
{0x2FFC, 15},{0x001D, 7},{0x017D, 10},{0x037C, 11},
{0x0DFD, 13},{0x2FFD, 15},{0x003C, 8},{0x01BC, 10},
{0x0BFD, 13},{0x17FD, 14},{0x003D, 8},{0x01FD, 11},
{0x0DFC, 13},{0x37FC, 15},{0x007C, 9},{0x03FC, 12},
{0x00FC, 10},{0x0BFC, 13},{0x00FD, 10},{0x37FD, 15},
{0x01FC, 11},{0x07FC, 13},{0x07FD, 13},{0x0FFC, 14},
{0x0FFD, 14},{0x17FC, 14},{0x3BFC, 15},
{0x000B, 4},{0x0078, 8},{0x03F5, 11},{0x0FEC, 13},
{0x1FEC, 14},{0x0012, 5},{0x00ED, 9},{0x07DC, 12},
{0x1FED, 14},{0x3BFD, 15},{0x0013, 5},{0x03F8, 11},
{0x3DFC, 15},{0x0018, 6},{0x07DD, 12},{0x0019, 6},
{0x07EC, 12},{0x0022, 6},{0x0FED, 13},{0x0023, 6},
{0x0FF4, 13},{0x0035, 7},{0x0FF5, 13},{0x0038, 7},
{0x0FF8, 13},{0x0039, 7},{0x0FF9, 13},{0x0042, 7},
{0x1FF4, 14},{0x0043, 7},{0x1FF5, 14},{0x0079, 8},
{0x1FF8, 14},{0x0082, 8},{0x3DFD, 15},{0x0083, 8},
{0x00F4, 9},{0x00F5, 9},{0x00F8, 9},{0x00F9, 9},
{0x0102, 9},{0x0103, 9},{0x01F5, 10},{0x01F8, 10},
{0x01F9, 10},{0x0202, 10},{0x0203, 10},{0x03F9, 11},
{0x0402, 11},{0x0403, 11},{0x07ED, 12},{0x07F4, 12},
{0x07F5, 12},{0x07F8, 12},{0x07F9, 12},{0x0802, 12},
{0x0803, 12},{0x1002, 13},{0x1003, 13},{0x1FF9, 14},
{0x2002, 14},{0x2003, 14},{0x3EFC, 15},{0x3EFD, 15},
{0x3F7C, 15},{0x3F7D, 15},{0x0000, 4}
};
static const int8_t intra_rvlc_run[169]={
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 3, 3, 3, 3, 3,
3, 3, 3, 3, 4, 4, 4, 4,
4, 4, 5, 5, 5, 5, 5, 5,
6, 6, 6, 6, 6, 7, 7, 7,
7, 7, 8, 8, 8, 8, 9, 9,
9, 9, 10, 10, 11, 11, 12, 12,
13, 14, 15, 16, 17, 18, 19,
0, 0, 0, 0, 0, 1, 1, 1,
1, 1, 2, 2, 2, 3, 3, 4,
4, 5, 5, 6, 6, 7, 7, 8,
8, 9, 9, 10, 10, 11, 11, 12,
12, 13, 13, 14, 15, 16, 17, 18,
19, 20, 21, 22, 23, 24, 25, 26,
27, 28, 29, 30, 31, 32, 33, 34,
35, 36, 37, 38, 39, 40, 41, 42,
43, 44,
};
static const int8_t intra_rvlc_level[169]={
1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24,
25, 26, 27, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 11, 12, 13,
1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 1, 2, 3, 4, 5,
6, 7, 8, 9, 1, 2, 3, 4,
5, 6, 1, 2, 3, 4, 5, 6,
1, 2, 3, 4, 5, 1, 2, 3,
4, 5, 1, 2, 3, 4, 1, 2,
3, 4, 1, 2, 1, 2, 1, 2,
1, 1, 1, 1, 1, 1, 1,
1, 2, 3, 4, 5, 1, 2, 3,
4, 5, 1, 2, 3, 1, 2, 1,
2, 1, 2, 1, 2, 1, 2, 1,
2, 1, 2, 1, 2, 1, 2, 1,
2, 1, 2, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1,
};
RLTable rvlc_rl_intra = {
169,
103,
intra_rvlc,
intra_rvlc_run,
intra_rvlc_level,
};
const uint16_t sprite_trajectory_tab[15][2] = {
{0x00, 2}, {0x02, 3}, {0x03, 3}, {0x04, 3}, {0x05, 3}, {0x06, 3},
{0x0E, 4}, {0x1E, 5}, {0x3E, 6}, {0x7E, 7}, {0xFE, 8},
{0x1FE, 9},{0x3FE, 10},{0x7FE, 11},{0xFFE, 12},
};
const uint8_t mb_type_b_tab[4][2] = {
{1, 1}, {1, 2}, {1, 3}, {1, 4},
};
/* these matrixes will be permuted for the idct */
const int16_t ff_mpeg4_default_intra_matrix[64] = {
8, 17, 18, 19, 21, 23, 25, 27,
17, 18, 19, 21, 23, 25, 27, 28,
20, 21, 22, 23, 24, 26, 28, 30,
21, 22, 23, 24, 26, 28, 30, 32,
22, 23, 24, 26, 28, 30, 32, 35,
23, 24, 26, 28, 30, 32, 35, 38,
25, 26, 28, 30, 32, 35, 38, 41,
27, 28, 30, 32, 35, 38, 41, 45,
};
const int16_t ff_mpeg4_default_non_intra_matrix[64] = {
16, 17, 18, 19, 20, 21, 22, 23,
17, 18, 19, 20, 21, 22, 23, 24,
18, 19, 20, 21, 22, 23, 24, 25,
19, 20, 21, 22, 23, 24, 26, 27,
20, 21, 22, 23, 25, 26, 27, 28,
21, 22, 23, 24, 26, 27, 28, 30,
22, 23, 24, 26, 27, 28, 30, 31,
23, 24, 25, 27, 28, 30, 31, 33,
};
const uint8_t ff_mpeg4_y_dc_scale_table[32]={
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
0, 8, 8, 8, 8,10,12,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,34,36,38,40,42,44,46
};
const uint8_t ff_mpeg4_c_dc_scale_table[32]={
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
0, 8, 8, 8, 8, 9, 9,10,10,11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18,19,20,21,22,23,24,25
};
const uint16_t ff_mpeg4_resync_prefix[8]={
0x7F00, 0x7E00, 0x7C00, 0x7800, 0x7000, 0x6000, 0x4000, 0x0000
};
const uint8_t mpeg4_dc_threshold[8]={
99, 13, 15, 17, 19, 21, 23, 0
};
#endif /* AVCODEC_MPEG4DATA_H */
| 123linslouis-android-video-cutter | jni/libavcodec/mpeg4data.h | C | asf20 | 13,222 |
/*
* Alpha optimized DSP utils
* Copyright (c) 2002 Falk Hueffner <falk@debian.org>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef AVCODEC_ALPHA_ASM_H
#define AVCODEC_ALPHA_ASM_H
#include <inttypes.h>
#include "libavutil/common.h"
#if AV_GCC_VERSION_AT_LEAST(2,96)
# define likely(x) __builtin_expect((x) != 0, 1)
# define unlikely(x) __builtin_expect((x) != 0, 0)
#else
# define likely(x) (x)
# define unlikely(x) (x)
#endif
#define AMASK_BWX (1 << 0)
#define AMASK_FIX (1 << 1)
#define AMASK_CIX (1 << 2)
#define AMASK_MVI (1 << 8)
static inline uint64_t BYTE_VEC(uint64_t x)
{
x |= x << 8;
x |= x << 16;
x |= x << 32;
return x;
}
static inline uint64_t WORD_VEC(uint64_t x)
{
x |= x << 16;
x |= x << 32;
return x;
}
#define sextw(x) ((int16_t) (x))
#ifdef __GNUC__
#define ldq(p) \
(((const union { \
uint64_t __l; \
__typeof__(*(p)) __s[sizeof (uint64_t) / sizeof *(p)]; \
} *) (p))->__l)
#define ldl(p) \
(((const union { \
int32_t __l; \
__typeof__(*(p)) __s[sizeof (int32_t) / sizeof *(p)]; \
} *) (p))->__l)
#define stq(l, p) \
do { \
(((union { \
uint64_t __l; \
__typeof__(*(p)) __s[sizeof (uint64_t) / sizeof *(p)]; \
} *) (p))->__l) = l; \
} while (0)
#define stl(l, p) \
do { \
(((union { \
int32_t __l; \
__typeof__(*(p)) __s[sizeof (int32_t) / sizeof *(p)]; \
} *) (p))->__l) = l; \
} while (0)
struct unaligned_long { uint64_t l; } __attribute__((packed));
#define ldq_u(p) (*(const uint64_t *) (((uint64_t) (p)) & ~7ul))
#define uldq(a) (((const struct unaligned_long *) (a))->l)
#if AV_GCC_VERSION_AT_LEAST(3,3)
#define prefetch(p) __builtin_prefetch((p), 0, 1)
#define prefetch_en(p) __builtin_prefetch((p), 0, 0)
#define prefetch_m(p) __builtin_prefetch((p), 1, 1)
#define prefetch_men(p) __builtin_prefetch((p), 1, 0)
#define cmpbge __builtin_alpha_cmpbge
/* Avoid warnings. */
#define extql(a, b) __builtin_alpha_extql(a, (uint64_t) (b))
#define extwl(a, b) __builtin_alpha_extwl(a, (uint64_t) (b))
#define extqh(a, b) __builtin_alpha_extqh(a, (uint64_t) (b))
#define zap __builtin_alpha_zap
#define zapnot __builtin_alpha_zapnot
#define amask __builtin_alpha_amask
#define implver __builtin_alpha_implver
#define rpcc __builtin_alpha_rpcc
#else
#define prefetch(p) __asm__ volatile("ldl $31,%0" : : "m"(*(const char *) (p)) : "memory")
#define prefetch_en(p) __asm__ volatile("ldq $31,%0" : : "m"(*(const char *) (p)) : "memory")
#define prefetch_m(p) __asm__ volatile("lds $f31,%0" : : "m"(*(const char *) (p)) : "memory")
#define prefetch_men(p) __asm__ volatile("ldt $f31,%0" : : "m"(*(const char *) (p)) : "memory")
#define cmpbge(a, b) ({ uint64_t __r; __asm__ ("cmpbge %r1,%2,%0" : "=r" (__r) : "rJ" (a), "rI" (b)); __r; })
#define extql(a, b) ({ uint64_t __r; __asm__ ("extql %r1,%2,%0" : "=r" (__r) : "rJ" (a), "rI" (b)); __r; })
#define extwl(a, b) ({ uint64_t __r; __asm__ ("extwl %r1,%2,%0" : "=r" (__r) : "rJ" (a), "rI" (b)); __r; })
#define extqh(a, b) ({ uint64_t __r; __asm__ ("extqh %r1,%2,%0" : "=r" (__r) : "rJ" (a), "rI" (b)); __r; })
#define zap(a, b) ({ uint64_t __r; __asm__ ("zap %r1,%2,%0" : "=r" (__r) : "rJ" (a), "rI" (b)); __r; })
#define zapnot(a, b) ({ uint64_t __r; __asm__ ("zapnot %r1,%2,%0" : "=r" (__r) : "rJ" (a), "rI" (b)); __r; })
#define amask(a) ({ uint64_t __r; __asm__ ("amask %1,%0" : "=r" (__r) : "rI" (a)); __r; })
#define implver() ({ uint64_t __r; __asm__ ("implver %0" : "=r" (__r)); __r; })
#define rpcc() ({ uint64_t __r; __asm__ volatile ("rpcc %0" : "=r" (__r)); __r; })
#endif
#define wh64(p) __asm__ volatile("wh64 (%0)" : : "r"(p) : "memory")
#if AV_GCC_VERSION_AT_LEAST(3,3) && defined(__alpha_max__)
#define minub8 __builtin_alpha_minub8
#define minsb8 __builtin_alpha_minsb8
#define minuw4 __builtin_alpha_minuw4
#define minsw4 __builtin_alpha_minsw4
#define maxub8 __builtin_alpha_maxub8
#define maxsb8 __builtin_alpha_maxsb8
#define maxuw4 __builtin_alpha_maxuw4
#define maxsw4 __builtin_alpha_maxsw4
#define perr __builtin_alpha_perr
#define pklb __builtin_alpha_pklb
#define pkwb __builtin_alpha_pkwb
#define unpkbl __builtin_alpha_unpkbl
#define unpkbw __builtin_alpha_unpkbw
#else
#define minub8(a, b) ({ uint64_t __r; __asm__ (".arch ev6; minub8 %r1,%2,%0" : "=r" (__r) : "%rJ" (a), "rI" (b)); __r; })
#define minsb8(a, b) ({ uint64_t __r; __asm__ (".arch ev6; minsb8 %r1,%2,%0" : "=r" (__r) : "%rJ" (a), "rI" (b)); __r; })
#define minuw4(a, b) ({ uint64_t __r; __asm__ (".arch ev6; minuw4 %r1,%2,%0" : "=r" (__r) : "%rJ" (a), "rI" (b)); __r; })
#define minsw4(a, b) ({ uint64_t __r; __asm__ (".arch ev6; minsw4 %r1,%2,%0" : "=r" (__r) : "%rJ" (a), "rI" (b)); __r; })
#define maxub8(a, b) ({ uint64_t __r; __asm__ (".arch ev6; maxub8 %r1,%2,%0" : "=r" (__r) : "%rJ" (a), "rI" (b)); __r; })
#define maxsb8(a, b) ({ uint64_t __r; __asm__ (".arch ev6; maxsb8 %r1,%2,%0" : "=r" (__r) : "%rJ" (a), "rI" (b)); __r; })
#define maxuw4(a, b) ({ uint64_t __r; __asm__ (".arch ev6; maxuw4 %r1,%2,%0" : "=r" (__r) : "%rJ" (a), "rI" (b)); __r; })
#define maxsw4(a, b) ({ uint64_t __r; __asm__ (".arch ev6; maxsw4 %r1,%2,%0" : "=r" (__r) : "%rJ" (a), "rI" (b)); __r; })
#define perr(a, b) ({ uint64_t __r; __asm__ (".arch ev6; perr %r1,%r2,%0" : "=r" (__r) : "%rJ" (a), "rJ" (b)); __r; })
#define pklb(a) ({ uint64_t __r; __asm__ (".arch ev6; pklb %r1,%0" : "=r" (__r) : "rJ" (a)); __r; })
#define pkwb(a) ({ uint64_t __r; __asm__ (".arch ev6; pkwb %r1,%0" : "=r" (__r) : "rJ" (a)); __r; })
#define unpkbl(a) ({ uint64_t __r; __asm__ (".arch ev6; unpkbl %r1,%0" : "=r" (__r) : "rJ" (a)); __r; })
#define unpkbw(a) ({ uint64_t __r; __asm__ (".arch ev6; unpkbw %r1,%0" : "=r" (__r) : "rJ" (a)); __r; })
#endif
#elif defined(__DECC) /* Digital/Compaq/hp "ccc" compiler */
#include <c_asm.h>
#define ldq(p) (*(const uint64_t *) (p))
#define ldl(p) (*(const int32_t *) (p))
#define stq(l, p) do { *(uint64_t *) (p) = (l); } while (0)
#define stl(l, p) do { *(int32_t *) (p) = (l); } while (0)
#define ldq_u(a) asm ("ldq_u %v0,0(%a0)", a)
#define uldq(a) (*(const __unaligned uint64_t *) (a))
#define cmpbge(a, b) asm ("cmpbge %a0,%a1,%v0", a, b)
#define extql(a, b) asm ("extql %a0,%a1,%v0", a, b)
#define extwl(a, b) asm ("extwl %a0,%a1,%v0", a, b)
#define extqh(a, b) asm ("extqh %a0,%a1,%v0", a, b)
#define zap(a, b) asm ("zap %a0,%a1,%v0", a, b)
#define zapnot(a, b) asm ("zapnot %a0,%a1,%v0", a, b)
#define amask(a) asm ("amask %a0,%v0", a)
#define implver() asm ("implver %v0")
#define rpcc() asm ("rpcc %v0")
#define minub8(a, b) asm ("minub8 %a0,%a1,%v0", a, b)
#define minsb8(a, b) asm ("minsb8 %a0,%a1,%v0", a, b)
#define minuw4(a, b) asm ("minuw4 %a0,%a1,%v0", a, b)
#define minsw4(a, b) asm ("minsw4 %a0,%a1,%v0", a, b)
#define maxub8(a, b) asm ("maxub8 %a0,%a1,%v0", a, b)
#define maxsb8(a, b) asm ("maxsb8 %a0,%a1,%v0", a, b)
#define maxuw4(a, b) asm ("maxuw4 %a0,%a1,%v0", a, b)
#define maxsw4(a, b) asm ("maxsw4 %a0,%a1,%v0", a, b)
#define perr(a, b) asm ("perr %a0,%a1,%v0", a, b)
#define pklb(a) asm ("pklb %a0,%v0", a)
#define pkwb(a) asm ("pkwb %a0,%v0", a)
#define unpkbl(a) asm ("unpkbl %a0,%v0", a)
#define unpkbw(a) asm ("unpkbw %a0,%v0", a)
#define wh64(a) asm ("wh64 %a0", a)
#else
#error "Unknown compiler!"
#endif
#endif /* AVCODEC_ALPHA_ASM_H */
| 123linslouis-android-video-cutter | jni/libavcodec/alpha/asm.h | C | asf20 | 9,327 |
/*
* Alpha optimized DSP utils
* Copyright (c) 2002 Falk Hueffner <falk@debian.org>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "libavcodec/dsputil.h"
#include "dsputil_alpha.h"
#include "asm.h"
void get_pixels_mvi(DCTELEM *restrict block,
const uint8_t *restrict pixels, int line_size)
{
int h = 8;
do {
uint64_t p;
p = ldq(pixels);
stq(unpkbw(p), block);
stq(unpkbw(p >> 32), block + 4);
pixels += line_size;
block += 8;
} while (--h);
}
void diff_pixels_mvi(DCTELEM *block, const uint8_t *s1, const uint8_t *s2,
int stride) {
int h = 8;
uint64_t mask = 0x4040;
mask |= mask << 16;
mask |= mask << 32;
do {
uint64_t x, y, c, d, a;
uint64_t signs;
x = ldq(s1);
y = ldq(s2);
c = cmpbge(x, y);
d = x - y;
a = zap(mask, c); /* We use 0x4040404040404040 here... */
d += 4 * a; /* ...so we can use s4addq here. */
signs = zap(-1, c);
stq(unpkbw(d) | (unpkbw(signs) << 8), block);
stq(unpkbw(d >> 32) | (unpkbw(signs >> 32) << 8), block + 4);
s1 += stride;
s2 += stride;
block += 8;
} while (--h);
}
static inline uint64_t avg2(uint64_t a, uint64_t b)
{
return (a | b) - (((a ^ b) & BYTE_VEC(0xfe)) >> 1);
}
static inline uint64_t avg4(uint64_t l1, uint64_t l2, uint64_t l3, uint64_t l4)
{
uint64_t r1 = ((l1 & ~BYTE_VEC(0x03)) >> 2)
+ ((l2 & ~BYTE_VEC(0x03)) >> 2)
+ ((l3 & ~BYTE_VEC(0x03)) >> 2)
+ ((l4 & ~BYTE_VEC(0x03)) >> 2);
uint64_t r2 = (( (l1 & BYTE_VEC(0x03))
+ (l2 & BYTE_VEC(0x03))
+ (l3 & BYTE_VEC(0x03))
+ (l4 & BYTE_VEC(0x03))
+ BYTE_VEC(0x02)) >> 2) & BYTE_VEC(0x03);
return r1 + r2;
}
int pix_abs8x8_mvi(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
{
int result = 0;
if ((size_t) pix2 & 0x7) {
/* works only when pix2 is actually unaligned */
do { /* do 8 pixel a time */
uint64_t p1, p2;
p1 = ldq(pix1);
p2 = uldq(pix2);
result += perr(p1, p2);
pix1 += line_size;
pix2 += line_size;
} while (--h);
} else {
do {
uint64_t p1, p2;
p1 = ldq(pix1);
p2 = ldq(pix2);
result += perr(p1, p2);
pix1 += line_size;
pix2 += line_size;
} while (--h);
}
return result;
}
#if 0 /* now done in assembly */
int pix_abs16x16_mvi(uint8_t *pix1, uint8_t *pix2, int line_size)
{
int result = 0;
int h = 16;
if ((size_t) pix2 & 0x7) {
/* works only when pix2 is actually unaligned */
do { /* do 16 pixel a time */
uint64_t p1_l, p1_r, p2_l, p2_r;
uint64_t t;
p1_l = ldq(pix1);
p1_r = ldq(pix1 + 8);
t = ldq_u(pix2 + 8);
p2_l = extql(ldq_u(pix2), pix2) | extqh(t, pix2);
p2_r = extql(t, pix2) | extqh(ldq_u(pix2 + 16), pix2);
pix1 += line_size;
pix2 += line_size;
result += perr(p1_l, p2_l)
+ perr(p1_r, p2_r);
} while (--h);
} else {
do {
uint64_t p1_l, p1_r, p2_l, p2_r;
p1_l = ldq(pix1);
p1_r = ldq(pix1 + 8);
p2_l = ldq(pix2);
p2_r = ldq(pix2 + 8);
pix1 += line_size;
pix2 += line_size;
result += perr(p1_l, p2_l)
+ perr(p1_r, p2_r);
} while (--h);
}
return result;
}
#endif
int pix_abs16x16_x2_mvi(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
{
int result = 0;
uint64_t disalign = (size_t) pix2 & 0x7;
switch (disalign) {
case 0:
do {
uint64_t p1_l, p1_r, p2_l, p2_r;
uint64_t l, r;
p1_l = ldq(pix1);
p1_r = ldq(pix1 + 8);
l = ldq(pix2);
r = ldq(pix2 + 8);
p2_l = avg2(l, (l >> 8) | ((uint64_t) r << 56));
p2_r = avg2(r, (r >> 8) | ((uint64_t) pix2[16] << 56));
pix1 += line_size;
pix2 += line_size;
result += perr(p1_l, p2_l)
+ perr(p1_r, p2_r);
} while (--h);
break;
case 7:
/* |.......l|lllllllr|rrrrrrr*|
This case is special because disalign1 would be 8, which
gets treated as 0 by extqh. At least it is a bit faster
that way :) */
do {
uint64_t p1_l, p1_r, p2_l, p2_r;
uint64_t l, m, r;
p1_l = ldq(pix1);
p1_r = ldq(pix1 + 8);
l = ldq_u(pix2);
m = ldq_u(pix2 + 8);
r = ldq_u(pix2 + 16);
p2_l = avg2(extql(l, disalign) | extqh(m, disalign), m);
p2_r = avg2(extql(m, disalign) | extqh(r, disalign), r);
pix1 += line_size;
pix2 += line_size;
result += perr(p1_l, p2_l)
+ perr(p1_r, p2_r);
} while (--h);
break;
default:
do {
uint64_t disalign1 = disalign + 1;
uint64_t p1_l, p1_r, p2_l, p2_r;
uint64_t l, m, r;
p1_l = ldq(pix1);
p1_r = ldq(pix1 + 8);
l = ldq_u(pix2);
m = ldq_u(pix2 + 8);
r = ldq_u(pix2 + 16);
p2_l = avg2(extql(l, disalign) | extqh(m, disalign),
extql(l, disalign1) | extqh(m, disalign1));
p2_r = avg2(extql(m, disalign) | extqh(r, disalign),
extql(m, disalign1) | extqh(r, disalign1));
pix1 += line_size;
pix2 += line_size;
result += perr(p1_l, p2_l)
+ perr(p1_r, p2_r);
} while (--h);
break;
}
return result;
}
int pix_abs16x16_y2_mvi(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
{
int result = 0;
if ((size_t) pix2 & 0x7) {
uint64_t t, p2_l, p2_r;
t = ldq_u(pix2 + 8);
p2_l = extql(ldq_u(pix2), pix2) | extqh(t, pix2);
p2_r = extql(t, pix2) | extqh(ldq_u(pix2 + 16), pix2);
do {
uint64_t p1_l, p1_r, np2_l, np2_r;
uint64_t t;
p1_l = ldq(pix1);
p1_r = ldq(pix1 + 8);
pix2 += line_size;
t = ldq_u(pix2 + 8);
np2_l = extql(ldq_u(pix2), pix2) | extqh(t, pix2);
np2_r = extql(t, pix2) | extqh(ldq_u(pix2 + 16), pix2);
result += perr(p1_l, avg2(p2_l, np2_l))
+ perr(p1_r, avg2(p2_r, np2_r));
pix1 += line_size;
p2_l = np2_l;
p2_r = np2_r;
} while (--h);
} else {
uint64_t p2_l, p2_r;
p2_l = ldq(pix2);
p2_r = ldq(pix2 + 8);
do {
uint64_t p1_l, p1_r, np2_l, np2_r;
p1_l = ldq(pix1);
p1_r = ldq(pix1 + 8);
pix2 += line_size;
np2_l = ldq(pix2);
np2_r = ldq(pix2 + 8);
result += perr(p1_l, avg2(p2_l, np2_l))
+ perr(p1_r, avg2(p2_r, np2_r));
pix1 += line_size;
p2_l = np2_l;
p2_r = np2_r;
} while (--h);
}
return result;
}
int pix_abs16x16_xy2_mvi(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
{
int result = 0;
uint64_t p1_l, p1_r;
uint64_t p2_l, p2_r, p2_x;
p1_l = ldq(pix1);
p1_r = ldq(pix1 + 8);
if ((size_t) pix2 & 0x7) { /* could be optimized a lot */
p2_l = uldq(pix2);
p2_r = uldq(pix2 + 8);
p2_x = (uint64_t) pix2[16] << 56;
} else {
p2_l = ldq(pix2);
p2_r = ldq(pix2 + 8);
p2_x = ldq(pix2 + 16) << 56;
}
do {
uint64_t np1_l, np1_r;
uint64_t np2_l, np2_r, np2_x;
pix1 += line_size;
pix2 += line_size;
np1_l = ldq(pix1);
np1_r = ldq(pix1 + 8);
if ((size_t) pix2 & 0x7) { /* could be optimized a lot */
np2_l = uldq(pix2);
np2_r = uldq(pix2 + 8);
np2_x = (uint64_t) pix2[16] << 56;
} else {
np2_l = ldq(pix2);
np2_r = ldq(pix2 + 8);
np2_x = ldq(pix2 + 16) << 56;
}
result += perr(p1_l,
avg4( p2_l, ( p2_l >> 8) | ((uint64_t) p2_r << 56),
np2_l, (np2_l >> 8) | ((uint64_t) np2_r << 56)))
+ perr(p1_r,
avg4( p2_r, ( p2_r >> 8) | ((uint64_t) p2_x),
np2_r, (np2_r >> 8) | ((uint64_t) np2_x)));
p1_l = np1_l;
p1_r = np1_r;
p2_l = np2_l;
p2_r = np2_r;
p2_x = np2_x;
} while (--h);
return result;
}
| 123linslouis-android-video-cutter | jni/libavcodec/alpha/motion_est_alpha.c | C | asf20 | 9,835 |
/*
* Alpha optimized DSP utils
* Copyright (c) 2002 Falk Hueffner <falk@debian.org>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
* These functions are scheduled for pca56. They should work
* reasonably on ev6, though.
*/
#include "regdef.h"
/* Some nicer register names. */
#define ta t10
#define tb t11
#define tc t12
#define td AT
/* Danger: these overlap with the argument list and the return value */
#define te a5
#define tf a4
#define tg a3
#define th v0
.set noat
.set noreorder
.arch pca56
.text
/************************************************************************
* void put_pixels_axp_asm(uint8_t *block, const uint8_t *pixels,
* int line_size, int h)
*/
.align 6
.globl put_pixels_axp_asm
.ent put_pixels_axp_asm
put_pixels_axp_asm:
.frame sp, 0, ra
.prologue 0
#if CONFIG_GPROF
lda AT, _mcount
jsr AT, (AT), _mcount
#endif
and a1, 7, t0
beq t0, $aligned
.align 4
$unaligned:
ldq_u t0, 0(a1)
ldq_u t1, 8(a1)
addq a1, a2, a1
nop
ldq_u t2, 0(a1)
ldq_u t3, 8(a1)
addq a1, a2, a1
nop
ldq_u t4, 0(a1)
ldq_u t5, 8(a1)
addq a1, a2, a1
nop
ldq_u t6, 0(a1)
ldq_u t7, 8(a1)
extql t0, a1, t0
addq a1, a2, a1
extqh t1, a1, t1
addq a0, a2, t8
extql t2, a1, t2
addq t8, a2, t9
extqh t3, a1, t3
addq t9, a2, ta
extql t4, a1, t4
or t0, t1, t0
extqh t5, a1, t5
or t2, t3, t2
extql t6, a1, t6
or t4, t5, t4
extqh t7, a1, t7
or t6, t7, t6
stq t0, 0(a0)
stq t2, 0(t8)
stq t4, 0(t9)
subq a3, 4, a3
stq t6, 0(ta)
addq ta, a2, a0
bne a3, $unaligned
ret
.align 4
$aligned:
ldq t0, 0(a1)
addq a1, a2, a1
ldq t1, 0(a1)
addq a1, a2, a1
ldq t2, 0(a1)
addq a1, a2, a1
ldq t3, 0(a1)
addq a0, a2, t4
addq a1, a2, a1
addq t4, a2, t5
subq a3, 4, a3
stq t0, 0(a0)
addq t5, a2, t6
stq t1, 0(t4)
addq t6, a2, a0
stq t2, 0(t5)
stq t3, 0(t6)
bne a3, $aligned
ret
.end put_pixels_axp_asm
/************************************************************************
* void put_pixels_clamped_mvi_asm(const DCTELEM *block, uint8_t *pixels,
* int line_size)
*/
.align 6
.globl put_pixels_clamped_mvi_asm
.ent put_pixels_clamped_mvi_asm
put_pixels_clamped_mvi_asm:
.frame sp, 0, ra
.prologue 0
#if CONFIG_GPROF
lda AT, _mcount
jsr AT, (AT), _mcount
#endif
lda t8, -1
lda t9, 8 # loop counter
zap t8, 0xaa, t8 # 00ff00ff00ff00ff
.align 4
1: ldq t0, 0(a0)
ldq t1, 8(a0)
ldq t2, 16(a0)
ldq t3, 24(a0)
maxsw4 t0, zero, t0
subq t9, 2, t9
maxsw4 t1, zero, t1
lda a0, 32(a0)
maxsw4 t2, zero, t2
addq a1, a2, ta
maxsw4 t3, zero, t3
minsw4 t0, t8, t0
minsw4 t1, t8, t1
minsw4 t2, t8, t2
minsw4 t3, t8, t3
pkwb t0, t0
pkwb t1, t1
pkwb t2, t2
pkwb t3, t3
stl t0, 0(a1)
stl t1, 4(a1)
addq ta, a2, a1
stl t2, 0(ta)
stl t3, 4(ta)
bne t9, 1b
ret
.end put_pixels_clamped_mvi_asm
/************************************************************************
* void add_pixels_clamped_mvi_asm(const DCTELEM *block, uint8_t *pixels,
* int line_size)
*/
.align 6
.globl add_pixels_clamped_mvi_asm
.ent add_pixels_clamped_mvi_asm
add_pixels_clamped_mvi_asm:
.frame sp, 0, ra
.prologue 0
#if CONFIG_GPROF
lda AT, _mcount
jsr AT, (AT), _mcount
#endif
lda t1, -1
lda th, 8
zap t1, 0x33, tg
nop
srl tg, 1, t0
xor tg, t0, tg # 0x8000800080008000
zap t1, 0xaa, tf # 0x00ff00ff00ff00ff
.align 4
1: ldl t1, 0(a1) # pix0 (try to hit cache line soon)
ldl t4, 4(a1) # pix1
addq a1, a2, te # pixels += line_size
ldq t0, 0(a0) # shorts0
ldl t7, 0(te) # pix2 (try to hit cache line soon)
ldl ta, 4(te) # pix3
ldq t3, 8(a0) # shorts1
ldq t6, 16(a0) # shorts2
ldq t9, 24(a0) # shorts3
unpkbw t1, t1 # 0 0 (quarter/op no.)
and t0, tg, t2 # 0 1
unpkbw t4, t4 # 1 0
bic t0, tg, t0 # 0 2
unpkbw t7, t7 # 2 0
and t3, tg, t5 # 1 1
addq t0, t1, t0 # 0 3
xor t0, t2, t0 # 0 4
unpkbw ta, ta # 3 0
and t6, tg, t8 # 2 1
maxsw4 t0, zero, t0 # 0 5
bic t3, tg, t3 # 1 2
bic t6, tg, t6 # 2 2
minsw4 t0, tf, t0 # 0 6
addq t3, t4, t3 # 1 3
pkwb t0, t0 # 0 7
xor t3, t5, t3 # 1 4
maxsw4 t3, zero, t3 # 1 5
addq t6, t7, t6 # 2 3
xor t6, t8, t6 # 2 4
and t9, tg, tb # 3 1
minsw4 t3, tf, t3 # 1 6
bic t9, tg, t9 # 3 2
maxsw4 t6, zero, t6 # 2 5
addq t9, ta, t9 # 3 3
stl t0, 0(a1) # 0 8
minsw4 t6, tf, t6 # 2 6
xor t9, tb, t9 # 3 4
maxsw4 t9, zero, t9 # 3 5
lda a0, 32(a0) # block += 16;
pkwb t3, t3 # 1 7
minsw4 t9, tf, t9 # 3 6
subq th, 2, th
pkwb t6, t6 # 2 7
pkwb t9, t9 # 3 7
stl t3, 4(a1) # 1 8
addq te, a2, a1 # pixels += line_size
stl t6, 0(te) # 2 8
stl t9, 4(te) # 3 8
bne th, 1b
ret
.end add_pixels_clamped_mvi_asm
| 123linslouis-android-video-cutter | jni/libavcodec/alpha/dsputil_alpha_asm.S | Motorola 68K Assembly | asf20 | 7,357 |
/*
* Alpha optimized DSP utils
* Copyright (c) 2002 Falk Hueffner <falk@debian.org>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "libavcodec/dsputil.h"
#include "dsputil_alpha.h"
#include "asm.h"
void (*put_pixels_clamped_axp_p)(const DCTELEM *block, uint8_t *pixels,
int line_size);
void (*add_pixels_clamped_axp_p)(const DCTELEM *block, uint8_t *pixels,
int line_size);
#if 0
/* These functions were the base for the optimized assembler routines,
and remain here for documentation purposes. */
static void put_pixels_clamped_mvi(const DCTELEM *block, uint8_t *pixels,
int line_size)
{
int i = 8;
uint64_t clampmask = zap(-1, 0xaa); /* 0x00ff00ff00ff00ff */
do {
uint64_t shorts0, shorts1;
shorts0 = ldq(block);
shorts0 = maxsw4(shorts0, 0);
shorts0 = minsw4(shorts0, clampmask);
stl(pkwb(shorts0), pixels);
shorts1 = ldq(block + 4);
shorts1 = maxsw4(shorts1, 0);
shorts1 = minsw4(shorts1, clampmask);
stl(pkwb(shorts1), pixels + 4);
pixels += line_size;
block += 8;
} while (--i);
}
void add_pixels_clamped_mvi(const DCTELEM *block, uint8_t *pixels,
int line_size)
{
int h = 8;
/* Keep this function a leaf function by generating the constants
manually (mainly for the hack value ;-). */
uint64_t clampmask = zap(-1, 0xaa); /* 0x00ff00ff00ff00ff */
uint64_t signmask = zap(-1, 0x33);
signmask ^= signmask >> 1; /* 0x8000800080008000 */
do {
uint64_t shorts0, pix0, signs0;
uint64_t shorts1, pix1, signs1;
shorts0 = ldq(block);
shorts1 = ldq(block + 4);
pix0 = unpkbw(ldl(pixels));
/* Signed subword add (MMX paddw). */
signs0 = shorts0 & signmask;
shorts0 &= ~signmask;
shorts0 += pix0;
shorts0 ^= signs0;
/* Clamp. */
shorts0 = maxsw4(shorts0, 0);
shorts0 = minsw4(shorts0, clampmask);
/* Next 4. */
pix1 = unpkbw(ldl(pixels + 4));
signs1 = shorts1 & signmask;
shorts1 &= ~signmask;
shorts1 += pix1;
shorts1 ^= signs1;
shorts1 = maxsw4(shorts1, 0);
shorts1 = minsw4(shorts1, clampmask);
stl(pkwb(shorts0), pixels);
stl(pkwb(shorts1), pixels + 4);
pixels += line_size;
block += 8;
} while (--h);
}
#endif
static void clear_blocks_axp(DCTELEM *blocks) {
uint64_t *p = (uint64_t *) blocks;
int n = sizeof(DCTELEM) * 6 * 64;
do {
p[0] = 0;
p[1] = 0;
p[2] = 0;
p[3] = 0;
p[4] = 0;
p[5] = 0;
p[6] = 0;
p[7] = 0;
p += 8;
n -= 8 * 8;
} while (n);
}
static inline uint64_t avg2_no_rnd(uint64_t a, uint64_t b)
{
return (a & b) + (((a ^ b) & BYTE_VEC(0xfe)) >> 1);
}
static inline uint64_t avg2(uint64_t a, uint64_t b)
{
return (a | b) - (((a ^ b) & BYTE_VEC(0xfe)) >> 1);
}
#if 0
/* The XY2 routines basically utilize this scheme, but reuse parts in
each iteration. */
static inline uint64_t avg4(uint64_t l1, uint64_t l2, uint64_t l3, uint64_t l4)
{
uint64_t r1 = ((l1 & ~BYTE_VEC(0x03)) >> 2)
+ ((l2 & ~BYTE_VEC(0x03)) >> 2)
+ ((l3 & ~BYTE_VEC(0x03)) >> 2)
+ ((l4 & ~BYTE_VEC(0x03)) >> 2);
uint64_t r2 = (( (l1 & BYTE_VEC(0x03))
+ (l2 & BYTE_VEC(0x03))
+ (l3 & BYTE_VEC(0x03))
+ (l4 & BYTE_VEC(0x03))
+ BYTE_VEC(0x02)) >> 2) & BYTE_VEC(0x03);
return r1 + r2;
}
#endif
#define OP(LOAD, STORE) \
do { \
STORE(LOAD(pixels), block); \
pixels += line_size; \
block += line_size; \
} while (--h)
#define OP_X2(LOAD, STORE) \
do { \
uint64_t pix1, pix2; \
\
pix1 = LOAD(pixels); \
pix2 = pix1 >> 8 | ((uint64_t) pixels[8] << 56); \
STORE(AVG2(pix1, pix2), block); \
pixels += line_size; \
block += line_size; \
} while (--h)
#define OP_Y2(LOAD, STORE) \
do { \
uint64_t pix = LOAD(pixels); \
do { \
uint64_t next_pix; \
\
pixels += line_size; \
next_pix = LOAD(pixels); \
STORE(AVG2(pix, next_pix), block); \
block += line_size; \
pix = next_pix; \
} while (--h); \
} while (0)
#define OP_XY2(LOAD, STORE) \
do { \
uint64_t pix1 = LOAD(pixels); \
uint64_t pix2 = pix1 >> 8 | ((uint64_t) pixels[8] << 56); \
uint64_t pix_l = (pix1 & BYTE_VEC(0x03)) \
+ (pix2 & BYTE_VEC(0x03)); \
uint64_t pix_h = ((pix1 & ~BYTE_VEC(0x03)) >> 2) \
+ ((pix2 & ~BYTE_VEC(0x03)) >> 2); \
\
do { \
uint64_t npix1, npix2; \
uint64_t npix_l, npix_h; \
uint64_t avg; \
\
pixels += line_size; \
npix1 = LOAD(pixels); \
npix2 = npix1 >> 8 | ((uint64_t) pixels[8] << 56); \
npix_l = (npix1 & BYTE_VEC(0x03)) \
+ (npix2 & BYTE_VEC(0x03)); \
npix_h = ((npix1 & ~BYTE_VEC(0x03)) >> 2) \
+ ((npix2 & ~BYTE_VEC(0x03)) >> 2); \
avg = (((pix_l + npix_l + AVG4_ROUNDER) >> 2) & BYTE_VEC(0x03)) \
+ pix_h + npix_h; \
STORE(avg, block); \
\
block += line_size; \
pix_l = npix_l; \
pix_h = npix_h; \
} while (--h); \
} while (0)
#define MAKE_OP(OPNAME, SUFF, OPKIND, STORE) \
static void OPNAME ## _pixels ## SUFF ## _axp \
(uint8_t *restrict block, const uint8_t *restrict pixels, \
int line_size, int h) \
{ \
if ((size_t) pixels & 0x7) { \
OPKIND(uldq, STORE); \
} else { \
OPKIND(ldq, STORE); \
} \
} \
\
static void OPNAME ## _pixels16 ## SUFF ## _axp \
(uint8_t *restrict block, const uint8_t *restrict pixels, \
int line_size, int h) \
{ \
OPNAME ## _pixels ## SUFF ## _axp(block, pixels, line_size, h); \
OPNAME ## _pixels ## SUFF ## _axp(block + 8, pixels + 8, line_size, h); \
}
#define PIXOP(OPNAME, STORE) \
MAKE_OP(OPNAME, , OP, STORE) \
MAKE_OP(OPNAME, _x2, OP_X2, STORE) \
MAKE_OP(OPNAME, _y2, OP_Y2, STORE) \
MAKE_OP(OPNAME, _xy2, OP_XY2, STORE)
/* Rounding primitives. */
#define AVG2 avg2
#define AVG4 avg4
#define AVG4_ROUNDER BYTE_VEC(0x02)
#define STORE(l, b) stq(l, b)
PIXOP(put, STORE);
#undef STORE
#define STORE(l, b) stq(AVG2(l, ldq(b)), b);
PIXOP(avg, STORE);
/* Not rounding primitives. */
#undef AVG2
#undef AVG4
#undef AVG4_ROUNDER
#undef STORE
#define AVG2 avg2_no_rnd
#define AVG4 avg4_no_rnd
#define AVG4_ROUNDER BYTE_VEC(0x01)
#define STORE(l, b) stq(l, b)
PIXOP(put_no_rnd, STORE);
#undef STORE
#define STORE(l, b) stq(AVG2(l, ldq(b)), b);
PIXOP(avg_no_rnd, STORE);
static void put_pixels16_axp_asm(uint8_t *block, const uint8_t *pixels,
int line_size, int h)
{
put_pixels_axp_asm(block, pixels, line_size, h);
put_pixels_axp_asm(block + 8, pixels + 8, line_size, h);
}
void dsputil_init_alpha(DSPContext* c, AVCodecContext *avctx)
{
c->put_pixels_tab[0][0] = put_pixels16_axp_asm;
c->put_pixels_tab[0][1] = put_pixels16_x2_axp;
c->put_pixels_tab[0][2] = put_pixels16_y2_axp;
c->put_pixels_tab[0][3] = put_pixels16_xy2_axp;
c->put_no_rnd_pixels_tab[0][0] = put_pixels16_axp_asm;
c->put_no_rnd_pixels_tab[0][1] = put_no_rnd_pixels16_x2_axp;
c->put_no_rnd_pixels_tab[0][2] = put_no_rnd_pixels16_y2_axp;
c->put_no_rnd_pixels_tab[0][3] = put_no_rnd_pixels16_xy2_axp;
c->avg_pixels_tab[0][0] = avg_pixels16_axp;
c->avg_pixels_tab[0][1] = avg_pixels16_x2_axp;
c->avg_pixels_tab[0][2] = avg_pixels16_y2_axp;
c->avg_pixels_tab[0][3] = avg_pixels16_xy2_axp;
c->avg_no_rnd_pixels_tab[0][0] = avg_no_rnd_pixels16_axp;
c->avg_no_rnd_pixels_tab[0][1] = avg_no_rnd_pixels16_x2_axp;
c->avg_no_rnd_pixels_tab[0][2] = avg_no_rnd_pixels16_y2_axp;
c->avg_no_rnd_pixels_tab[0][3] = avg_no_rnd_pixels16_xy2_axp;
c->put_pixels_tab[1][0] = put_pixels_axp_asm;
c->put_pixels_tab[1][1] = put_pixels_x2_axp;
c->put_pixels_tab[1][2] = put_pixels_y2_axp;
c->put_pixels_tab[1][3] = put_pixels_xy2_axp;
c->put_no_rnd_pixels_tab[1][0] = put_pixels_axp_asm;
c->put_no_rnd_pixels_tab[1][1] = put_no_rnd_pixels_x2_axp;
c->put_no_rnd_pixels_tab[1][2] = put_no_rnd_pixels_y2_axp;
c->put_no_rnd_pixels_tab[1][3] = put_no_rnd_pixels_xy2_axp;
c->avg_pixels_tab[1][0] = avg_pixels_axp;
c->avg_pixels_tab[1][1] = avg_pixels_x2_axp;
c->avg_pixels_tab[1][2] = avg_pixels_y2_axp;
c->avg_pixels_tab[1][3] = avg_pixels_xy2_axp;
c->avg_no_rnd_pixels_tab[1][0] = avg_no_rnd_pixels_axp;
c->avg_no_rnd_pixels_tab[1][1] = avg_no_rnd_pixels_x2_axp;
c->avg_no_rnd_pixels_tab[1][2] = avg_no_rnd_pixels_y2_axp;
c->avg_no_rnd_pixels_tab[1][3] = avg_no_rnd_pixels_xy2_axp;
c->clear_blocks = clear_blocks_axp;
/* amask clears all bits that correspond to present features. */
if (amask(AMASK_MVI) == 0) {
c->put_pixels_clamped = put_pixels_clamped_mvi_asm;
c->add_pixels_clamped = add_pixels_clamped_mvi_asm;
c->get_pixels = get_pixels_mvi;
c->diff_pixels = diff_pixels_mvi;
c->sad[0] = pix_abs16x16_mvi_asm;
c->sad[1] = pix_abs8x8_mvi;
c->pix_abs[0][0] = pix_abs16x16_mvi_asm;
c->pix_abs[1][0] = pix_abs8x8_mvi;
c->pix_abs[0][1] = pix_abs16x16_x2_mvi;
c->pix_abs[0][2] = pix_abs16x16_y2_mvi;
c->pix_abs[0][3] = pix_abs16x16_xy2_mvi;
}
put_pixels_clamped_axp_p = c->put_pixels_clamped;
add_pixels_clamped_axp_p = c->add_pixels_clamped;
if (!avctx->lowres &&
(avctx->idct_algo == FF_IDCT_AUTO ||
avctx->idct_algo == FF_IDCT_SIMPLEALPHA)) {
c->idct_put = ff_simple_idct_put_axp;
c->idct_add = ff_simple_idct_add_axp;
c->idct = ff_simple_idct_axp;
}
}
| 123linslouis-android-video-cutter | jni/libavcodec/alpha/dsputil_alpha.c | C | asf20 | 13,644 |
/*
* Alpha optimized DSP utils
* copyright (c) 2002 Falk Hueffner <falk@debian.org>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/* Some BSDs don't seem to have regdef.h... sigh */
#ifndef AVCODEC_ALPHA_REGDEF_H
#define AVCODEC_ALPHA_REGDEF_H
#define v0 $0 /* function return value */
#define t0 $1 /* temporary registers (caller-saved) */
#define t1 $2
#define t2 $3
#define t3 $4
#define t4 $5
#define t5 $6
#define t6 $7
#define t7 $8
#define s0 $9 /* saved-registers (callee-saved registers) */
#define s1 $10
#define s2 $11
#define s3 $12
#define s4 $13
#define s5 $14
#define s6 $15
#define fp s6 /* frame-pointer (s6 in frame-less procedures) */
#define a0 $16 /* argument registers (caller-saved) */
#define a1 $17
#define a2 $18
#define a3 $19
#define a4 $20
#define a5 $21
#define t8 $22 /* more temps (caller-saved) */
#define t9 $23
#define t10 $24
#define t11 $25
#define ra $26 /* return address register */
#define t12 $27
#define pv t12 /* procedure-variable register */
#define AT $at /* assembler temporary */
#define gp $29 /* global pointer */
#define sp $30 /* stack pointer */
#define zero $31 /* reads as zero, writes are noops */
#endif /* AVCODEC_ALPHA_REGDEF_H */
| 123linslouis-android-video-cutter | jni/libavcodec/alpha/regdef.h | C | asf20 | 2,136 |
OBJS += alpha/dsputil_alpha.o \
alpha/dsputil_alpha_asm.o \
alpha/motion_est_alpha.o \
alpha/motion_est_mvi_asm.o \
alpha/mpegvideo_alpha.o \
alpha/simple_idct_alpha.o \
| 123linslouis-android-video-cutter | jni/libavcodec/alpha/Makefile | Makefile | asf20 | 444 |
/*
* Alpha optimized DSP utils
* Copyright (c) 2002 Falk Hueffner <falk@debian.org>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "regdef.h"
/* Some nicer register names. */
#define ta t10
#define tb t11
#define tc t12
#define td AT
/* Danger: these overlap with the argument list and the return value */
#define te a5
#define tf a4
#define tg a3
#define th v0
.set noat
.set noreorder
.arch pca56
.text
/*****************************************************************************
* int pix_abs16x16_mvi_asm(uint8_t *pix1, uint8_t *pix2, int line_size)
*
* This code is written with a pca56 in mind. For ev6, one should
* really take the increased latency of 3 cycles for MVI instructions
* into account.
*
* It is important to keep the loading and first use of a register as
* far apart as possible, because if a register is accessed before it
* has been fetched from memory, the CPU will stall.
*/
.align 4
.globl pix_abs16x16_mvi_asm
.ent pix_abs16x16_mvi_asm
pix_abs16x16_mvi_asm:
.frame sp, 0, ra, 0
.prologue 0
#if CONFIG_GPROF
lda AT, _mcount
jsr AT, (AT), _mcount
#endif
and a2, 7, t0
clr v0
beq t0, $aligned
.align 4
$unaligned:
/* Registers:
line 0:
t0: left_u -> left lo -> left
t1: mid
t2: right_u -> right hi -> right
t3: ref left
t4: ref right
line 1:
t5: left_u -> left lo -> left
t6: mid
t7: right_u -> right hi -> right
t8: ref left
t9: ref right
temp:
ta: left hi
tb: right lo
tc: error left
td: error right */
/* load line 0 */
ldq_u t0, 0(a2) # left_u
ldq_u t1, 8(a2) # mid
ldq_u t2, 16(a2) # right_u
ldq t3, 0(a1) # ref left
ldq t4, 8(a1) # ref right
addq a1, a3, a1 # pix1
addq a2, a3, a2 # pix2
/* load line 1 */
ldq_u t5, 0(a2) # left_u
ldq_u t6, 8(a2) # mid
ldq_u t7, 16(a2) # right_u
ldq t8, 0(a1) # ref left
ldq t9, 8(a1) # ref right
addq a1, a3, a1 # pix1
addq a2, a3, a2 # pix2
/* calc line 0 */
extql t0, a2, t0 # left lo
extqh t1, a2, ta # left hi
extql t1, a2, tb # right lo
or t0, ta, t0 # left
extqh t2, a2, t2 # right hi
perr t3, t0, tc # error left
or t2, tb, t2 # right
perr t4, t2, td # error right
addq v0, tc, v0 # add error left
addq v0, td, v0 # add error left
/* calc line 1 */
extql t5, a2, t5 # left lo
extqh t6, a2, ta # left hi
extql t6, a2, tb # right lo
or t5, ta, t5 # left
extqh t7, a2, t7 # right hi
perr t8, t5, tc # error left
or t7, tb, t7 # right
perr t9, t7, td # error right
addq v0, tc, v0 # add error left
addq v0, td, v0 # add error left
/* loop */
subq a4, 2, a4 # h -= 2
bne a4, $unaligned
ret
.align 4
$aligned:
/* load line 0 */
ldq t0, 0(a2) # left
ldq t1, 8(a2) # right
addq a2, a3, a2 # pix2
ldq t2, 0(a1) # ref left
ldq t3, 8(a1) # ref right
addq a1, a3, a1 # pix1
/* load line 1 */
ldq t4, 0(a2) # left
ldq t5, 8(a2) # right
addq a2, a3, a2 # pix2
ldq t6, 0(a1) # ref left
ldq t7, 8(a1) # ref right
addq a1, a3, a1 # pix1
/* load line 2 */
ldq t8, 0(a2) # left
ldq t9, 8(a2) # right
addq a2, a3, a2 # pix2
ldq ta, 0(a1) # ref left
ldq tb, 8(a1) # ref right
addq a1, a3, a1 # pix1
/* load line 3 */
ldq tc, 0(a2) # left
ldq td, 8(a2) # right
addq a2, a3, a2 # pix2
ldq te, 0(a1) # ref left
ldq a0, 8(a1) # ref right
/* calc line 0 */
perr t0, t2, t0 # error left
addq a1, a3, a1 # pix1
perr t1, t3, t1 # error right
addq v0, t0, v0 # add error left
/* calc line 1 */
perr t4, t6, t0 # error left
addq v0, t1, v0 # add error right
perr t5, t7, t1 # error right
addq v0, t0, v0 # add error left
/* calc line 2 */
perr t8, ta, t0 # error left
addq v0, t1, v0 # add error right
perr t9, tb, t1 # error right
addq v0, t0, v0 # add error left
/* calc line 3 */
perr tc, te, t0 # error left
addq v0, t1, v0 # add error right
perr td, a0, t1 # error right
addq v0, t0, v0 # add error left
addq v0, t1, v0 # add error right
/* loop */
subq a4, 4, a4 # h -= 4
bne a4, $aligned
ret
.end pix_abs16x16_mvi_asm
| 123linslouis-android-video-cutter | jni/libavcodec/alpha/motion_est_mvi_asm.S | Motorola 68K Assembly | asf20 | 6,289 |