
    Cc-                         d Z ddlmZ ddlmZ ddlmZmZmZm	Z	m
Z
mZ ddlmZ g dZ G d de      Z G d	 d
e      Z G d de      Z G d de      Z G d de      ZeZeZddgZy)zn
* ADTS - Audio Data Transport Stream
* ADIF - Audio Data Interchange Format
* See ISO/IEC 13818-7 / 14496-03
    )
StreamInfo)FileType)	BitReaderBitReaderErrorMutagenErrorloadfileconvert_errorendswith)BitPaddedInt)i w iX i   i  iD  i }  i]  i"V  i>  i.  i+  i@  i  c                       e Zd ZdZdZ	 dZ	 ed        Zd Zd Z	e
d        Ze
d        Ze
d        Ze
d	        Ze
d
        Zd Zd Zy)_ADTSStreamz:Represents a series of frames belonging to the same streamr   c                     t        |      } | |      }|j                  |      r|j                         dz
  dz  |_        |S y)zxReturns a possibly valid _ADTSStream or None.

        Args:
            max_bytes (int): maximum bytes to read
              N)r   syncget_positionoffset)clsfileobj	max_bytesrstreams        -/usr/lib/python3/dist-packages/mutagen/aac.pyfind_streamz_ADTSStream.find_stream&   sE     gQ;;y!^^-2q8FMM "    c                    t        |d      }| j                  }|j                          |dkD  rM	 |j                  d      }|dk(  r+|j	                  d      dk(  ry|j                          |dz  }n|dz  }|dkD  rMy# t
        $ r Y yw xY w)	z2Find the next sync.
        Returns True if found.   r               TF)max_ralignbytesbitsr   )selfr   r   bs       r   r   z_ADTSStream.sync4   s    
 	1%	GG		!m
GGAJ<vvayC'#GGININI !m  " s   *A< A< <	BBc                     d| _         || _        d| _        d| _        d| _        d| _        |j                         dz  | _        | j                  | _        y)z.Use _ADTSStream.find_stream to create a streamNr   r   )	_fixed_header_keyr#   r   parsed_frames_samples_payloadr   _start_last)r'   r   s     r   __init__z_ADTSStream.__init__K   sN     "&nn&*[[
r   c                     | j                   sJ d       | j                  dk(  ryd| j                  z  | j                  z  | j                  z  S )z4Bitrate of the raw aac blocks, excluding framing/crcno frame parsed yetr   r   )r,   r-   r.   	frequencyr'   s    r   bitratez_ADTSStream.bitrateX   sI     !!8#88!==ADMM!DNN2t}}DDr   c                 @    | j                   sJ d       | j                  S )zsamples so farr3   )r,   r-   r5   s    r   samplesz_ADTSStream.samplesc   s#     !!8#88!}}r   c                 Z    | j                   sJ d       | j                  | j                  z
  S )z3bytes read in the stream so far (including framing)r3   )r,   r0   r/   r5   s    r   sizez_ADTSStream.sizek   s,     !!8#88!zzDKK''r   c                 b    | j                   sJ d       | j                  d   }|dk(  ry|dkD  ry|S )0 means unknownr3         r   r   )r,   r+   )r'   b_indexs     r   channelsz_ADTSStream.channelss   sB     !!8#88!((+a<q[Nr   c                 x    | j                   sJ d       | j                  d   }	 t        |   S # t        $ r Y yw xY w)r<   r3   r    r   )r,   r+   _FREQS
IndexError)r'   f_indexs     r   r4   z_ADTSStream.frequency   sJ     !!8#88!((+	'?" 		s   - 	99c                 B    	 | j                         S # t        $ r Y yw xY w)zqTrue if parsing was successful.
        Fails either because the frame wasn't valid or the stream ended.
        F)_parse_framer   r5   s    r   parse_framez_ADTSStream.parse_frame   s(    
	$$&& 		s    	c           	         | j                   }|j                         dz
  }|j                  d      }|j                  d      }|j                  d      }|j                  d      }|j                  d      }|j                  d      }|j                  d      }	|j                  d      }
