Example #1
0
        /*public byte[] Data
         * {
         *      get{return data;}
         * }*/

        public unsafe TheoraDecoder()
        {
            Console.WriteLine(Theora.Version());

            fixed(Theora.th_info *p = &info)
            {
                Theora.th_info_init(p);
            }

            Theora.th_comment_init(ref comment);
        }
Example #2
0
        public int PacketIn(csogg.Packet pp)
        {
            Theora.ogg_packet op = new Theora.ogg_packet();
            op.bytes      = new IntPtr(pp.bytes);
            op.b_o_s      = new IntPtr(pp.b_o_s);
            op.e_o_s      = new IntPtr(pp.e_o_s);
            op.granulepos = pp.granulepos;
            op.packet     = Marshal.UnsafeAddrOfPinnedArrayElement(pp.packet_base, pp.packet);
            op.packetno   = pp.packetno;

            if (!HeadersRead)
            {
                int r = Theora.th_decode_headerin(ref info, ref comment, ref setup, ref op);
                if (r == 0)
                {
                    Console.WriteLine("header complete");
                    width  = (int)info.frame_width;
                    height = (int)info.frame_height;
                    Data   = new byte[width * height * 3];
                    Console.WriteLine(width.ToString() + "x" + height.ToString());
                    HeadersRead = true;
                    decode      = Theora.th_decode_alloc(ref info, setup);
                    if (decode == IntPtr.Zero)
                    {
                        throw new Exception("decode");
                    }
                }
                else
                {
                    return(0);
                }
            }

            ogg_int64_t granpos = 0;
            int         i;

            if (Theora.th_decode_packetin(decode, ref op, ref granpos) == 0)
            {
                //Console.WriteLine("frame complete"+granpos.ToString());
                Theora.th_ycbcr_buffer picture = new Theora.th_ycbcr_buffer();
                i = Theora.th_decode_ycbcr_out(decode, ref picture);
                if (i != 0)
                {
                    throw new Exception("picture");
                }
                float xfactor1 = (float)picture.plane1.width / (float)picture.plane0.width;
                float yfactor1 = (float)picture.plane1.height / (float)picture.plane0.height;
                float xfactor2 = (float)picture.plane2.width / (float)picture.plane0.width;
                float yfactor2 = (float)picture.plane2.height / (float)picture.plane0.height;

                if (ConvertToRGB)
                {
                    for (int y = 0; y < picture.plane0.height; ++y)
                    {
                        for (int x = 0; x < picture.plane0.width; ++x)
                        {
                            float Y  = Marshal.ReadByte(picture.plane0.data, y * picture.plane0.stride + x);
                            float Cb = Marshal.ReadByte(picture.plane1.data, (int)((float)y * yfactor1 * (float)picture.plane1.stride + (float)x * xfactor1));
                            float Cr = Marshal.ReadByte(picture.plane2.data, (int)((float)y * yfactor2 * (float)picture.plane2.stride + (float)x * xfactor2));

                            int R = (int)(Y + 1.402f * (Cr - 128.0f));
                            int G = (int)(Y - 0.34414f * (Cb - 128.0f) - 0.71414f * (Cr - 128.0f));
                            int B = (int)(Y + 1.772f * (Cb - 128.0f));

                            R = Clamp(R, 255, 0);
                            G = Clamp(G, 255, 0);
                            B = Clamp(B, 255, 0);

                            Data[y * width * 3 + x * 3]     = (byte)B;
                            Data[y * width * 3 + x * 3 + 1] = (byte)G;
                            Data[y * width * 3 + x * 3 + 2] = (byte)R;

                            //w.WriteLine(R.ToString()+" "+G.ToString()+" "+B.ToString());
                        }
                    }
                }
                else
                {
                    for (int y = 0; y < picture.plane0.height; ++y)
                    {
                        for (int x = 0; x < picture.plane0.width; ++x)
                        {
                            byte Y  = Marshal.ReadByte(picture.plane0.data, y * picture.plane0.stride + x);
                            byte Cb = Marshal.ReadByte(picture.plane1.data, (int)((float)y * yfactor1 * (float)picture.plane1.stride + (float)x * xfactor1));
                            byte Cr = Marshal.ReadByte(picture.plane2.data, (int)((float)y * yfactor2 * (float)picture.plane2.stride + (float)x * xfactor2));


                            Data[y * width * 3 + x * 3]     = (byte)Y;
                            Data[y * width * 3 + x * 3 + 1] = (byte)Cb;
                            Data[y * width * 3 + x * 3 + 2] = (byte)Cr;

                            //w.WriteLine(R.ToString()+" "+G.ToString()+" "+B.ToString());
                        }
                    }
                }

                //w.Close();
                //f.Close();
                frame++;
                return(1);
            }

            return(0);
        }