|j                  d      }|||||||	|
|f	}| j                  || _        n| j                  |k7  ry|j	                  d       |j                  d      }|j	                  d       |j                  d      }d	}|s||dz   d
z  z  }|d	k7  r|dz  }|dz  |j                         |z
  z
  }|d	k  ry|j	                  |       |j                         sJ | xj                  ||z
  dz  z  c_        | xj                  |dz   dz  z  c_        |j                         dz  | _        | xj                  dz  c_	        y)Nr   r   r   r       F      r      r   i   T)
r#   r   r&   r+   skip
is_alignedr.   r-   r0   r,   )r'   r   startid_layerprotection_absentprofilesampling_frequency_indexprivate_bitchannel_configurationoriginal_copyhomefixed_header_keyframe_lengthnordbifcrc_overheadlefts                    r   rF   z_ADTSStream._parse_frame   s   GG 2% ffQiq	FF1I&&)#$66!9 ffQi !q	q	vvay )74L.t

 !!)%5D"%%)99 	
q	vvbz	r
&&)  Wq[B..L!|!q Q^^%5%=>!8	t||~~$-22'A+--^^%)
ar   N)__name__
__module____qualname____doc__r,   r   classmethodr   r   r1   propertyr6   r8   r:   r@   r4   rG   rF    r   r   r   r      s    DM.FJ .! E E   ( (   	 	6r   r   c                   "    e Zd ZdZdZdZdZd Zy)ProgramConfigElementNc                    |j                  d      | _        |j                  d      | _        |j                  d      | _        |j                  d      }|j                  d      }|j                  d      }|j                  d      }|j                  d      }|j                  d      }|j                  d      }|dk(  r|j	                  d       |j                  d      }	|	dk(  r|j	                  d       |j                  d      }
|
dk(  r|j	                  d       ||z   |z   }d}t        |      D ]0  }|dz  }|j                  d      }|r|dz  }|j	                  d       2 ||z  }|| _        |j	                  d|z         |j	                  d|z         |j	                  d|z         |j                          |j                  d      }|j	                  d|z         y)	zJReads the program_config_element()

        Raises BitReaderError
        r    r   rI   r   r      r   N)r&   element_instance_tagobject_typerT   rM   ranger@   r$   )r'   r   num_front_channel_elementsnum_side_channel_elementsnum_back_channel_elementsnum_lfe_channel_elementsnum_assoc_data_elementsnum_valid_cc_elementsmono_mixdown_presentstereo_mixdown_presentmatrix_mixdown_idx_presentelmsr@   ielement_is_cpecomment_field_bytess                   r   r1   zProgramConfigElement.__init__   s    %&FF1I!66!9()q	%%&VVAY"$%FF1I!$%FF1I!#$66!9 "#&&) !q	 vvay1$FF1I!"!Q&FF1I%&VVAY"%*FF1I),EE%&t 	AMHVVAYNAFF1I	 	,, 	q++,	q**+	q(()		ffQi	q&&'r   )r^   r_   r`   ri   rj   rT   r@   r1   rd   r   r   rf   rf      s    K#H+(r   rf   c                       e Zd Zy)AACErrorN)r^   r_   r`   rd   r   r   rz   rz     s    r   rz   c                   P    e Zd ZdZdZdZdZdZ ee	e
      d        Zd Zd Zd Zy)AACInfoae  AACInfo()

    AAC stream information.
    The length of the stream is just a guess and might not be correct.

    Attributes:
        channels (`int`): number of audio channels
        length (`float`): file length in seconds, as a float
        sample_rate (`int`): audio sampling rate in Hz
        bitrate (`int`): audio bitrate, in bits per second
    r   c                 $   d}|j                  d      }|j                  d      rt        |dd       }|dz   }|j                  |       |j                  d      }|dk(  r| j	                  |       d| _        y| j                  ||       d	| _        y)
zRaises AACErrorr   
   s   ID3r=   Nr       ADIFADIFADTS)read
startswithr   seek_parse_adif_type_parse_adts)r'   r   start_offsetheaderr:   adifs         r   r1   zAACInfo.__init__  s    
 b!V$qr
+D"9L\"||A7?W%DJWl3DJr   c                    t        |      }	 |j                  d      }|r|j                  d       |j                  d       |j                  d      }|j                  d      | _        |j                  d      }|dk(  r|j                  d       t	        |      }	 t
        |j                     | _        |j                  | _	        t        |      D ]  }t	        |        |j                          |j                         }	|j                  dd       |j                         |	z
  }
| j                  dk7  rd|
z  | j                  z  | _        y y # t        $ r Y w xY w# t        $ r}t        |      d }~ww xY w)	Nr   H   r      r    r      g       @)r   r&   rM   r6   rf   rB   rT   sample_raterC   r@   rk   r$   r   rz   tellr   length)r'   r   r   copyright_id_presentbitstream_typenpcepcerv   erO   r   s              r   r   zAACInfo._parse_adif/  sH   g	#$66!9 #r
FF5MVVAYN66":DL66!9D"r
&q)C#)#*F*F#G   LLDM 4[ ($Q'(GGI
 Q%'<<1<4<<7DK     	1+	s<   BE E 4<E 	EE EE 	E5%E00E5c                    d}d}d}d}d}|}t        |      D ]  }	|j                  |       t        j                  ||      }
|
t	        d      ||
j
                  dz   z  }t        |      D ]'  }	|
j                         s n|
j                  |      r' n |
j                  |k\  s n t	        d
j                  z        |
j                  | _
        |
j                  | _        |
j                  | _        |j                  dd	       |j                         ||
j
                  z   z
  }d
| _        |
j                  dk7  r8t        |
j                   |z        |
j"                  |
j                  z  z  | _        y y )Ni   r~   d   rI   zsync not foundr   z&no valid stream found (only %d frames)r   r   g        )rk   r   r   r   rz   r   rG   r   r,   r4   r   r@   r6   r   r   floatr8   r:   )r'   r   r   max_initial_readmax_resync_readmax_sync_tries
frames_maxframes_neededr   rv   sstream_sizes               r   r   zAACInfo._parse_adtsQ  sg   
 ~& 	LALL ''1ABAy/00ahhl"F:& }}vvo.	 -/	L" 81??JL L ;;

yy 	Qlln(9:;;!aii+-.!&&1;;2FG K r   c                 z    d| j                   | j                  | j                  | j                  | j                  fz  S )Nz4AAC (%s), %d Hz, %.2f seconds, %d channel(s), %d bps)r   r   r   r@   r6   r5   s    r   pprintzAACInfo.pprint}  s7    FJJ(($++t}}LLJ  	r   N)r^   r_   r`   ra   r@   r   r   r6   r	   IOErrorrz   r1   r   r   r   rd   r   r   r|   r|   	  sH    
 HFKG7H%  & & 8D*HXr   r|   c                   F    e Zd ZdZdgZ e       d        Zd Zed        Z	y)AACzAAC(filething)

    Arguments:
        filething (filething)

    Load ADTS or ADIF streams containing AAC.

    Tagging is not supported.
    Use the ID3/APEv2 classes directly instead.

    Attributes:
        info (`AACInfo`)
    zaudio/x-aacc                 8    t        |j                        | _        y )N)r|   r   info)r'   	filethings     r   loadzAAC.load  s    I--.	r   c                     t        d      )Nzdoesn't support tags)rz   r5   s    r   add_tagszAAC.add_tags  s    -..r   c                     | j                         } t        | d      xs t        | d      xs t        | d      }|d|v z  }|S )Nz.aacz.adtsz.adifr   )lowerr
   )filenamer   r   r   s       r   scorez	AAC.score  sN    >>#Xv& ((8W*E (Xw' 	
	Wr   N)
r^   r_   r`   ra   _mimesr   r   r   staticmethodr   rd   r   r   r   r     s=     _FZ/ //  r   r   OpenN)ra   mutagenr   mutagen._filer   mutagen._utilr   r   r   r   r	   r
   mutagen.id3._utilr   rB   objectr   rf   rz   r|   r   r   error__all__rd   r   r   <module>r      s     "  *
p& pf2(6 2(j	| 	wj wt( B &/r   